TheWeirdDev / Bluetooth_Headset_Battery_Level

A python script to get battery level from Bluetooth headsets
GNU General Public License v3.0
766 stars 80 forks source link

bluetooth.btcommon.BluetoothError: [Errno 111] Connection refused #76

Open HarshalRathore opened 2 years ago

HarshalRathore commented 2 years ago

Installation is done with yay -S python-bluetooth-battery

command run bluetooth_battery $(bluetoothctl info | awk '/^Device/ {print $2}') and sudo bluetooth_battery $(bluetoothctl info | awk '/^Device/ {print $2}')

Error received

Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (111, 'Connection refused')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/bluetooth_battery", line 8, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/bluetooth_battery/bluetooth_battery.py", line 163, in main
    print("Battery level for {} is {}".format(device, str(query)))
  File "/usr/lib/python3.10/site-packages/bluetooth_battery/bluetooth_battery.py", line 93, in __str__
    return "{:.0%}".format(self._perform_query() / 100)
  File "/usr/lib/python3.10/site-packages/bluetooth_battery/bluetooth_battery.py", line 101, in _perform_query
    sock.connect(self._bt_settings)
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: [Errno 111] Connection refused

---- Update ----

Tried Manual installation, returned with the same error

Also tried to run with docker, returned with the same error.

---- Conculsion ----

Problem lies in the project/script not in the underlying or user's OS.

MadFlasheroo7 commented 2 years ago

Did u find any fixes?

HarshalRathore commented 2 years ago

@MadFlasheroo7 no I haven't yet, waiting for the author of the project to respond.

TheWeirdDev commented 2 years ago

This error could be caused by many reasons. Some devices are not supported by this script and some have issues. I need to have access to the specific device in order to debug and test.

You can try the workarounds mentioned in the readme file, maybe that fixes the issue.

P3nguin-M commented 2 years ago

Same issue here, would like to help on this although i can't send you a phone. Telegram @toxicbyte

The actual failure is when its trying to itterate through sock object: result = None sock = RFCOMMSocket() sock.connect(self._bt_settings) for line in sock: if b"BRSF" in line:

I've been doing some smalll debugging and noticed the UUID isn't listed on my device (Galaxy S10 - OS: 9.0)

Here's what it lists though: UUID: OBEX Object Push (00001105-0000-1000-8000-00805f9b34fb) UUID: Headset (00001108-0000-1000-8000-00805f9b34fb) UUID: Audio Source (0000110a-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: Headset AG (00001112-0000-1000-8000-00805f9b34fb) UUID: PANU (00001115-0000-1000-8000-00805f9b34fb) UUID: NAP (00001116-0000-1000-8000-00805f9b34fb) UUID: Handsfree Audio Gateway (0000111f-0000-1000-8000-00805f9b34fb) UUID: Phonebook Access Server (0000112f-0000-1000-8000-00805f9b34fb) UUID: Message Access Server (00001132-0000-1000-8000-00805f9b34fb) UUID: PnP Information (00001200-0000-1000-8000-00805f9b34fb) UUID: Generic Access Profile (00001800-0000-1000-8000-00805f9b34fb) UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb) UUID: Vendor specific (594a34fc-31db-11ea-978f-2e728ce88125) UUID: Vendor specific (a23d00bc-217c-123b-9c00-fc44577136ee)

Please contact me if you want help with resolving. I can help debug a bit.

P3nguin-M commented 2 years ago

Connection is opening and connecting: Under class SocketDataIterator return the received socket data is being held up. have tried different chunk sizes, etc. The AT commands are sending to the device using logcat I found: 01-30 11:32:36.776 1661 1661 I Telecom:SamsungGearCommand: sendSecBluetoothATCommand : DUAL,S****E

Any ideas why the socket is unable to receive data back?

EDIT: AT+BIEV= should be working, if not maybe this alternate will help? I don't understand your format. I'm used to doing soemthing like "AT+BIEV=" write to serial

Here's the AT command to fetch battery info via AT on samsung: AT+BATGETLEVEL? BATGETLEVEL=6 VOLT=4210mV

TheWeirdDev commented 2 years ago

@P3nguin-M Good job with the debugging. Can you also sniff the commands using this trick and send me the capture file? Include your logcat as well.

P3nguin-M commented 2 years ago

@P3nguin-M Good job with the debugging. Can you also sniff the commands using this trick and send me the capture file? Include your logcat as well.

btsnoop_hci.log

I just wanted to clarify -- I'm using this tool not to read battery but just communicate with bluetooth stack using linux PC with bluetooth USB chip and bluetoothctl. Although I believe it's sending the AT commands I am not seeing ANY in the log i dumped after connecting it to my machine and running your script on it. I get past the socket connection, but it freezes on the read.

TheWeirdDev commented 2 years ago

This script is written only with battery level in mind. The commands which are being sent to the device might be wrong, and the device stops answering back. You can add debug prints to the program to see what is being sent/received. You can also look for similar commands in the logcat that you found before, and update the code.

P3nguin-M commented 2 years ago

This script is written only with battery level in mind. The commands which are being sent to the device might be wrong, and the device stops answering back. You can add debug prints to the program to see what is being sent/received. You can also look for similar commands in the logcat that you found before, and update the code.

