简体   繁体   English

Raspberry Pi 4 用作 BLE 中央设备连接问题

[英]Raspberry Pi 4 used as BLE central device connection issues

For a professional project, i have to use a RPI4 as a central device to connect to a particular peripheral device.对于专业项目,我必须使用 RPI4 作为中央设备来连接到特定的外围设备。 For testing, I developped a program to simulate a peripheral on a RPi (thanks to bleno Node.js module) by setting a GATT server and use another RPi as my central with the bluepy Python library.为了进行测试,我开发了一个程序来模拟 RPi 上的外围设备(感谢 bleno Node.js 模块),方法是设置 GATT 服务器并使用另一个 RPi 作为我的 bluepy Python 库的中心。

All worked fine but when I set the advertising interval higher than 4000ms on my peripheral, connection doesn't work anymore (even on the RPi GATT server or with the production one).一切正常,但是当我在外围设备上设置高于 4000 毫秒的广告间隔时,连接不再起作用(即使在 RPi GATT 服务器或生产服务器上)。

I tried to use gatttool/hcitool on my central, same issues, it works well but only if advertising interval is less than 4000ms.我尝试在我的中心问题上使用 gatttool/hcitool,它运行良好,但前提是广告间隔小于 4000 毫秒。 But it works when I try to connect to my GATT server with my phone with a dedicated application (nRF connect).但是当我尝试使用带有专用应用程序(nRF 连接)的手机连接到 GATT 服务器时,它会起作用。

