使用 udev / bluez 检测蓝牙耳机连接

使用 udev / bluez 检测蓝牙耳机连接

我想要实现的目标:连接时将蓝牙耳机设置为pulseaudio中的默认接收器,并在断开连接时设置回笔记本电脑扬声器。

原因:我的笔记本电脑将所有四个潜在的 HDMI 输出检测为音频接收器,并经常决定将其中之一设置为默认输出。我厌倦了打开 pavucontrol 并改变它。

问题:我已经考虑过为此编写一个 udev 规则,但不幸的是,来自 udevadm 的数据并不是很有帮助。

udevadm monitor连接耳机时的输出:

~ > udevadm monitor                                                                             
monitor will print the received events for:
UDEV - the event which udev sends out after rule processing
KERNEL - the kernel uevent

KERNEL[74617.230946] add      /devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0/hci0:256 (bluetooth)
UDEV  [74617.232667] add      /devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0/hci0:256 (bluetooth)
KERNEL[74618.266963] add      /devices/virtual/input/input47 (input)
KERNEL[74618.267067] add      /devices/virtual/input/input47/event22 (input)
UDEV  [74618.269394] add      /devices/virtual/input/input47 (input)
UDEV  [74618.364725] add      /devices/virtual/input/input47/event22 (input)

udevadm info添加的设备的输出:

~ > udevadm info -ap /devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0/hci0:256

...

  looking at device '/devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0/hci0:256':
    KERNEL=="hci0:256"
    SUBSYSTEM=="bluetooth"
    DRIVER==""
    ATTR{power/control}=="auto"
    ATTR{power/runtime_active_time}=="0"
    ATTR{power/runtime_status}=="unsupported"
    ATTR{power/runtime_suspended_time}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0':
    KERNELS=="hci0"
    SUBSYSTEMS=="bluetooth"
    DRIVERS==""
    ATTRS{power/control}=="auto"
    ATTRS{power/runtime_active_time}=="0"
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0/hci0:256':
    KERNEL=="hci0:256"
    SUBSYSTEM=="bluetooth"
    DRIVER==""
    ATTR{power/control}=="auto"
    ATTR{power/runtime_active_time}=="0"
    ATTR{power/runtime_status}=="unsupported"
    ATTR{power/runtime_suspended_time}=="0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb3/3-10/3-10:1.0/bluetooth/hci0':
    KERNELS=="hci0"
    SUBSYSTEMS=="bluetooth"
    DRIVERS==""
    ATTRS{power/control}=="auto"
    ATTRS{power/runtime_active_time}=="0"
    ATTRS{power/runtime_status}=="unsupported"
    ATTRS{power/runtime_suspended_time}=="0"
~ > udevadm info -ap /devices/virtual/input/input47

...

  looking at device '/devices/virtual/input/input47':
    KERNEL=="input47"
    SUBSYSTEM=="input"
    DRIVER==""
    ATTR{capabilities/abs}=="0"
    ATTR{capabilities/ev}=="100007"
    ATTR{capabilities/ff}=="0"
    ATTR{capabilities/key}=="2fc800 145200000000 0 10300 49e800000c00 e16800000000f f810000010000ffc"
    ATTR{capabilities/led}=="0"
    ATTR{capabilities/msc}=="0"
    ATTR{capabilities/rel}=="0"
    ATTR{capabilities/snd}=="0"
    ATTR{capabilities/sw}=="0"
    ATTR{id/bustype}=="0005"
    ATTR{id/product}=="4020"
    ATTR{id/vendor}=="009e"
    ATTR{id/version}=="0481"
    ATTR{inhibited}=="0"
    ATTR{name}=="gtf qc35ii (AVRCP)"
    ATTR{phys}=="3c:e9:f7:f2:f6:10"
    ATTR{power/control}=="auto"
    ATTR{power/runtime_active_time}=="0"
~ > udevadm info -ap /devices/virtual/input/input47/event22

...

  looking at device '/devices/virtual/input/input47/event22':
    KERNEL=="event22"
    SUBSYSTEM=="input"
    DRIVER==""
    ATTR{power/control}=="auto"
    ATTR{power/runtime_active_time}=="0"
    ATTR{power/runtime_status}=="unsupported"
    ATTR{power/runtime_suspended_time}=="0"

  looking at parent device '/devices/virtual/input/input47':
    KERNELS=="input47"
    SUBSYSTEMS=="input"
    DRIVERS==""
    ATTRS{capabilities/abs}=="0"
    ATTRS{capabilities/ev}=="100007"
    ATTRS{capabilities/ff}=="0"
    ATTRS{capabilities/key}=="2fc800 145200000000 0 10300 49e800000c00 e16800000000f f810000010000ffc"
    ATTRS{capabilities/led}=="0"
    ATTRS{capabilities/msc}=="0"
    ATTRS{capabilities/rel}=="0"
    ATTRS{capabilities/snd}=="0"
    ATTRS{capabilities/sw}=="0"

据我所知,这里没有太多东西可以让我检测到正在连接的蓝牙耳机,即使它input47是一个“输入”而不是音频接收器(我假设它是一个麦克风,但我可能是错的)。也许有一种方法可以监听bluez事件(或pulseaudio事件),但我还没有找到。

我可以用 udev 或 bluez 编写这种事件处理吗?如果是这样,怎么办?

相关内容