HomeTaskerBluetooth battery monitor - monitor any Bluetooth battery with Tasker

Bluetooth battery monitor – monitor any Bluetooth battery with Tasker

Measure the battery use of Bluetooth devices

I love my HBS-730 Bluetooth headphones. They come with all the bells and whistles, heck, even a battery feedback. All good but one little detail: the battery feedback comes in 3 levels HIGH, MEDIUM, LOW. Here is the problem:

  • HIGH – the device just has been charged, has full battery, mode lasts about 10 min
  • MEDIUM – this is where you spend the 99% of you 8h power time
  • LOW – battery about to die, you have literally 5 min to get a cable and start charging

It is entirely possible for me to go for a bike ride with 6 min left on my headphones thinking that I will be ok. The companion app although exist and let you pass through calls, messages etc, has no information about the exact battery level of the headphones. I had enough, time to employ Tasker.  The Bluetooth battery monitor lets you keep track of any Bluetooth device, even if that gadget has no battery indicator whatsoever.

Tasker – Bluetooth battery monitor

screenshot_20161003-121554

Although you can streamline the project quite a bit with plugins, I’m using vanilla Tasker for best compatibility with all Android users. The project consists three profiles. Not all of them are necessary, but they create an application that you can use straight away. The only editing that needs to be done is entering the device name in the Bluetooth battery monitor context.

The project offers you 2 calibrations modes.
You can choose to measure a lifespan of your fully charged device (more accurate). This measurement will be used in battery life estimation later.
Alternatively, you can enter an estimated value of the battery life in hours and minutes.

The calibration is being done once, then the main profile is enabled. A notification will appear on each connects and disconnects from the Bluetooth device to let you know about the estimated life span.

Headset override

This profile is responsible for opening the calibration scene and making sure the calibration profiles are triggered correctly. Initially, the Bluetooth device had to be off, for calibration to work correctly, now it doesn’t matter.

We are using BT device to set a variable (%BToverride), then to launch a scene unless the profile has been launched before. Once calibration screen is open it won’t run again (%Startup).

PROFILE - Headset Override
Profile: Headset Override 
  State: BT Connected [ Name:LG HBS730 Address:* ]
Enter: BT Connection Override 
  A1: Variable Set [ Name:%B_Toverride To:1 Do Maths:Off Append:Off ]
  A2: Perform Task [ Name:Start Scene Priority:%priority 
      Parameter 1 (%par1): Parameter 2 (%par2): Return Value Variable: Stop:Off ]
      If [ %Startup !Set ]
Exit: BT Connection Override-
  A1: Variable Set [ Name:%B_Toverride To:0 Do Maths:Off Append:Off ]

Start scene

screenshot_20161003-121454

This task creates then opens a scene. It also notifies our headset override profile that set up has been completed (%Startup =1). The scene contains initial elements and a few hidden ones that will become visible when selection is done. The calibration button simply closes the scene and enables a calibration profile. Time button hides other elements and enables all hidden UI elements including number pickers for an hour and minute values. These are written to variables %BTh and %BTm and converted later to a total number of seconds. The OK button closes the scene and sets the %B_Tmax value – which is the maximum battery life. Please note that %BT and %B_T spelling is intentional as not all variables will be cleared by the reset button later.

TASK Start scene
A1: Create Scene [ Name:BT Start ] If [ %Startup !Set ]
  A2: Show Scene [ Name:BT Start Display As:Dialog, Dim Behind Heavy Horizontal
    Position:100 Vertical Position:100 Animation:System Show Exit Button:On
    Continue Task Immediately:On ] If [ %Startup !Set ]
  A3: Variable Set [ Name:%Startup To:1 Do Maths:Off Append:Off ]

BT calibration

This mode will measure how long your device lasts on a single charge. Bluetooth battery monitor will use this value to set a correct %B_Tmax. The profile alone is a simplified version of the main profile, all variable referenced have the %BTC prefix to it. Each time your device is disconnected from the mobile a notification is issued that gives you the option to continue measurement if the device is not discharged, or end the calibration if your Bluetooth device run out of power.