After some research, I found that the Linux kernel only validates values of connection interval within the range 7.5ms - 4000ms ( https://lkml.org/lkml/2019/8/2/358 ), which match exactly with my experimental values.经过一番研究,我发现Linux内核只验证7.5ms - 4000ms范围内的连接间隔值( https://lkml.org/lkml/2019/8/2/358 ),这与我的实验值完全匹配. But except if I don't understand something with BLE, connection interval and advertising interval are totally independent and it should not be a problem.但是除非我对 BLE 不了解,否则连接间隔和广告间隔是完全独立的,这应该不是问题。 In the Bluetooth documentation, I found that maximum advertising interval value should be 10240ms.在蓝牙文档中,我发现最大广告间隔值应该是 10240ms。 There is something I don't understand.有一点我不明白。

Here is my GATT server running on one RPi4 if you want to reproduce that.如果你想重现它,这是我在一个 RPi4 上运行的 GATT 服务器。 I start it using sudo BLENO_ADVERTISING_INTERVAL=xxxx node my_gatt_server.js with xxxx the advertising interval I want in ms.我使用sudo BLENO_ADVERTISING_INTERVAL=xxxx node my_gatt_server.js启动它,其中 xxxx 是我想要的广告间隔(以毫秒为单位)。

var bleno = require('bleno');

// Once bleno starts, begin advertising our BLE address
bleno.on('stateChange', function(state) {
    console.log('State change: ' + state);
    if (state === 'poweredOn') {
        bleno.startAdvertising('MyDevice',['6d79686561727473656e74696e656c10']);
    } else {
        bleno.stopAdvertising();
    }
});

// Notify the console that we've accepted a connection
bleno.on('accept', function(clientAddress) {
    console.log("Accepted connection from address: " + clientAddress);
});

// Notify the console that we have disconnected from a client
bleno.on('disconnect', function(clientAddress) {
    console.log("Disconnected from address: " + clientAddress);
});

// When we begin advertising, create a new service and characteristic
bleno.on('advertisingStart', function(error) {
    if (error) {
        console.log("Advertising start error: " + error);
    } else {
        console.log("Advertising start success");
        bleno.setServices([

            // Define a new service
            new bleno.PrimaryService({
                uuid : '6d79686561727473656e74696e656c10',
                characteristics : [

                    // ECG characteristic
                    new bleno.Characteristic({
                        value : 'hello!',
                        uuid : '6d79686561727473656e74696e656c11',
                        properties : ['read'],

                        // Send a message back to the client with the characteristic's value
                        onReadRequest : function(offset, callback) {
                            console.log("Read request received");
                            callback(this.RESULT_SUCCESS, new Buffer("Echo: " + this.value);
                        }
                    })
                ]
            }), 
        ]);
    }
});

HCI packets logs HCI 数据包日志

When connection succeeded连接成功时

Bluetooth monitor ver 5.50
= Note: Linux version 4.19.57-v7l+ (armv7l)                                                                                                                                                                                         0.501891
= Note: Bluetooth subsystem version 2.22                                                                                                                                                                                            0.501900
= New Index: xx:xx:xx:xx:xx:xx (Primary,UART,hci0)                                                                                                                                                                           [hci0] 0.501905
= Open Index: xx:xx:xx:xx:xx:xx                                                                                                                                                                                              [hci0] 0.501908
= Index Info: xx:xx:xx:xx:xx:xx (Cypress Semiconductor Corporation)                                                                                                                                                          [hci0] 0.501911
@ MGMT Open: bluetoothd (privileged) version 1.14                                                                                                                                                                          {0x0001} 0.501915
@ MGMT Open: btmon (privileged) version 1.14                                                                                                                                                                               {0x0002} 0.501989
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                                                                                #1 [hci0] 4.586280
        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.586656
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                    #3 [hci0] 4.586717
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                               #4 [hci0] 4.587132
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 33                                                                                                                                                                                 #5 [hci0] 4.619942
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Data length: 21
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        128-bit Service UUIDs (partial): 1 entry
          Vendor specific (6d796865-6172-7473-656e-74696e656c10)
        RSSI: -31 dBm (0xe1)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                    #6 [hci0] 4.620014
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                               #7 [hci0] 4.621661
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Create Connection (0x08|0x000d) plen 25                                                                                                                                                                 #8 [hci0] 4.621724
        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: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        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] 4.622251
      LE Create Connection (0x08|0x000d) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 19                                                                                                                                                                                #10 [hci0] 4.726113
      LE Connection Complete (0x01)
        Status: Success (0x00)
        Handle: 64
        Role: Master (0x00)
        Peer address type: Public (0x00)
        Peer address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        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 34                                                                                                                                                                     {0x0002} [hci0] 4.726162
        LE Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Flags: 0x00000000
        Data length: 21
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        128-bit Service UUIDs (partial): 1 entry
          Vendor specific (6d796865-6172-7473-656e-74696e656c10)
@ MGMT Event: Device Connected (0x000b) plen 34                                                                                                                                                                     {0x0001} [hci0] 4.726162
        LE Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Flags: 0x00000000
        Data length: 21
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        128-bit Service UUIDs (partial): 1 entry
          Vendor specific (6d796865-6172-7473-656e-74696e656c10)
< HCI Command: LE Read Remote Used Features (0x08|0x0016) plen 2                                                                                                                                                         #11 [hci0] 4.726356
        Handle: 64
> HCI Event: Command Status (0x0f) plen 4                                                                                                                                                                                #12 [hci0] 4.726943
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
> HCI Event: Command Complete (0x0e) plen 14                                                                                                                                                                             #13 [hci0] 4.726951
      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] 4.881359
      LE Read Remote Used Features (0x04)
        Status: Success (0x00)
        Handle: 64
        Features: 0x3f 0x00 0x00 0x00 0x00 0x00 0x00 0x00
          LE Encryption
          Connection Parameter Request Procedure
          Extended Reject Indication
          Slave-initiated Features Exchange
          LE Ping
          LE Data Packet Length Extension
> HCI Event: LE Meta Event (0x3e) plen 11                                                                                                                                                                                #15 [hci0] 5.027630
      LE Data Length Change (0x07)
        Handle: 64
        Max TX octets: 251
        Max TX time: 2120
        Max RX octets: 251
        Max RX time: 2120

When connection failed连接失败时

