树莓派BLE加密/配对

4

我正在尝试使用我的Raspberry Pi 3B与一个不需要PIN码的BLE设备进行通信。

我首先在iOS上使用cordova BLE central(https://github.com/don/cordova-plugin-ble-central)尝试了一下,结果是如预期一样的。这里是交换的数据包:

iOS(PacketLogger)

May 21 07:59:36.825  Note             0x0000                     Product: iPhone9,3  
May 21 07:59:36.825  Note             0x0000                     Software: Version 13.4 (Build 17E255)  
May 21 08:09:42.178  Config           0x0041  315X               Connected: C4:64:E3:9B:04:44 - Handle: 0x41 - 315X - C7D0F30E-78B4-C82C-6912-DD0F5A5BCA2E  
May 21 08:09:43.690  HCI Command      0x0041  00:00:00:00:00:00  Disconnect - Connection Handle: 0x0041 - Reason: Remote User Terminated Connection  
May 21 08:09:43.690  Power            0x0000                     H4 wake  
May 21 08:09:43.690  HCI Event        0x0000                     Command Status - Disconnect  
May 21 08:09:43.705  LMP Sent         0x0041  00:00:00:00:00:00  LE Terminate Indicator - Error: 0x13  
May 21 08:09:43.706  HCI Event        0x0041  00:00:00:00:00:00  Disconnection Complete - Success  
May 21 08:09:43.710  Config           0x0000                     (  
May 21 08:09:43.710  Config           0x0000                     Invalid packet  
May 21 08:09:43.710  Config           0x0000                     (  
May 21 08:09:43.710  Config           0x0000                     Invalid packet  
May 21 08:09:43.710  Config           0x0000                     Invalid packet  
May 21 08:09:43.710  Config           0x0000                     Invalid packet  
May 21 08:09:43.891  Power            0x0000                     H4 sleep  
May 21 08:09:47.801  HCI Command      0x0000                     LE Set Scan Enable - 0x00, Filter Duplicates: 1  
May 21 08:09:47.801  Power            0x0000                     H4 wake  
May 21 08:09:47.803  HCI Event        0x0000                     Command Complete [200C] - LE Set Scan Enable  
May 21 08:09:47.803  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:47.804  HCI Event        0x0000                     Command Complete [FCE9] -   
May 21 08:09:47.804  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:47.804  HCI Event        0x0000                     Command Complete [FCE9] -   
May 21 08:09:47.804  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:47.804  HCI Event        0x0000                     Command Complete [FCE9] -   
May 21 08:09:47.804  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:47.805  HCI Event        0x0000                     Command Complete [FCE9] -   
May 21 08:09:47.805  HCI Command      0x0000                     LE Set Scan Parameters - Active - 30/40 (ms)  
May 21 08:09:47.805  HCI Event        0x0000                     Command Complete [200B] - LE Set Scan Parameters  
May 21 08:09:47.805  HCI Command      0x0000                     LE Set Scan Enable - 0x01, Filter Duplicates: 1  
May 21 08:09:47.805  HCI Event        0x0000                     Command Complete [200C] - LE Set Scan Enable  
May 21 08:09:47.808  HCI Event        0x0000  315X               LE - Advertising Report - 1 Report - Normal - Public - C4:64:E3:9B:04:44  -59 dBm - Channel 37  
May 21 08:09:47.809  HCI Event        0x0000  315X               LE - Advertising Report - 1 Report - Normal - Public - C4:64:E3:9B:04:44  -60 dBm - Manufacturer Specific Data - 315X - Channel 37  
May 21 08:09:48.006  Power            0x0000                     H4 sleep  
May 21 08:09:48.636  HCI Command      0x0000  315X               LE Add Device To White List - Public - C4:64:E3:9B:04:44  
May 21 08:09:48.636  Power            0x0000                     H4 wake  
May 21 08:09:48.636  HCI Event        0x0000                     Command Complete [2011] - LE Add Device To White List  
May 21 08:09:48.643  HCI Command      0x0000                     LE Create Connection - 00:00:00:00:00:00, Scan Window/Interval: 30ms/30ms, Min/Max Conn Interval: 10ms/30ms   
May 21 08:09:48.647  HCI Event        0x0000                     Command Status - LE Create Connection  
May 21 08:09:48.701  HCI Event        0x0000  315X               LE - Advertising Report - 1 Report - Normal - Public - C4:64:E3:9B:04:44  -57 dBm - Channel 38/37  
May 21 08:09:48.702  HCI Event        0x0041  315X               LE - Enhanced Connection Complete - Master - Public - C4:64:E3:9B:04:44 local - 00:00:00:00:00:00 peer - 00:00:00:00:00:00 - Conn Interval: 30 ms  
May 21 08:09:48.704  HCI Command      0x0000  315X               LE Remove Device From White List - 0 - C4:64:E3:9B:04:44  
May 21 08:09:48.704  HCI Event        0x0000                     Command Complete [2012] - LE Remove Device From White List  
May 21 08:09:48.704  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:48.704  HCI Event        0x0000                     Command Complete [FDC1] -   
May 21 08:09:48.706  HCI Command      0x0041  315X               LE Start Encryption - Connection Handle: 0x0041  
May 21 08:09:48.706  HCI Event        0x0000                     Command Status - LE Start Encryption  
May 21 08:09:48.712  LMP Sent         0x0041  315X               LE Version Indicator  
May 21 08:09:48.741  LMP Received     0x0041  315X               LE Version Indicator  
May 21 08:09:48.772  LMP Sent         0x0041  315X               LE Encryption Request  
May 21 08:09:48.801  LMP Received     0x0041  315X               LE Encryption Response  
May 21 08:09:48.861  LMP Received     0x0041  315X               LE Start Encryption Request  
May 21 08:09:48.891  LMP Sent         0x0041  315X               LE Start Encryption Response  
May 21 08:09:48.922  HCI Event        0x0041  315X               Encryption Change Complete - Encryption Enabled  
May 21 08:09:48.922  HCI Command      0x0041  315X               Read Remote Version Information - Connection Handle: 0x0041  
May 21 08:09:48.922  LMP Received     0x0041  315X               LE Start Encryption Response  
May 21 08:09:48.922  HCI Event        0x0000                     Command Status - Read Remote Version Information  
May 21 08:09:48.922  HCI Event        0x0041  315X               Read Remote Version Information Complete  
May 21 08:09:48.922  HCI Command      0x0041  315X               LE Read Remote Used Features - Connection Handle: 0x0041  
May 21 08:09:48.922  HCI Event        0x0000                     Command Status - LE Read Remote Used Features  
May 21 08:09:48.923  HCI Command      0x0000                     Vendor Specific Command  
May 21 08:09:48.923  HCI Event        0x0000                     Command Complete [FD82] -   
May 21 08:09:48.952  LMP Sent         0x0041  315X               LE Feature Request - Feature Set: 0x00000000000000FD  
May 21 08:09:48.982  HCI Event        0x0041  315X               LE - Read Remote Used Features Complete - DPLE Supported  
May 21 08:09:48.982  LMP Received     0x0041  315X               LE Feature Response - Feature Set: 0x00000000000000FD  
May 21 08:09:49.012  LMP Sent         0x0041  315X               LE Length Request  
May 21 08:09:49.042  HCI Event        0x0041  315X               LE - Data Length Change  
May 21 08:09:49.042  LMP Received     0x0041  315X               LE Length Response  
May 21 08:09:49.044  ATT Send         0x0041  315X               Exchange MTU Request - MTU: 185  
May 21 08:09:49.101  ATT Receive      0x0041  315X               Exchange MTU Response - MTU: 65  
May 21 08:09:49.102  Config           0x0000                     A(  
May 21 08:09:49.102  Config           0x0000                     Invalid packet  
May 21 08:09:49.102  Config           0x0000                     A(  
May 21 08:09:49.102  Config           0x0000                     Invalid packet  
May 21 08:09:49.102  Config           0x0000                     Invalid packet  
May 21 08:09:49.102  Config           0x0000                     Invalid packet  
May 21 08:09:49.102  ATT Send         0x0041  315X               Read By Type Request - Start Handle: 0x0008 - End Handle: 0x0008 - UUID: GATT Characteristic Declaration  
May 21 08:09:49.130  HCI Event        0x0041  315X               Number Of Completed Packets - Handle: 0x0041 - Packets: 0x0002    
May 21 08:09:49.161  ATT Receive      0x0041  315X               Error Response - Attribute Handle: 0x0008 - Error Code: Attribute Not Found (0x0A)  
May 21 08:09:49.162  Config           0x0000                     Invalid packet  
May 21 08:09:49.162  Config           0x0000                     Invalid packet  
May 21 08:09:49.162  Config           0x0000                     Invalid packet  
May 21 08:09:49.162  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.165  Config           0x0000                     Invalid packet  
May 21 08:09:49.169  ATT Send         0x0041  315X               Read Request - Handle:0x0012  
May 21 08:09:49.222  ATT Receive      0x0041  315X               Read Response - Value: 4175 746F 6D6F 7765 7200 0000 0000 0000 …  
May 21 08:09:49.224  ATT Send         0x0041  315X               Write Request - Handle:0x000F - Value: 0100   
May 21 08:09:49.250  HCI Event        0x0041  315X               Number Of Completed Packets - Handle: 0x0041 - Packets: 0x0002    
May 21 08:09:49.281  ATT Receive      0x0041  315X               Write Response  

对于树莓派,我使用了noble (https://github.com/abandonware/noble),我能够发现设备并读取特征,但似乎写入命令的效果不如预期。

hcidump

pi@raspberrypi:~ $ sudo hcidump -t -x
HCI sniffer - Bluetooth packet analyzer ver 5.50
device: hci0 snap_len: 1500 filter: 0xffffffff
2020-06-02 08:49:48.643635 < HCI Command: Set Event Mask (0x03|0x0001) plen 8
    Mask: 0xfffffbff07f8bf3d
2020-06-02 08:49:48.644192 > HCI Event: Command Complete (0x0e) plen 4
    Set Event Mask (0x03|0x0001) ncmd 1
    status 0x00
2020-06-02 08:49:48.644641 < HCI Command: LE Set Event Mask (0x08|0x0001) plen 8
    mask 0x1f00000000000000 (Reserved)
2020-06-02 08:49:48.645116 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Event Mask (0x08|0x0001) ncmd 1
    status 0x00
2020-06-02 08:49:48.645147 < HCI Command: Read Local Version Information (0x04|0x0001) plen 0
2020-06-02 08:49:48.645641 > HCI Event: Command Complete (0x0e) plen 12
    Read Local Version Information (0x04|0x0001) ncmd 1
    status 0x00
    HCI Version: 4.1 (0x7) HCI Revision: 0x1ed
    LMP Version: 4.1 (0x7) LMP Subversion: 0x2209
    Manufacturer: Broadcom Corporation (15)
2020-06-02 08:49:48.645672 < HCI Command: Write LE Host Supported (0x03|0x006d) plen 2
  01 00 
2020-06-02 08:49:48.646081 > HCI Event: Command Complete (0x0e) plen 4
    Write LE Host Supported (0x03|0x006d) ncmd 1
    00 
2020-06-02 08:49:48.646264 < HCI Command: Read LE Host Supported (0x03|0x006c) plen 0
2020-06-02 08:49:48.646731 > HCI Event: Command Complete (0x0e) plen 6
    Read LE Host Supported (0x03|0x006c) ncmd 1
    00 01 00 
2020-06-02 08:49:48.646767 < HCI Command: Read BD ADDR (0x04|0x0009) plen 0
2020-06-02 08:49:48.647206 > HCI Event: Command Complete (0x0e) plen 10
    Read BD ADDR (0x04|0x0009) ncmd 1
    status 0x00 bdaddr B8:27:EB:08:5F:F0
2020-06-02 08:49:48.654744 < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
    value 0x00 (scanning disabled)
    filter duplicates 0x01 (enabled)
2020-06-02 08:49:48.655164 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x0c
    Error: Command Disallowed
2020-06-02 08:49:48.655465 < HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7
    type 0x01 (active)
    interval 10.000ms window 10.000ms
    own address: 0x00 (Public) policy: All
2020-06-02 08:49:48.655928 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
2020-06-02 08:49:48.669824 < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
    value 0x00 (scanning disabled)
    filter duplicates 0x01 (enabled)
2020-06-02 08:49:48.670225 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x0c
    Error: Command Disallowed
2020-06-02 08:49:48.670261 < HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7
    type 0x01 (active)
    interval 10.000ms window 10.000ms
    own address: 0x00 (Public) policy: All
2020-06-02 08:49:48.670716 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Parameters (0x08|0x000b) ncmd 1
    status 0x00
2020-06-02 08:49:48.670754 < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
    value 0x01 (scanning enabled)
    filter duplicates 0x01 (enabled)
2020-06-02 08:49:48.671215 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x00
2020-06-02 08:49:48.842103 > HCI Event: LE Meta Event (0x3e) plen 27
    LE Advertising Report
      ADV_IND - Connectable undirected advertising (0)
      bdaddr 9C:20:7B:BE:14:6E (Public)
      Flags: 0x1a
      Unknown type 0xff with 10 bytes data
      RSSI: -68
2020-06-02 08:49:48.842525 > HCI Event: LE Meta Event (0x3e) plen 12
    LE Advertising Report
      SCAN_RSP - Scan Response (4)
      bdaddr 9C:20:7B:BE:14:6E (Public)
      RSSI: -68
2020-06-02 08:49:49.384917 > HCI Event: LE Meta Event (0x3e) plen 33
    LE Advertising Report
      ADV_IND - Connectable undirected advertising (0)
      bdaddr C4:64:E3:9B:04:44 (Public)
      Flags: 0x06
      Unknown type 0x06 with 16 bytes data
      RSSI: -95
2020-06-02 08:49:49.385781 > HCI Event: LE Meta Event (0x3e) plen 28
    LE Advertising Report
      SCAN_RSP - Scan Response (4)
      bdaddr C4:64:E3:9B:04:44 (Public)
      Unknown type 0xff with 8 bytes data
      Shortened local name: '315X'
      RSSI: -95
2020-06-02 08:49:49.392478 < HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2
    value 0x00 (scanning disabled)
    filter duplicates 0x01 (enabled)
2020-06-02 08:49:49.394279 > HCI Event: Command Complete (0x0e) plen 4
    LE Set Scan Enable (0x08|0x000c) ncmd 1
    status 0x00
2020-06-02 08:49:49.399822 < HCI Command: LE Create Connection (0x08|0x000d) plen 25
    bdaddr C4:64:E3:9B:04:44 type 0
    interval 96 window 48 initiator_filter 0
    own_bdaddr_type 0 min_interval 6 max_interval 12
    latency 0 supervision_to 200 min_ce 4 max_ce 6
2020-06-02 08:49:49.400717 > HCI Event: Command Status (0x0f) plen 4
    LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
2020-06-02 08:49:59.914436 > HCI Event: LE Meta Event (0x3e) plen 19
    LE Connection Complete
      status 0x00 handle 64, role master
      bdaddr C4:64:E3:9B:04:44 (Public)
2020-06-02 08:49:59.914783 < HCI Command: LE Read Remote Used Features (0x08|0x0016) plen 2
  40 00 
2020-06-02 08:49:59.915700 > HCI Event: Command Status (0x0f) plen 4
    LE Read Remote Used Features (0x08|0x0016) status 0x00 ncmd 1
2020-06-02 08:50:00.205123 > HCI Event: LE Meta Event (0x3e) plen 12
    LE Read Remote Used Features Complete
      status 0x00 handle 64
      Features: 0x1d 0x00 0x00 0x00 0x00 0x00 0x00 0x00
2020-06-02 08:50:00.211514 < ACL data: handle 64 flags 0x00 dlen 7
    ATT: MTU req (0x02)
      client rx mtu 256
2020-06-02 08:50:00.249993 > ACL data: handle 64 flags 0x02 dlen 7
    ATT: MTU resp (0x03)
      server rx mtu 65
2020-06-02 08:50:00.252698 < ACL data: handle 64 flags 0x00 dlen 11
    ATT: Read By Group req (0x10)
      start 0x0001, end 0xffff
      type-uuid 0x2800
2020-06-02 08:50:00.295009 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 2
2020-06-02 08:50:00.385200 > ACL data: handle 64 flags 0x02 dlen 18
    ATT: Read By Group resp (0x11)
      attr handle 0x0001, end group handle 0x0007
      value 0x00 0x18
      attr handle 0x0008, end group handle 0x0008
      value 0x01 0x18
2020-06-02 08:50:00.386993 < ACL data: handle 64 flags 0x00 dlen 11
    ATT: Read By Group req (0x10)
      start 0x0009, end 0xffff
      type-uuid 0x2800
2020-06-02 08:50:00.460829 > ACL data: handle 64 flags 0x02 dlen 26
    ATT: Read By Group resp (0x11)
      attr handle 0x0009, end group handle 0xffff
      value 0xe4 0x6d 0xdc 0x75 0xbf 0xdd 0xe5 0x84 0x1a 0x42 0x0e 0x0b 0x01 0x00 0xbd 0x98
2020-06-02 08:50:00.464756 < ACL data: handle 64 flags 0x00 dlen 11
    ATT: Read By Type req (0x08)
      start 0x0009, end 0xffff
      type-uuid 0x2803
2020-06-02 08:50:00.490056 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 2
2020-06-02 08:50:00.520226 > ACL data: handle 64 flags 0x02 dlen 27
2020-06-02 08:50:00.535226 > ACL data: handle 64 flags 0x01 dlen 27
2020-06-02 08:50:00.580143 > ACL data: handle 64 flags 0x01 dlen 15
    ATT: Read By Type resp (0x09)
      length: 21
        handle 0x000a, value 0x04 0x0b 0x00 0xe4 0x6d 0xdc 0x75 0xbf 0xdd 0xe5 0x84 0x1a 0x42 0x0e 0x0b 0x02 0x00 0xbd 0x98 
        handle 0x000d, value 0x12 0x0e 0x00 0xe4 0x6d 0xdc 0x75 0xbf 0xdd 0xe5 0x84 0x1a 0x42 0x0e 0x0b 0x03 0x00 0xbd 0x98 
        handle 0x0011, value 0x02 0x12 0x00 0xe4 0x6d 0xdc 0x75 0xbf 0xdd 0xe5 0x84 0x1a 0x42 0x0e 0x0b 0x04 0x00 0xbd 0x98 
2020-06-02 08:50:00.582577 < ACL data: handle 64 flags 0x00 dlen 11
    ATT: Read By Type req (0x08)
      start 0x0013, end 0xffff
      type-uuid 0x2803
2020-06-02 08:50:00.730061 > ACL data: handle 64 flags 0x02 dlen 9
    ATT: Error (0x01)
      Error: Attribute not found (10)
      Read By Type req (0x08) on handle 0x0013
2020-06-02 08:50:00.737443 < ACL data: handle 64 flags 0x00 dlen 7
    ATT: Read req (0x0a)
      handle 0x0012
2020-06-02 08:50:00.759998 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 2
2020-06-02 08:50:00.760889 > ACL data: handle 64 flags 0x02 dlen 25
    ATT: Read resp (0x0b)
      41 75 74 6F 6D 6F 77 65 72 00 00 00 00 00 00 00 00 00 00 00 
2020-06-02 08:50:00.823478 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x60 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x2e 0x01 0x49 0x01 0x1f 0x8f 0x07 0x50 0x00 0x00 0x00 0x00 0x04 0x0d
2020-06-02 08:50:00.828900 < ACL data: handle 64 flags 0x00 dlen 14
    ATT: Write cmd (0x52)
      handle 0x000b value  0x61 0x69 0x06 0xe0 0x00 0xf0 0x03
2020-06-02 08:50:00.940081 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 2
2020-06-02 08:50:01.294462 < ACL data: handle 64 flags 0x00 dlen 24
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x00 0xa0 0x00 0x91 0x1f 0x08 0xf7 0x05 0x00 0x7c 0x00 0x80 0x01 0x28 0x03
2020-06-02 08:50:01.562877 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1
2020-06-02 08:50:01.774077 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x00 0x00 0x91 0x1f 0x08 0xf7 0x05 0x01 0x52 0x00 0x0a 0x0f 0x5a 0x12 0x00 0x20 0x00 0x00 0x00 0x02 0x30 0x03
2020-06-02 08:50:01.937876 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1
2020-06-02 08:50:02.281245 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x00 0x00 0x91 0x1f 0x08 0xf7 0x05 0x01 0x52 0x00 0x0a 0x0f 0x38 0x12 0x00 0x20 0x00 0x00 0x00 0x08 0xe0 0x03
2020-06-02 08:50:02.562878 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1
2020-06-02 08:50:02.773394 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x20 0x00 0x91 0x1f 0x08 0xf7 0x05 0x01 0x28 0x00 0x0a 0x0f 0x38 0x12 0x00 0x40 0x00 0x02 0x00 0x0d 0x20 0x04
2020-06-02 08:50:02.776651 < ACL data: handle 64 flags 0x00 dlen 9
    ATT: Write cmd (0x52)
      handle 0x000b value  0xee 0x03
2020-06-02 08:50:02.876322 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 2
2020-06-02 08:50:03.284576 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x00 0x00 0x91 0x1f 0x08 0xf7 0x05 0x01 0x52 0x00 0x0a 0x0f 0x38 0x12 0x00 0x30 0x00 0x00 0x00 0x00 0x10 0x03
2020-06-02 08:50:03.437917 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1
2020-06-02 08:50:03.773593 < ACL data: handle 64 flags 0x00 dlen 32
    ATT: Write cmd (0x52)
      handle 0x000b value  0x02 0xfd 0x01 0x00 0x00 0x91 0x1f 0x08 0xf7 0x05 0x01 0x52 0x00 0x0a 0x0f 0xea 0x11 0x00 0x50 0x00 0x00 0x00 0x0e 0xc0 0x03
2020-06-02 08:50:03.937996 > HCI Event: Number of Completed Packets (0x13) plen 5
    handle 64 packets 1

我注意到在iOS上,我有一个加密进程,在连接时似乎会自动触发:

a May 21 08:09:48.706  HCI Command      0x0041  315X               LE Start Encryption - Connection Handle: 0x0041

当在树莓派上时,我只有一个创建连接的选项:

2020-06-02 08:49:49.399822 < HCI Command: LE Create Connection (0x08|0x000d) plen 25
    bdaddr C4:64:E3:9B:04:44 type 0
    interval 96 window 48 initiator_filter 0
    own_bdaddr_type 0 min_interval 6 max_interval 12
    latency 0 supervision_to 200 min_ce 4 max_ce 6

我尝试使用bluetoothctl手动配对BLE设备,但看起来配对没有成功:

pi@raspberrypi:~ $ bluetoothctl
Agent registered
[bluetooth]# connect C4:64:E3:9B:04:44
Attempting to connect to C4:64:E3:9B:04:44
[CHG] Device C4:64:E3:9B:04:44 Connected: yes
Connection successful
[NEW] Primary Service
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0008
        00001801-0000-1000-8000-00805f9b34fb
        Generic Attribute Profile
[NEW] Primary Service
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009
        98bd0001-0b0e-421a-84e5-ddbf75dc6de4
        Vendor specific
[NEW] Characteristic
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char000a
        98bd0002-0b0e-421a-84e5-ddbf75dc6de4
        Vendor specific
[NEW] Descriptor
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char000a/desc000c
        00002901-0000-1000-8000-00805f9b34fb
        Characteristic User Description
[NEW] Characteristic
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char000d
        98bd0003-0b0e-421a-84e5-ddbf75dc6de4
        Vendor specific
[NEW] Descriptor
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char000d/desc000f
        00002902-0000-1000-8000-00805f9b34fb
        Client Characteristic Configuration
[NEW] Descriptor
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char000d/desc0010
        00002901-0000-1000-8000-00805f9b34fb
        Characteristic User Description
[NEW] Characteristic
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char0011
        98bd0004-0b0e-421a-84e5-ddbf75dc6de4
        Vendor specific
[NEW] Descriptor
        /org/bluez/hci0/dev_C4_64_E3_9B_04_44/service0009/char0011/desc0013
        00002901-0000-1000-8000-00805f9b34fb
        Characteristic User Description
[CHG] Device C4:64:E3:9B:04:44 UUIDs: 00001800-0000-1000-8000-00805f9b34fb
[CHG] Device C4:64:E3:9B:04:44 UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device C4:64:E3:9B:04:44 UUIDs: 98bd0001-0b0e-421a-84e5-ddbf75dc6de4
[CHG] Device C4:64:E3:9B:04:44 ServicesResolved: yes
[315X]# pair C4:64:E3:9B:04:44
Attempting to pair with C4:64:E3:9B:04:44
Failed to pair: org.bluez.Error.AuthenticationFailed
[CHG] Device C4:64:E3:9B:04:44 ServicesResolved: no
[CHG] Device C4:64:E3:9B:04:44 Connected: no

编辑

好的,看起来当我尝试订阅通知特征值但配对失败时,加密过程被触发了:

2020-06-02 10:57:45.657381 > ACL data: handle 64 flags 0x02 dlen 9
    ATT: Error (0x01)
      Error: Insufficient authentication (5)
      Read By Type req (0x08) on handle 0x000f
2020-06-02 10:57:45.658692 < ACL data: handle 64 flags 0x00 dlen 11
    SMP: Pairing Request (0x01)
      capability 0x03 oob 0x00 auth req 0x01
      max key size 0x10 init key dist 0x00 resp key dist 0x01
      Capability: NoInputNoOutput (OOB data not present)
      Authentication: Bonding (No MITM Protection)
      Initiator Key Distribution:    
      Responder Key Distribution:  LTK  
2020-06-02 10:57:45.687326 > ACL data: handle 64 flags 0x02 dlen 6
    SMP: Pairing Failed (0x05)
      reason 0x05
      Reason Pairing Not Supported

我不知道如何解决这个配对问题。

编辑2

看起来这是一个设备问题,它允许不一致的配对,或者是通过特定的 GATT 命令启用的,我会深入了解以弄清楚。

2个回答

3

这似乎是因为树莓派不支持配对功能。您可以通过btmgmt和bluetoothctl命令的组合来解决此问题:

sudo btmgmt power off
sudo btmgmt discov on
sudo btmgmt connectable on
sudo btmgmt pairable on
sudo btmgmt power on

根据您的日志,您已将IO能力设置为0x03(无输入无输出)。 您可以通过使用以下命令启动bluetoothctl进行更改:

bluetoothctl --agent KeyboardDisplay

接下来,您应该会提示输入密码或接受树莓派的连接。您可以尝试使用所有不同的IO能力,以查看哪个最适合您。执行此操作的命令如下:

bluetoothctl --agent DisplayOnly
bluetoothctl --agent DisplayYesNo
bluetoothctl --agent KeyboardOnly
bluetoothctl --agent NoInputNoOutput
bluetoothctl --agent KeyboardDisplay

以下链接提供了更多详细信息:

希望这有所帮助。


还可以查看https://stackoverflow.com/questions/62212363/gatttool-pairing-failed-0x05/62212885?noredirect=1#comment110037601_62212885获取更多信息。 - bdo334
我认为这与BLE版本无关,因为配对是向后兼容的。如果您将一个4.2设备与一个4.0设备配对,将使用4.0配对。是的,请尝试一下并告诉我它是否适用于您。在我之前玩弄设置和IO能力后,这在过去曾经对我有用,所以我希望它也适用于您。 - Youssif Saeed
即使在使用btmgmt和bluetoothctl之后,我仍然收到SMP配对失败的错误:https://gist.github.com/buildog/319f8d4029f0ce69f76484038e7351bf - bdo334
1
我终于成功配对了!看起来这是一个设备问题,可能会不一致地允许配对或通过特定的GATT命令被激活,我会深入挖掘以找出原因。无论如何,非常感谢您的时间和努力。 - bdo334
@bdo334,你最终是怎么解决这个问题的?我现在也被这个认证问题卡住了。 - Austin
显示剩余3条评论

0

我遇到了类似的问题。我使用了Raspberry Pi 4和nRF Connect工具。 我可以绑定设备并连接它。但是,即使在绑定后,它仍无法读取加密的gatt特征。StackOverflow链接到问题。

问题的根本原因和解决方案在这个Github链接中给出。

问题:

  1. 启动Pi。
  2. 启动代理、广告和gatt服务器。
  3. 绑定设备。
  4. 连接设备。
  5. 尝试读取加密特征。失败。

根本原因:

经过多次研究,我注意到这个问题是由蓝牙芯片处于 BlueZ 启动时的状态引起的(您可以使用 hciconfig hci0 检查状态)。如果它处于“UP RUNNING”状态或“UP RUNNING PSCAN ISCAN”状态,则 BlueZ 将抱怨其中一个或多个问题:

Failed to set mode: Rejected (0x0b) Failed to set mode: Rejected (0x0b) Failed to set privacy: Rejected (0x0b)

但如果它处于“DOWN”状态,则 BlueZ 可以无问题启动。因此,在蓝牙服务启动之前,您首先需要执行 hciconfig hci0 down。但在使用 hciconfig 之前,您也需要确保 sys-subsystem-bluetooth-devices-hci0.device 服务已经启动!

解决方案1:

我最终禁用了自动启动顺序,并运行了这个脚本:

systemctl start sys-subsystem-bluetooth-devices-hci0.device; hciconfig hci0 down; systemctl start bluetooth

解决方案2:

将bthelper的运行延迟几秒钟,可以解决这个问题,而无需禁用自动引导序列并运行任何手动命令。

我在/lib/systemd/system/bthelper@.service中添加了一个ExecStartPre行,使得Service部分现在看起来像这样:

[Service]
Type=simple
ExecStartPre=/bin/sleep 2
ExecStart=/usr/bin/bthelper %I

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接