无法连接到 Raspberry Pi 上的 BLE 设备

Cannot connect to BLE device on Raspberry Pi

我正在尝试连接到 Raspberry Pi 上的 BLE 设备(心率传感器,Polar H7)2。我使用此处找到的最新版本的 bluez (5.35):http://www.bluez.org/download/ 但是当我尝试使用 gatttool 进行连接时,我总是遇到 "connection refused" 错误。

我是这样做的:

pi@raspberrypi ~ $ sudo su
root@raspberrypi:/home/pi# hciconfig dev
hci0:   Type: BR/EDR  Bus: USB
        BD Address: 5C:F3:70:69:54:3D  ACL MTU: 1021:8 SCO MTU: 64:1
        DOWN
        RX bytes:616 acl:0 sco:0 events:34 errors:0
        TX bytes:380 acl:0 sco:0 commands:34 errors:0

root@raspberrypi:/home/pi# hciconfig dev up
root@raspberrypi:/home/pi# hcitool lescan
LE Scan ...
00:22:D0:6D:E0:E6 (unknown)
00:22:D0:6D:E0:E6 Polar H7 6DE0E61C
^Croot@raspberrypi:/home/pi# hcitool lecc 00:22:D0:6D:E0:E6
Connection handle 64
root@raspberrypi:/home/pi# gatttool -b 00:22:D0:6D:E0:E6 -I
[00:22:D0:6D:E0:E6][LE]> connect
Attempting to connect to 00:22:D0:6D:E0:E6
Error connect: Connection refused (111)
[00:22:D0:6D:E0:E6][LE]> 

我试着关注这个话题:BLE gatttool cannot connect even though device is discoverable with hcitool lescan 但它对我不起作用。

我不得不在 /etc/bluetooth/main.conf -> DisablePlugins=pnat 中禁用插件 pnat。看了说不稳定,但是我对这个插件了解不多

您可以尝试使用 gatttool -b 00:22:D0:6D:E0:E6 -I 然后连接,只是在发现 MAC 之前没有连接 hcitool lecc 00:22:D0:6D:E0:E6

默认情况下 GATT 未启用。将以下行添加到 /etc/bluetooth/main.conf

EnableLE = true           // Enable Low Energy support. Default is false.
AttributeServer = true    // Enable the GATT attribute server. Default is false.

在将 LE address type 设置为 random 后,我可以使用 gatttool 通过 BluetoothRaspberry 连接到我的 Android 设备使用 -t random 参数,即 :

sudo gatttool -t random -b DD:9D:0B:43:A1:77 -I
connect

来自 gatttool man

--t, ---addr-type=[public | random] 
# Set LE address type. Default: public

USAGE gatttool [OPTION...]

 Help Options:
     -h, --help                                  Show help options
     -h, --help                                  Show help options
     --help-all                                  Show all help options
     --help-gatt                                 Show all GATT commands
     --help-params                               Show  all  Primary  Services/Characteristics
   arguments
     --help-char-read-write                       Show  all  Characteristics Value/Descriptor
   Read/Write arguments

   Application Options:
     --i, ---adapter=hciX                        Specify local adapter interface
     --b, ---device=MAC                          Specify remote Bluetooth address
     --t, ---addr-type=[public | random]         Set LE address type. Default: public
     --m, ---mtu=MTU                             Specify the MTU size
     --p, ---psm=PSM                             Specify the PSM for GATT/ATT over BR/EDR
     --l, ---sec-level=[low | medium | high]     Set security level. Default: low
     --I, ---interactive                         Use interactive mode

如果您仍然想知道随机数为何有效。我已经浏览了代码,这是我发现的。

​-t ​(Addr:Type: Set LE Address Type)     
Public | random    
Default: Public

A random or static address is a 48-bit randomly generated address and shall meet the following requirements:

• The two most significant bits of the static address shall be equal to ‘1’

• All bits of the random part of the static address shall not be equal to ‘1’

• All bits of the random part of the static address shall not be equal to ‘0’

(Source)

修复了 raspberry pi 3B 运行 raspbian buster 中 Updating/installing BlueZ 通过 apt-get

连接被拒绝 (111) 的问题

> sudo apt-get install --no-install-recommends bluetooth

然后重新 运行

> sudo gatttool -t random -b E9:1C:89:B7:16:F9 -I

我知道了运行:

btmgmt le on