Bluetooth monitor ver 5.50
= Note: Linux version 4.19.57-v7l+ (armv7l)                                                                                                                                                                                         0.114663
= Note: Bluetooth subsystem version 2.22                                                                                                                                                                                            0.114671
= New Index: xx:xx:xx:xx:xx:xx (Primary,UART,hci0)                                                                                                                                                                           [hci0] 0.114675
= Open Index: xx:xx:xx:xx:xx:xx                                                                                                                                                                                              [hci0] 0.114678
= Index Info: xx:xx:xx:xx:xx:xx (Cypress Semiconductor Corporation)                                                                                                                                                          [hci0] 0.114681
@ MGMT Open: bluetoothd (privileged) version 1.14                                                                                                                                                                          {0x0001} 0.114686
@ MGMT Open: btmon (privileged) version 1.14                                                                                                                                                                               {0x0002} 0.114760
< HCI Command: Disconnect (0x01|0x0006) plen 3                                                                                                                                                                            #1 [hci0] 5.864108
        Handle: 64
        Reason: Remote User Terminated Connection (0x13)
> HCI Event: Command Status (0x0f) plen 4                                                                                                                                                                                 #2 [hci0] 5.864469
      Disconnect (0x01|0x0006) ncmd 1
        Status: Success (0x00)
> HCI Event: Disconnect Complete (0x05) plen 4                                                                                                                                                                            #3 [hci0] 5.883803
        Status: Success (0x00)
        Handle: 64
        Reason: Remote User Terminated Connection (0x13)
@ MGMT Event: Device Disconnected (0x000c) plen 8                                                                                                                                                                   {0x0002} [hci0] 5.883848
        LE Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Reason: Connection terminated by remote host (0x03)
@ MGMT Event: Device Disconnected (0x000c) plen 8                                                                                                                                                                   {0x0001} [hci0] 5.883848
        LE Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Reason: Connection terminated by remote host (0x03)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                                                                               #4 [hci0] 25.059876
        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                                                                                                                                                                              #5 [hci0] 25.060698
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                   #6 [hci0] 25.060762
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                              #7 [hci0] 25.061665
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 33                                                                                                                                                                                #8 [hci0] 39.473657
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Data length: 21
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        128-bit Service UUIDs (partial): 1 entry
          Vendor specific (6d796865-6172-7473-656e-74696e656c10)
        RSSI: -28 dBm (0xe4)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                   #9 [hci0] 39.473746
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                             #10 [hci0] 39.475900
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Create Connection (0x08|0x000d) plen 25                                                                                                                                                               #11 [hci0] 39.475956
        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: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        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                                                                                                                                                                               #12 [hci0] 39.476993
      LE Create Connection (0x08|0x000d) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Create Connection Cancel (0x08|0x000e) plen 0                                                                                                                                                         #13 [hci0] 43.544123
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                             #14 [hci0] 43.546763
      LE Create Connection Cancel (0x08|0x000e) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 19                                                                                                                                                                               #15 [hci0] 43.546914
      LE Connection Complete (0x01)
        Status: Unknown Connection Identifier (0x02)
        Handle: 64
        Role: Master (0x00)
        Peer address type: Public (0x00)
        Peer address: xx:xx:xx:xx:xx:xx (OUI DC-A6-32)
        Connection interval: 48.75 msec (0x0027)
        Connection latency: 0 (0x0000)
        Supervision timeout: 420 msec (0x002a)
        Master clock accuracy: 0x00

When it fails, gatttool output error is Error: connect error: Transport endpoint is not connected (107)失败时,gatttool 输出错误为Error: connect error: Transport endpoint is not connected (107)

4000 ms is a quite large advertising interval. 4000 毫秒是一个相当大的广告间隔。 Anyway, bluepy uses BlueZ, and when BlueZ connects, it first scans and when it finds the wanted device, it stops scanning and initiates a connection.无论如何,bluepy 使用 BlueZ,当 BlueZ 连接时,它首先扫描,当它找到想要的设备时,它停止扫描并启动连接。 During this connection attempt, there is a timeout before starting scanning again.在此连接尝试期间,再次开始扫描之前会超时。 It could be that this 4000 ms advertising interval is larger than the timeout.可能是这个 4000 毫秒的广告间隔大于超时。 It's commonly 2 seconds I think.我认为通常是 2 秒。

Please start "sudo btmon" on your central device before attempting to connect, let it capture and print the HCI packets and post the output here.在尝试连接之前,请在您的中央设备上启动“sudo btmon”,让它捕获并打印 HCI 数据包并在此处发布输出。

Advertising interval and Connection interval are completely independent.广告间隔和连接间隔是完全独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM