node.js - Raspberry BLE 加密/配对

标签 node.js raspberry-pi bluetooth-lowenergy

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

我首先使用cordova BLE central(https://github.com/don/cordova-plugin-ble-central)在iOS上尝试使用Ionic,它按预期工作,这是交换的数据包:

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

我注意到在 iO 上我有一个加密过程,它看起来像是在连接时自动触发的:
a May 21 08:09:48.706  HCI Command      0x0041  315X               LE Start Encryption - Connection Handle: 0x0041

而在 Pi 上,我只有一个创建连接:
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 命令被激活,我将深入研究以找出

最佳答案

这看起来正在发生,因为 Raspberry Pi 不支持配对。您可以通过 btmgmt 和 bluetoothctl 命令的组合解决此问题:-

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

此外,根据您的日志,您已将 IO 功能设置为 0x03(NoInputNoOutput)。您可以通过使用以下命令启动 bluetoothctl 来更改此设置:-
bluetoothctl --agent KeyboardDisplay

之后,系统会提示您输入密码或接受 Raspberry Pi 上的传入连接。您可以使用所有不同的 IO 功能,看看哪一种最适合您。执行此操作的命令是:-
bluetoothctl --agent DisplayOnly
bluetoothctl --agent DisplayYesNo
bluetoothctl --agent KeyboardOnly
bluetoothctl --agent NoInputNoOutput
bluetoothctl --agent KeyboardDisplay

可以在下面的链接中找到更多详细信息
  • BLE Pairing the Raspberry Pi 3 Model B
  • Configuring Bluetooth devices with bluetoothctl
  • How to Encrypt Data Using BLE Connection Using BlueZ

  • 我希望这有帮助。

    关于node.js - Raspberry BLE 加密/配对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62147384/

    相关文章:

    javascript - 如何在 TypeScript 中将箭头函数返回值分配给 string[]

    linux - Bluez 自动重新连接设备

    android - 将 BLE 设备连接到 Android 并发送数据 (nUART)

    c - bluez 中 GATT 服务的回调方法是什么样的?

    node.js - 预检响应中的 Access-Control-Allow-Headers 不允许请求 header 字段内容类型

    node.js - collection.findOne({query}) 不应该返回文档本身吗?

    javascript - 尝试编写 Google Cloud Function 以使用 node.js 写入 Google Sheet

    c++ - (C++) 使用天气 API

    python - Python OpenCV将图像转换为字节字符串

    python - 从网络服务器连接到树莓派