Bluetooth_Headset_Battery_Level
Bluetooth_Headset_Battery_Level copied to clipboard
s.recv(128) gets stuck (line 81)
Hi, I have been wanting something like this for a long time, so thank you for making this.
I have a Jabra 65e bluetooth headset. It shows the battery level when I connect to my phone. When I run this script however, for every port other than 3, I get an Errno 111 Connection refused.
However, when I run it with port 3, the script gets stuck. Specifically, the s.recv(128) function does not return in the while loop on line 81.
I don't know anything about low level network interfaces, so can you help me figure out what might be going wrong?
Thanks a lot!
Hi. This is a known issue with some devices. I still don't have a solution for it.
Hi! Thank you so much for this code. It works fine with a Bluetooth speaker using port .2-9 suffix. Unfortunately, I encounter the same error as EkansheepGupta when trying to read the battery level of either my iPhone 4S or iPhone 6s without defining a port suffix. When defining any port suffix I get {device} is offline [Errno 104] Connection reset by peer. This happens allthough the iPhone still says that it is connected.
Best regards!
Hi @maxwell380. This code only supports Bluetooth accessories and not phones, etc... Why do you need that anyway? The phone can show its battery level.
Thanks for the quick response, @TheWeirdDev. I am using a Pi4 with Python and would like to indicate the battery level of the iPhone in an UI on the Pi. Similar to what you may have in your car.
Hi. This is a known issue with some devices. I still don't have a solution for it.
Can you explain or point me to some resources so I can understand better what exactly is happening? And is there literally anything I can try that might work at this point?
I tried another bluetooth headset and faced the same issue.
Can you explain or point me to some resources so I can understand better what exactly is happening? And is there literally anything I can try that might work at this point?
This code simply opens a Bluetooth Socket and expects to receive raw AT commands from the headset (usually the first command that is received is AT+BRSF).
This problem happens to one of my speakers too. I checked the sniffed data from my android phone and it seems like there is some sort of RFCOMM communication before receiving the AT Commands (maybe that's what wee need?).

Currently i'm busy with my exams and don't have enough time to research on this. For now there is nothing you can try except the things i mentioned in the 'readme' file. Or you can research and figure out what is going on. Every pull request is welcome and appreciated.
Getting same behaviour on Sennheiser headphones:
Device 00:16:94:xx:xx:xx (public)
Name: HD 4.40BT
Alias: HD 4.40BT
Class: 0x00240404
Icon: audio-card
Paired: yes
Trusted: yes
Blocked: no
Connected: yes
LegacyPairing: no
UUID: Headset (00001108-0000-1000-8000-00805f9b34fb)
UUID: Audio Sink (0000110b-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
UUID: Advanced Audio Distribu.. (0000110d-0000-1000-8000-00805f9b34fb)
UUID: A/V Remote Control (0000110e-0000-1000-8000-00805f9b34fb)
UUID: Handsfree (0000111e-0000-1000-8000-00805f9b34fb)
UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb)
Modalias: bluetooth:v0A12p0001d0000
Every port responds with following message, although i'm currently using headphones perfectly:
spurlos@spurlos:~/Bluetooth_project$ bluetooth_battery 00:16:94:xx:xx:xx.6
00:16:94:xx:xx:xx is offline [Errno 111] Connection refused
With OnePlus Buds Z on 3 ports got stuck: 3, 7 & 9 Interrupt with Ctrl+C
Traceback (most recent call last):
File "bluetooth_battery.py", line 167, in <module>
main()
File "bluetooth_battery.py", line 164, in main
print("Battery level for {} is {}".format(device, str(query)))
File "bluetooth_battery.py", line 94, in __str__
return "{:.0%}".format(self._perform_query() / 100)
File "bluetooth_battery.py", line 104, in _perform_query
for line in sock:
File "bluetooth_battery.py", line 35, in __next__
return self._sock.recv(self._chunk_size)
File "<string>", line 3, in recv
KeyboardInterrupt
When call on 9 also breaks audio play