Zend certified PHP/Magento developer

systemd/udev/bluez: Removing other bluetooth audio devices when connecting

My laptop’s bluetooth adapter doesn’t seem to be very powerful, and when I have more than one audio device connected at the same time, the audio gets choppy for some reason.

So I would like to write a service that disconnects all other bluetooth audio devices when a new one gets connected. The main issue I run into is that I don’t know how to catch the device add event in a way that lets me subsequently identify which device got added, so I can disconnect the rest.

What I’ve been trying is udev. I already have a udev rule to catch all bluetooth add events, which looks like this:

ACTION=="add", SUBSYSTEM=="bluetooth", TAG+="systemd", ENV{SYSTEMD_USER_WANTS}="bluetooth-add-event.service"

So after some digging I figured that I could write the device info to a temporary file and then reference it in the script started by the service, so I added this for testing:

RUN+="/bin/sh -c 'echo %k_%b_$driver_%E{HID_ID} > /run/btadd-id'"

The output was quite unhelpful, though:

hci0:256___

I don’t know any way to actually use this to identify a device known by bluetoothctl, which tells me this about the device in question:

~$ bluetoothctl info 25:4E:4A:C7:43:C4
Device 25:4E:4A:C7:43:C4 (public)
        Name: JR-CB1
        Alias: JR-CB1
        Class: 0x00240404
        Icon: audio-headset
        Paired: yes
        Bonded: yes
        Trusted: yes
        Blocked: no
        Connected: yes
        LegacyPairing: no
        UUID: Serial Port               (00001101-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)
        UUID: Vendor specific           (fe010000-1234-5678-abcd-00805f9b34fb)
        Modalias: bluetooth:v05D6p000Ad0240

udev on the other hand gives me this:

~$ udevadm info --path /devices/pci0000:00/0000:00:14.0/usb2/2-7/2-7:1.0/bluetooth/hci0/hci0:256
P: /devices/pci0000:00/0000:00:14.0/usb2/2-7/2-7:1.0/bluetooth/hci0/hci0:256
M: hci0:256
R: 256
U: bluetooth
T: link
E: DEVPATH=/devices/pci0000:00/0000:00:14.0/usb2/2-7/2-7:1.0/bluetooth/hci0/hci0:256
E: SUBSYSTEM=bluetooth
E: DEVTYPE=link
E: USEC_INITIALIZED=34817721040
E: SYSTEMD_USER_WANTS=bluetooth-add-event.service bluetooth.target
E: SYSTEMD_ALIAS=/sys/subsystem/bluetooth/devices/hci0:256
E: SYSTEMD_WANTS=bluetooth.target
E: TAGS=:systemd:
E: CURRENT_TAGS=:systemd:

None of these properties correspond.

I don’t know what else to try, honestly. Is there perhaps a feasible way to skip udev and handle add events directly through bluez?