I figured if i can get the bluetooth battery stuff working i can modify afterwards to send the AT commands i extracted from the device's firmware. Is there any other scripts that you know of that lets us do just that? Test AT commands without modifying yours? Also notice you don't use any "at+" commands but just the endings. How does that work?

TheWeirdDev commented 2 years ago

The code is fairly small. You can remove all the battery stuff and replace your own commands. I don't remember any script that does what you want but there was a java app called 'AT Command Tester' I think.

Also notice you don't use any "at+" commands but just the endings. How does that work?

The headset sends commands with the 'at+' prefix but the response doesn't have that. We got these commands and their responses mostly by reading Bluetooth captures, and that's what we found there. I'm not an expert in this.

MadFlasheroo7 commented 2 years ago

This error could be caused by many reasons. Some devices are not supported by this script and some have issues. I need to have access to the specific device in order to debug and test.

You can try the workarounds mentioned in the readme file, maybe that fixes the issue.

how can i contribute add support for my devices

TheWeirdDev commented 2 years ago

@MadFlasheroo7 By capturing the Bluetooth connection from your phone and the headset, then analyzing the commands being sent/received. Then you can change code in order to support your device. It needs a lot of debugging and trial and error to get things working, plus there is no guarantee that it will work.

P3nguin-M commented 2 years ago

What mode is this bluetooth device supposed to be in? When i issue MAC.2 -> "Connected for calls" is shown. MAC.4 -> "Connected (Selected) Message Access". When i try MAC.3 -> Connecting then fails and reset by peer. MAC.2 & MAC.4 both stay connected but do not output any data from socket. I am now finding out the AT commands are going through while im on one of those modes 2/4. btsnoop log pasted below btsnoop_hci.log

TheWeirdDev commented 2 years ago

What mode is this bluetooth device supposed to be in?

For Bluetooth headsets, we'll use the RFCOMM uuid to get the desired port number. Perhaps you can use the UUIDs that you have to find the port that suits your needs.

P3nguin-M commented 2 years ago

Pulled apart the bluetooth apk on this S10 i have and found the commands it will take:

        sock = RFCOMMSocket()
        sock.connect(self._bt_settings)
        print(f'Socket connected..')

        sock.send(b"AT+CSQ")
        sock.send(b"AT+CGMI")
        sock.send(b"AT+CGMM")
        sock.send(b"AT+SSVOICE=1")

        # Iterate received packets until there is no more or a result was found
        for line in sock:
            print(f'Socket Data: {line}')

output: Socket Data: b'\r\n+CSQ: 0,99\r\n' Returning data chunks.. Socket Data: b'\r\nOK\r\n' Returning data chunks.. Socket Data: b'\r\n+CGMI: samsung\r\n' Returning data chunks.. Socket Data: b'\r\nOK\r\n' Returning data chunks.. Socket Data: b'\r\n+CGMM: SM-G973U\r\n' Returning data chunks.. Socket Data: b'\r\nOK\r\n' Returning data chunks..

MadFlasheroo7 commented 2 years ago

@MadFlasheroo7 By capturing the Bluetooth connection from your phone and the headset, then analyzing the commands being sent/received. Then you can change code in order to support your device. It needs a lot of debugging and trial and error to get things working, plus there is no guarantee that it will work.

Should my android device be rooted? And kindly tell the process to log commands too

TheWeirdDev commented 2 years ago

Should my android device be rooted? And kindly tell the process to log commands too

No I don't think so. You can check this comment for more info.

asandikci commented 2 years ago

interestingly, $ sudo apt-get install bluez* solved my problem (on kali (5.15.0-kali3-amd64) and Soundcore Life Q10 bluetooth headphone)

carlocastoldi commented 2 years ago

@TheWeirdDev I don't know if it's the right place where to post this, but I experience Errno 111 as well with my BT dongle. I thus created the file log with Android: btsnoop_hci.log

If you can't fix it yourself, can you guide me on how to do it myself? Thanks in advance!

@MadFlasheroo7: Should my android device be rooted? And kindly tell the process to log commands too

in my experience you have to. On my Android 11 it got saved in /data/misc/bluetooth/logs/, not in sdcard

TheWeirdDev commented 2 years ago

@carlocastoldi Your log file seems OK and the script should work, but the error 111 happens when the socket connection wasn't successful, so there was no way to send or receive commands.

Did you try changing the port number? Try to figure out why it can't connect, then you can check if commands work.

MadFlasheroo7 commented 2 years ago

@TheWeirdDev I don't know if it's the right place where to post this, but I experience Errno 111 as well with my BT dongle. I thus created the file log with Android: btsnoop_hci.log

If you can't fix it yourself, can you guide me on how to do it myself? Thanks in advance!

@MadFlasheroo7: Should my android device be rooted? And kindly tell the process to log commands too

in my experience you have to. On my Android 11 it got saved in /data/misc/bluetooth/logs/, not in sdcard

i actually got busy with my college stuff so once i root my device ill let u know

ask-compu commented 1 year ago

i have a Soundcore Anker Life Q20 i get this error on all but a few ports (tested 1 through 30, waiting a few seconds between tries) ports 13,18: when specifying these ports the script seems to just hang port 19 (and when not specifying any port): i get bluetooth.btcommon.BluetoothError: [Errno 16] Device or resource busy

this testing was done one a brand new pair, first thing i did upon receiving them in the mail was to pair and connect them to my computer and play some music on them while i did this testing