BlueZ LE 远程使用功能

BlueZ LE Remote Used Features

我正在使用 BlueZ 处理 BLE 设备。我已经从源代码编译它,并围绕它写了一个包装器。

我正在搜索这一点点信息:

我的问题是我的解决方案已经适用于某些设备(可以连接到它们),但是对于特定设备,很多时候由于超时连接失败。

当连接失败时,我用 btmon 创建了一个嗅探:

# btmon
Bluetooth monitor ver 5.50
= Note: Linux version 4.19.97-v7l+ (armv7l)                            0.742019
= Note: Bluetooth subsystem version 2.22                               0.742027
= New Index: AA:BB:CC:DD:EE:FF (Primary,UART,hci0)              [hci0] 0.742030
= Open Index: AA:BB:CC:DD:EE:FF                                 [hci0] 0.742033
= Index Info: AA:BB:CC:D.. (Cypress Semiconductor Corporation)  [hci0] 0.742035
@ MGMT Open: bluetoothd (privileged) version 1.14             {0x0001} 0.742038
@ MGMT Open: btmon (privileged) version 1.14                  {0x0002} 0.742321
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7   #1 [hci0] 4.737267
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Ignore not in white list (0x01)
> HCI Event: Command Complete (0x0e) plen 4                  #2 [hci0] 4.737714
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2       #3 [hci0] 4.737767
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                  #4 [hci0] 4.738160
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 42                    #5 [hci0] 6.099681
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: FF:EE:DD:CC:BB:AA
        Data length: 30
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Company: Apple, Inc. (76)
          Type: iBeacon (2)
          UUID: 669a0c20-0008-6c91-e411-015500e22ea9
          Version: 48661.62728
          TX power: -59 dB
        RSSI: -78 dBm (0xb2)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2       #6 [hci0] 6.099747
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4                  #7 [hci0] 6.101862
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Create Connection (0x08|0x000d) plen 25    #8 [hci0] 6.101916
        Scan interval: 60.000 msec (0x0060)
        Scan window: 60.000 msec (0x0060)
        Filter policy: White list is not used (0x00)
        Peer address type: Public (0x00)
        Peer address: FF:EE:DD:CC:BB:AA
        Own address type: Public (0x00)
        Min connection interval: 30.00 msec (0x0018)
        Max connection interval: 50.00 msec (0x0028)
        Connection latency: 0 (0x0000)
        Supervision timeout: 420 msec (0x002a)
        Min connection length: 0.000 msec (0x0000)
        Max connection length: 0.000 msec (0x0000)
> HCI Event: Command Status (0x0f) plen 4                    #9 [hci0] 6.102446
      LE Create Connection (0x08|0x000d) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 19                   #10 [hci0] 7.476997
      LE Connection Complete (0x01)
        Status: Success (0x00)
        Handle: 64
        Role: Master (0x00)
        Peer address type: Public (0x00)
        Peer address: FF:EE:DD:CC:BB:AA
        Connection interval: 48.75 msec (0x0027)
        Connection latency: 0 (0x0000)
        Supervision timeout: 420 msec (0x002a)
        Master clock accuracy: 0x00
@ MGMT Event: Device Connected (0x000b) plen 43        {0x0002} [hci0] 7.477047
        LE Address: FF:EE:DD:CC:BB:AA
        Flags: 0x00000000
        Data length: 30
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Company: Apple, Inc. (76)
          Type: iBeacon (2)
          UUID: 669a0c20-0008-6c91-e411-015500e22ea9
          Version: 48661.62728
          TX power: -59 dB
@ MGMT Event: Device Connected (0x000b) plen 43        {0x0001} [hci0] 7.477047
        LE Address: FF:EE:DD:CC:BB:AA
        Flags: 0x00000000
        Data length: 30
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        Company: Apple, Inc. (76)
          Type: iBeacon (2)
          UUID: UUID
          Version: 48661.62728
          TX power: -59 dB
< HCI Command: LE Read Remote Used... (0x08|0x0016) plen 2  #11 [hci0] 7.477210
        Handle: 64
