Sending pause/resume playing and next/previous track bluetooth commands

I would like my Ubuntu PC sending bluetooth audio commands (play/pause, next/previous song and turn up/down) to other device streaming music over bluetooth to it. In other words, I would like to implement my Ubuntu PC "pretending" to be kind of bluetooth headphones, which are able to change tracks, and pause and resume music.

I have the following setup illustrated with the pavucontrol screenshots below (pt2 is device connected via bluetooth): enter image description here enter image description here

In this configuration streaming works as expected (I can hear music streamed from pt2), but I didn't find any way to be able to send it any signal such as pause/resume playing or change track.

I have already found out that there are many bluetooth protocols allowing specific functions. If I understood specs correctly, what I am interested in is A2DP, which is the way devices are connected now. But under profile dropdown, I have also options HSP/HFP headset head unit, HSP/HFPheadset gateway and off.

I am going to develop my own application in Python which will allow sending such commands, so I am interested in sending them via Python API or via bash commands.

I am asking this question because I am interested in a way how I can communicate with bluetooth devices.


Solution 1:

Inspired by @kenn, I decided to go deeper into dbus and d-feet tools. Eventually I reached my goal using the following command:

dbus-send --system --print-reply --dest=org.bluez /org/bluez/hci0/dev_44_78_3E_85_9D_6F org.bluez.MediaControl1.Play

which of course triggered playing music on my mobile device connected to my PC over bluetooth.

Generically for bluetooth devices this command would look like:

dbus-send --system --print-reply --dest=org.bluez /org/bluez/hci0/dev_<mobile_bluetooth_device_mac_address_with_numbers_underscore_separated> org.bluez.MediaControl1.<command_to_send>

In order to check your devices' MAC address run bt-devices -l. It will list all known (but not necessarily connected or even discovered) devices with MAC address in parentheses.

In order to find allowed commands list, install d-feet with sudo apt install d-feet. After running it, apply search for bluez query under System Bus tab and find entry with your devices' MAC:

enter image description here

There are methods similar to stuff that uses bluetooth headphones under org.bluez.MediaControl1. But, when you browse those tree, you can find A WAY more, this is really worth your attention.

dbus-send is a command for sending signals using dbus. --system switch indicates that we want to use stuff from System Bus d-feet's tab. I haven't try it yet, but I suppose --print-reply is only for debugging purposes and isn't obligatory. --dest=org.blez refers to Name in d-feet header. /org/bluez/hci0/dev_<mobile_bluetooth_device_mac_address_with_numbers_underscore_separated> and org.bluez.MediaControl1.<command> refer to object tree paths.