When calibration is done the BT measuring done task is executed. It will set the correct %B_Tmax and clear the %BTC related variables. At this point, the main profile becomes activated and the calibration profile is no longer in use.

PROFILE - BLuetooth Calibration
State: BT Connected [ Name:LG HBS730 Address:* ]
Enter: BTC Connect
  A1: Notify Cancel [ Title:BT Battery Warn Not Exist:Off ]
  A2: Variable Set [ Name:%BTCstart To:%TIMES Do Maths:Off Append:Off ]
Exit: BTC Disconnect 
  A1: Variable Set [ Name:%BTCstop To:%TIMES - %BTCstart Do Maths:On Append:Off ]
  A2: Variable Clear [ Name:%BTCstart Pattern Matching:Off ]
  A3: Variable Set [ Name:%BTCtotal To:0 Do Maths:Off Append:Off ] If [ %BTCtotal !Set ]
  A4: Variable Set [ Name:%BTCtotal To:%BTCtotal+%BTCstop Do Maths:On Append:Off ]
  A5: Variable Clear [ Name:%BTCstop Pattern Matching:Off ]
  A6: Notify [ Title:BT Battery Text:Would you like to: Icon:hd_device_access_bluetooth_connected
      Number:0 Permanent:Off Priority:3 Actions:(2) ]

BT battery MAIN

screenshot_20161003-121705

This is the meat of things. When connected, if previous readings are available, a notification will let you know how long your device will live (%btttlchour and %btttlcminute) This values are calculated in a disconnect task and come from %BTttl (time to live) estimation. If the profile is run for the  first time a current time in seconds is measured and set as %BTstart. If a notification has been displayed before it will close within 10 sec.

The actions responsible for calculating minutes and hours from  total seconds value are shared across the connect and disconnect tasks. These are slightly modified for the purpose of displaying a correct message.

To calculate the estimated time to live we have to take into consideration two scenarios:

  • TTL<60 min
  • TTL>60 min

By default system will treat the %BTttl as if was smaller than 60 min and values of %btttlchours/minutes will become overridden in an IF condition should that value was greater than 60. The notification gives us two options. Close, if the Bluetooth device has some battery life in it (close notification) and reset if the battery device needs a charge (clear all %BT variables). The %B_Tmax will be unaffected.

PROFILE - BT Battery MAIN
State: BT Connected [ Name:LG HBS730 Address:* ]
Enter: BT Connect 
  Abort Existing Task
  A1: Notify Cancel [ Title:BT Battery Warn Not Exist:Off ]
  A2: Variable Set [ Name:%btttlc To:ceil(%BTttl/60) Do Maths:On Append:Off ] If [ %BTttl Set ]
  A3: Variable Set [ Name:%btttlchour To:0 Do Maths:Off Append:Off ]
  A4: Variable Set [ Name:%btttlcminute To:%btttlc Do Maths:Off Append:Off ]
  A5: If [ %btttlc > 60 ]
  A6: Variable Set [ Name:%btttlcminute To:(%btttlc)%60 Do Maths:On Append:Off ]
  A7: Variable Set [ Name:%btttlchour To:ceil((%btttlc)/60) Do Maths:On Append:Off ]
  A8: End If
  A9: Notify [ Title:BT Battery Text:You have %btttlchour H %btttlcminute min left 
      Icon:hd_device_access_bluetooth_connected Number:1 
      Permanent:Off Priority:1 ] If [ %BTttl Set ]
  A10: Variable Set [ Name:%BTstart To:%TIMES Do Maths:Off Append:Off ]
  A11: Wait [ MS:0 Seconds:10 Minutes:0 Hours:0 Days:0 ]
  A12: Notify Cancel [ Title:BT Battery Warn Not Exist:Off ]
