TheWeirdDev / Bluetooth_Headset_Battery_Level

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

Device or resource busy #77

Open hkpanchani opened 2 years ago

hkpanchani commented 2 years ago

Not working on ubuntu 20.04 with pipewire configuration.


$ bluetooth_battery E4:41:22:AB:AD:D1
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (16, 'Device or resource busy')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.local/bin/bluetooth_battery", line 8, in <module>
    sys.exit(main())
  File "/home/user/.local/lib/python3.8/site-packages/bluetooth_battery/bluetooth_battery.py", line 163, in main
    print("Battery level for {} is {}".format(device, str(query)))
  File "/home/user/.local/lib/python3.8/site-packages/bluetooth_battery/bluetooth_battery.py", line 93, in __str__
    return "{:.0%}".format(self._perform_query() / 100)
  File "/home/user/.local/lib/python3.8/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 16] Device or resource busy

And without pipewire I cant use mic from bluetooth headset so cant switch back.

Seif-apprentus commented 2 years ago

I have the same issue, please someone help us?

manishwg commented 2 years ago

I have the same issue, please someone help us?

thienandangthanh commented 2 years ago

I have the same issue on Arch Linux with kernel version 5.15.41-1-lts

alexandremsouza1 commented 1 year ago

I have same issue too

mirenradia commented 1 year ago

Me too (on Fedora 36 with Pipewire).

Plaque-fcc commented 1 year ago

Python 3.10.4 Ubuntu 22.04.1 File "./Bluetooth_Headset_Battery_Level/bluetooth_battery.py", line 101, in _perform_query sock.connect(self._bt_settings) File "", line 5, in connect bluetooth.btcommon.BluetoothError: [Errno 16] Device or resource busy

But it worked on Ubuntu 20.04 earlier.

TheWeirdDev commented 1 year ago

Did you try disconnecting the device before running the script?

Sorry for the late response. I'm currently super busy with work.

Plaque-fcc commented 1 year ago

Did you try disconnecting the device before running the script?

Sorry for the late response. I'm currently super busy with work.

I changed the version of setuptools as a workaround from #83. Then I installed the version of pybluez as a workaround from #78. And only then the script started to work, finally.

jpollack commented 1 year ago

For anyone else having this issue (which for me showed up after upgrading from debian bullseye to bookworm), I've found a workaround:

Before reading battery level, disconnect from the device using bluetoothctl, then reconnect after. I've wrapped this in a shell script for ease of use.

It's annoying that I can no longer read battery level during an active connection, but it's better than nothing!

TheWeirdDev commented 1 year ago

That's literally in the 'Readme' that no one reads 🤦‍♂️

meetdilip commented 1 year ago

I used the toggle button in settings to disconnect the device and connected it again. But, no matter what I do, I get this error again. :(

Even used the " bluetoothctl " command as suggested above.

Using Ubuntu 22.04. Thanks

jankratochvil commented 1 year ago

Fedora 36, after I disconnect "Jabra Evolve 65" it gets few lines of code further but still:

Traceback (most recent call last):
  File "/tmp/Bluetooth_Headset_Battery_Level/./bluetooth_battery.py", line 166, in <module>
    main()
  File "/tmp/Bluetooth_Headset_Battery_Level/./bluetooth_battery.py", line 163, in main
    print("Battery level for {} is {}".format(device, str(query)))
  File "/tmp/Bluetooth_Headset_Battery_Level/./bluetooth_battery.py", line 93, in __str__
    return "{:.0%}".format(self._perform_query() / 100)
  File "/tmp/Bluetooth_Headset_Battery_Level/./bluetooth_battery.py", line 105, in _perform_query
    sock.send(b"+BRSF: 1024")
  File "/tmp/Bluetooth_Headset_Battery_Level/./bluetooth_battery.py", line 66, in send
    return super().send(b"\r\n" + data + b"\r\n")
  File "<string>", line 5, in send
bluetooth.btcommon.BluetoothError: [Errno 14] Bad address
Garbaz commented 1 year ago

I had the same issue. It was resolved for me by following this advice, or rather the way it's done here. I.e.:

Now bluetooth_battery tool works again for me (or rather, I still have to run it twice or thrice sometimes to get an output, but at least i can somehow get the battery status of my headphones eventually).

meetdilip commented 1 year ago

I had the same issue. It was resolved for me by following this advice, or rather the way it's done here. I.e.:

* Create folder `/etc/systemd/system/bluetooth.service.d/` if not already existent

* Edit file `/etc/systemd/system/bluetooth.service.d/override.conf` to be:
[Service]
ExecStart=
ExecStart=/usr/lib/bluetooth/bluetoothd --experimental
* Load the new config with: `sudo systemctl daemon-reload`

* Restart bluetooth with: `sudo systemctl restart bluetooth`

* Restart/reconnect bluetooth headphones

Now bluetooth_battery tool works again for me (or rather, I still have to run it twice or thrice sometimes to get an output, but at least i can somehow get the battery status of my headphones eventually).

Tried this, no luck :(

MarijnS95 commented 1 year ago

@meetdilip Enabling experimental features in BlueZ allows PulseAudio 16.0 and PipeWire 0.3.34 to report battery status for the device back to it, on recent versions. It does so based on the most common AT commands similar to this script, hence you won't need / should not use this script at all anymore (and BlueZ will itself propagate battery information if a battery GATT or similar is available).

(You will need "some" utility that reads out the battery status from BlueZ and/or UPower[^1] over DBus, though. For i3blocks I've made my own: https://github.com/MarijnS95/i3blocks-contrib/blob/master/bluetooth-battery/bluetooth-battery, and other WMs may display extra battery icons for devices listed by UPower)

[^1]: For completeness: UPower picks up and propagates reported battery levels for Bluetooth devices when exposed by BlueZ over DBus


@TheWeirdDev perhaps it is time to start pointing users towards this in the README, as it effectively deprecates this script and perhaps even conflicts with it?

See the relevant changelog items in PulseAudio 16.0: https://www.freedesktop.org/wiki/Software/PulseAudio/Notes/16.0/#bluetoothdevicebatterylevelreportingadded https://www.freedesktop.org/wiki/Software/PulseAudio/Notes/16.0/#bluetoothbatterylevelreportingviabluezrequiresenablingexperimentalsfeaturesinbluez

And for PipeWire I'm unsure if it landed 0.3.34 or 0.3.24 already.

zheltikov commented 11 months ago

I had the same issue. It was resolved for me by following this advice, or rather the way it's done here. I.e.:

* Create folder `/etc/systemd/system/bluetooth.service.d/` if not already existent

* Edit file `/etc/systemd/system/bluetooth.service.d/override.conf` to be:
[Service]
ExecStart=
ExecStart=/usr/lib/bluetooth/bluetoothd --experimental
* Load the new config with: `sudo systemctl daemon-reload`

* Restart bluetooth with: `sudo systemctl restart bluetooth`

* Restart/reconnect bluetooth headphones

Now bluetooth_battery tool works again for me (or rather, I still have to run it twice or thrice sometimes to get an output, but at least i can somehow get the battery status of my headphones eventually).

Thanks a lot @Garbaz ! I've been looking for this for soo long... This not only solved the issue with this python script not being able to connect to some devices, but also added the missing bluetooth devices to the "Power" tab in GNOME Settings. Cheers!