以编程方式 enable/disable 蓝牙配置文件

Programmatically enable/disable Bluetooth profiles

我是 运行 Rasbian Jessie Lite,使用 Raspberry Pi 3,带有 USB/Bluetooth 加密狗 (blueZ) 5.4。

/etc/bluetooth/main.conf 有 Class = 0x0c0408。 我有一个启用蓝牙设备并接受任何传入配对请求的 Qt5 应用程序。

我可以从我的智能手机成功连接到所有启用的蓝牙配置文件:A2DP 和 HFP。

现在我想让用户select应该启用哪些配置文件。因此,我正在寻找一种 enable/disable 即时 A2DP 和 HFP 的方法。从 C++、bash 或 python 脚本没问题。

我不能只更改 Class 值,因为我无法重新启动蓝牙服务 - 我必须保留 运行 GATT 服务器。

想过吗?

可以使用 sdptool 命令在 Bluez 中启用和禁用任何 profile/service。如果你想启用任何 profile/service 你可以使用:

sdptool add A2SRC

以同样的方式禁用任何 service/profile 你可以使用:

sdptool del A2SRC

可以使用 sdptool

的帮助找到更多信息
    sdptool - SDP tool v5.37
Usage:
    sdptool [options] <command> [command parameters]
Options:
    -h      Display help
    -i      Specify source interface
Commands:
    search      Search for a service
    browse      Browse all available services
    records     Request all records
    add         Add local service
    del         Delete local service
    get         Get local service
    setattr     Set/Add attribute to a SDP record
    setseq      Set/Add attribute sequence to a SDP record

Services:
    DID SP DUN LAN FAX OPUSH FTP PRINT HS HSAG HF HFAG SAP PBAP MAP 
    NAP GN PANU HCRP HID KEYB WIIMOTE CIP CTP A2SRC A2SNK AVRCT AVRTG 
    UDIUE UDITE SEMCHLA SR1 SYNCML SYNCMLSERV ACTIVESYNC HOTSYNC 
    PALMOS NOKID PCSUITE NFTP NSYNCML NGAGE APPLE IAP ISYNC GATT 

现在,这是启用和禁用任何 profile/service 的方法。

转到第二个问题,如何让智能手机用户远程启用和禁用配置文件。这可以通过蓝牙中的串行端口配置文件 (SPP) 实现。简单介绍一下,SPP 是基于蓝牙的串口仿真。它基于RFcomm 协议,可以与A2DP 和HFP 并行使用。

所以这里的想法是创建从智能手机到 RSP 的 SPP 连接,然后发送命令以启用和禁用配置文件。可以使用 Bluez 提供的 rfcomm 命令从命令行使用 SPP。有关如何使用该命令的更多信息,请参见此处:

https://unix.stackexchange.com/questions/92255/how-do-i-connect-and-send-data-to-a-bluetooth-serial-port-on-linux

如果您需要进一步说明,请告诉我。