Exit: BT Disconnect 
  A1: Variable Set [ Name:%BTstop To:%TIMES - %BTstart Do Maths:On Append:Off ]
  A2: Variable Clear [ Name:%BTstart Pattern Matching:Off ]
  A3: Variable Set [ Name:%BTtotal To:0 Do Maths:Off Append:Off ] If [ %BTtotal !Set ]
  A4: Variable Set [ Name:%BTtotal To:%BTtotal+%BTstop Do Maths:On Append:Off ]
  A5: Variable Clear [ Name:%BTstop Pattern Matching:Off ]
  A6: Variable Set [ Name:%BTttl To:%B_Tmax-%BTtotal Do Maths:On Append:Off ]
  A7: Variable Set [ Name:%btttlc To:ceil(%BTttl/60) Do Maths:On Append:Off ] If [ %BTttl Set ]
  A8: Variable Set [ Name:%btttlchour To:0 Do Maths:Off Append:Off ]
  A9: Variable Set [ Name:%btttlcminute To:%btttlc Do Maths:Off Append:Off ]
  A10: If [ %btttlc > 60 ]
  A11: Variable Set [ Name:%btttlcminute To:(%btttlc)%60 Do Maths:On Append:Off ]
  A12: Variable Set [ Name:%btttlchour To:ceil((%btttlc)/60) Do Maths:On Append:Off ]
  A13: End If
  A14: Notify [ Title:BT Battery Text:You have %btttlchour H %btttlcminute min left 
       Icon:hd_device_access_bluetooth_connected Number:1 
       Permanent:Off Priority:1 Actions:(2) ] If [ %BTttl Set ]

Calculations

The math behind is simple.

  • Time of disconnect (s) – Time of connect (s) = %BTstop
  • Time in use %BTtotal(s) + %BTstop = %BTtotal
  • Time to live (s) = B_Tmax -%BTtotal

To calculate the seconds into minutes we need two operations. For TTL below 60min we have:

%BTTTLCHOUR = 0 AND %BTTTLCMINUTES = (%BTTTC)%60 (%BTTTC IS A COPY OF %BTTTL)

For TTL above 60 min:

%BTTTLCHOUR = CEIL(%BTTTLC)/60  AND %BTTTLCMINUTES = (%BTTTC)%60 (%BTTTC IS A COPY OF %BTTTL)

Ready files

As usual, you can follow Bluetooth battery monitor tutorial and the video above. In addition to this, you can download the complete file below. You can also consider supporting the website, it starts from 0.99  and it means a lot! Either way happy talking!

Project Download

Download project files here. Bear in mind that Patreon supporters have early access to project files and videos.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

New to Tasker?

Tasker Quick Start – Getting started with Tasker

0
From newb to not so newbie in 10 min

Best Tasker Projects

How to use Raspberry PI as WOL (wake on lan) server

0
While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

How to wake on LAN computers and put it to sleep with Power Menu,...

0
How to Wake on LAN properly via Android, Alexa, Google Assistant and Web

7 awesome Bluetooth keyboard shortcuts for Android

0
7 unique Android shortcuts that you can add to any Bluetooth keyboard.

Smart overnight charging with Tasker

0
Still keeping your phone plugged in overnight? Try smarter overnight charging with this profile

One thing that Join app can’t do and how to fix it with Tasker

0
It's not possible to share the clipboard automatically between join accounts registered to 2 different emails. But you can fix this with tasker.

Essential Guides

Tasker: Seconds into DD:HH:MM:SS (dynamic)

0
It's time to.... ok it's a pun, but I will show you how to master time and convert seconds to DD:HH:MM:SS dynamically

4 ways to organise Tasker projects

0
Keep your Tasker tidy!

A better way to store Tasker credentials

0
The more clever way of managing credentials

Annoyed with dozens of AutoApps populating your app drawer? Here is a fix!

0
Clear your app drawer from the clutter in seconds

Putting AutoTools pie chart to a good use – SSID logger

0
Who wants a piece of the pie (chart)?