Bluetooth music player

I use this code for Bluetooth and % battery in the music player.

$if(nc(bt)=2,nc(aname, 0), Muziek speler)$ $if(nc(bt)=0,offline,nc(bt)=1,enabled,nc(bt)=2 & nc(abatt, 0)>0,nc(abatt, 0)+“%”,nc(aname, 0))$

Sometimes I see the device 2 times.
When I choose this I see only %
$if(nc(bt)=0,offline,nc(bt)=1,enabled,nc(bt)=2 & nc(abatt, 0)>0,nc(abatt, 0)+“%”,nc(aname, 0))$

What do I wrong?

Hi there,

Just to make sure you already know about this:

  • nc(abatt, #)
  • nc(aname, #)

The # is the index number of the connected Bluetooth device.

Sometimes it may be possible that a Bluetooth device get a different index number everytime it connect to the phone (if there are multiple connected Bluetooth devices). So, you might need to check the name of the connected Bluetooth device before getting its battery level info.

I hope this would help.
:smiling_face::+1:

When I understand. I must write the name from all the device I use.

For example I have
JBL1 and JBL3

  • nc(abatt, #JBL1, #JBL3)
  • nc(aname, #JBL1, #JBL3)

Is this correct?

Hi there,

Not exactly, the index number should be written as a plain integer, without any other characters.

Example:

  • nc(abatt, 0) ➞ get the battery level of the first connected Bluetooth device (which usually is the phone itself)
  • nc(abatt, 1) ➞ get the battery level of the second connected Bluetooth device (if supported by the device)
  • … and so on

As far as I know, some Bluetooth headsets support the battery level info, I have a pair of them that gives the battery level info. But my Bluetooth speaker and my Bluetooth keyboard do not support it.

i hope this would help.
:smiling_face::+1:

1 Like

I have edit my code that I use. Is this correct?

$if(nc(bt)=2,nc(aname, 0), Muziek speler)$ $if(nc(bt)=0,offline,nc(bt)=1,enabled,nc(bt)=2 & nc(abatt, 0)>0,nc(abatt, 1), (abbat, 2), (abbat, 3)+“%”,nc(aname, 0))$

Hi there,

Here is my example of Kustom script to iterate the connected Bluetooth devices, and if the name of one of them match then show its battery level (if supported by the device).

I use fl() function for the loop to check every connected Bluetooth device.

$
fl(
  0, nc(acount)-1, "i+1",
  "if(
    tc(low, nc(aname, i))=bt_name_to_check,
    nc(abatt, i)
  )"
)
$

NOTE

  • It is important to include a pair of " (double quote) in the fl() function.
  • Replace the text bt_name_to_check with the name of your Bluetooth device.

I hope this would help.
:smiling_face::+1:

1 Like

I have a few devices. Can i use the same code and only change the name of the device?

Hi there,

If I may ask, what do you want to achieve by the way?

Hi there,

Here is another example of checking connected Bluetooth devices, and show the battery level info for specific Bluetooth devices.

$
fl(
  0, nc(acount)-1, "i+1",
  "if(
    tc(low, nc(aname, i))=bt_name_to_check_1 |
    tc(low, nc(aname, i))=bt_name_to_check_2 |
    tc(low, nc(aname, i))=bt_name_to_check_3,
    nc(abatt, i)
  )"
)
$

➞ show the battery level info if the name of the Bluetooth devices are: bt_name_to_check_1 OR bt_name_to_check_2 OR bt_name_to_check_3

I hope this would help.
:smiling_face::+1:

Hi there,

Here is a fine tuned script.

$
fl(
  0, nc(acount)-1, "i+1",
  "if(
    tc(low, nc(aname, i))=bt_name_to_check_1 |
    tc(low, nc(aname, i))=bt_name_to_check_2 |
    tc(low, nc(aname, i))=bt_name_to_check_3,
    nc(abatt, i)+tc(utf, 25)
  )", ", "
)
$

➞ this should show the battery level info in the form of comma-separated like this: 100%, 70%, 80%

I have use your code for 1 device I now use and see now -1

$
fl(
0, nc(acount)-1, “i+1”,
“if(
tc(low, nc(aname, i))=Clib 5 Fred |
tc(low, nc(aname, i))=bt_name_to_check_2 |
tc(low, nc(aname, i))=bt_name_to_check_3,
nc(abatt, i)
)”
)
$

Try this script to show the name of every connected Bluetooth device:

$
fl(
  0, nc(acount)-1, "i+1",
  "nc(aname, i)",
  ", "
)
$

Then, use those names.

I think nc(aname, #) shows the Bluetooth device name not the custom name we set in the Bluetooth settings.

If nc(abatt, #) gives -1 result, then it’s not supported by the device. In other words, the device does not provide the information about its battery level.

Thank you. When I am home I try another device.

I have try the Buds. They work fine.
There is 1 device that don’t support battery %.

Thank for your code.

1 Like