I have a Sony bluetooth headset connected to Ubuntu 20.04. They work very well, bluetooth can reconnect, I can even (mpris) control players from it.
The problem I have is that if I forget to turn the off while connected the headset will run out of battery overnight. I checked and there is nothing I can do from the headset side to automatically turn it off when there is no sound.
However the headset auto-turn off when there is no bluetooth connection. Therefore I think the only good option I have is if Ubuntu would disconnect the bluetooth after a certain period of silence.
Is there such option to auto disconnect a bluetooth audio device when there is no sound?
There is no much configuration I could find for the bluetooth device except this from the Settings window:
After some searching around I came to the following solution based on this answer, which works when running PulseAudio:
Using the command
pacmd list-sink-inputs
one can see all the audio sinks. The output looks something like this: Example output of "pacmd list-sink-inputs"Important for the solution here is
state: RUNNING
andsink: 31 <bluez_sink.[hardware address].a2dp_sink>
because with this information one can check, if audio is currently streaming via Bluetooth or not. BlueZ is the technology running Linux's Bluetooth services. A sink that is connect but no music is played would have the following statestate: CORKED
Based on this information one can now change the status of the Bluetooth adapter using the package
bluez-tools
. More specifically the commandbt-adapter --set Powered false
is used to turn of the the adapter when needed. Here is a link to all options and commans.I wrote a bash script which sporadically checks if the PC is streaming audio via Bluetooth and if not turns off the Bluetooth adapter after a given timeout duration. Please forgive my scripting, its only the second script I've ever written in bash.
Edit: You can also disconnect your device using
That way you don't have to turn off the adapter. The name is the human readable Device-Name which you can also change to whatever you like in the Bluetooth settings.