树莓派无法连接到BLE设备

19

我正在尝试连接一个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无法连接,即使使用hcitool lescan可以发现设备,但对我来说没有用。


如果你有Bluez,为什么不直接使用bluetoothctl呢? - Zimano
7个回答

41

在将 LE address type 设置为 random 并使用 -t random 参数后,我成功地通过 gatttoolRaspberry 连接到了我的 Android 设备,采用的是 Bluetooth

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

来自 gatttool说明文档

--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

如果有人按照这篇文章中的所有步骤操作,打开了BLE,使用了“-t random”,但连接仍然被拒绝怎么办?这是我朋友问的。 - Panossa

6
默认情况下,GATT未启用。在/etc/bluetooth/main.conf中添加以下行。
EnableLE = true           // Enable Low Energy support. Default is false.
AttributeServer = true    // Enable the GATT attribute server. Default is false.

2

我用以下方法使它运行起来:

btmgmt le on

1

通过使用apt-get更新/安装BlueZ,解决了我在运行raspbian buster的树莓派3B中遇到的连接被拒绝(111)问题。

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

然后重新运行

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


0

我不得不在/etc/bluetooth/main.conf中禁用插件pnat -> DisablePlugins=pnat。我读到它是不稳定的,但我对这个插件不太了解。


天啊,这是最被低估的答案!在尝试了其他方法数天后,它解决了我在Raspbian系统上遇到的所有问题。 - Andrea Lazzarotto

0

如果你仍然想知道为什么随机函数起作用了。我已经查看了代码,这是我发现的。

​-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’

(来源)


1
一个评论中的链接比创建新答案更为恰当。 - Pedro Lobito

-1

您可以尝试使用gatttool -b 00:22:D0:6D:E0:E6 -I,然后连接,在发现MAC地址后不要先使用hcitool lecc 00:22:D0:6D:E0:E6进行连接。


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