> HCI Event: Command Status (0x0f) plen 4                   #12 [hci0] 7.479342
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
> HCI Event: Command Complete (0x0e) plen 14                #13 [hci0] 7.479357
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
        00 00 00 00 00 00 00 00 00 00                    ..........      
> HCI Event: LE Meta Event (0x3e) plen 12                   #14 [hci0] 7.993969
      LE Read Remote Used Features (0x04)
        Status: Connection Timeout (0x08)
        Handle: 64
        Features: 0x2d 0x00 0x00 0x00 0x00 0x00 0x00 0x00
          LE Encryption
          Extended Reject Indication
          Slave-initiated Features Exchange
          LE Data Packet Length Extension
> HCI Event: Disconnect Complete (0x05) plen 4              #15 [hci0] 7.994591
        Status: Success (0x00)
        Handle: 64
        Reason: Connection Timeout (0x08)
@ MGMT Event: Device Disconnected (0x000c) plen 8      {0x0002} [hci0] 8.027693
        LE Address: FF:EE:DD:CC:BB:AA
        Reason: Connection timeout (0x01)
@ MGMT Event: Device Disconnected (0x000c) plen 8      {0x0001} [hci0] 8.027693
        LE Address: FF:EE:DD:CC:BB:AA
        Reason: Connection timeout (0x01)

连接首先成功,但随后我的设备执行 "LE Read Remote Used Features" HCI 命令,该命令在 500 毫秒后超时导致整个连接失败。

这就是我寻找上述问题答案的原因。

您所有问题的答案都可以在 Link 层章节的蓝牙核心规范中找到。

发生的情况是与远程设备的连接断开。信号质量差?天线坏了?时钟精度差? 如果在此时间内没有收到数据包(可能是空的),则连接超时发生在指定的监管超时之后。

现在碰巧 BlueZ 发送的第一件事是远程功能请求。如果改为发送任何其他数据包,您可能会得到相同的结果(连接超时)。

改用 BLE link 层嗅探器,看看到底发生了什么。

我遇到了类似的问题,这是我的发现:

What is "LE Read Remote Used Features" command for?

正如@Emil 指出的那样,这个命令在 BT 规范中,直到 5.0。所以在 BT 规范 4.2 中它说:

This command requests a list of the used LE features from the remote device. This command shall return a list of the used LE features

在期望 Remote 正确执行操作之前,您需要先了解 Remote 的功能?

Which features can it read?

将超时从 500 毫秒增加到上限 32000 毫秒后,结果是 如下:

        Handle: 128
 HCI Event: Command Status (0x0f) plen 4      
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
 HCI Event: LE Meta Event (0x3e) plen 11                                                                                                                                                                               
      LE Data Length Change (0x07)
        Handle: 128
        Max TX octets: 251
        Max TX time: 17040
        Max RX octets: 251
        Max RX time: 17040
 HCI Event: LE Meta Event (0x3e) plen 12                                                                                                                                                                               
      LE Read Remote Used Features (0x04)
        Status: Success (0x00)
        Handle: 128
        Features: 0x7f 0xfb 0x46 0x07 0x00 0x00 0x00 0x00
          LE Encryption
          Connection Parameter Request Procedure
          Extended Reject Indication
          Slave-initiated Features Exchange
          LE Ping
          LE Data Packet Length Extension
          LL Privacy
          LE 2M PHY
          Stable Modulation Index - Transmitter
          LE Coded PHY
          LE Extended Advertising
          LE Periodic Advertising
          Channel Selection Algorithm #2
          LE Power Class 1
          Unknown features (0x0000000007460000)

Is it safe to disable querying it after connection?

在我的例子中,罪魁祸首不是这个破坏连接的命令。我建议增加超时时间,看看会发生什么。

Is it possible to increase the timeout for the reception of this command's response?

如果您要单独发出此命令并且它包含这样的参数,那么是的,为什么不呢?在我的例子中,它使用了我提供给 (Extended)Create Connection 命令的任何监督超时。