1

我需要将 6 只蓝甲虫连接到我的 Raspi 3B+ 以同时接收一些数据。但是,只有 1 个连接到一只布鲁诺甲虫,我在收到几个数据包后已经经常断开连接。有时我能够在断开连接之前接收 20 个数据包,而有时我能够在断开连接之前接收 5 个数据包。接收的数据包数量波动。

这应该是正常的吗?

我的 raspi 3B+ 安装了 Raspbian GNU/Linux 10 (buster)。我安装了 python3 并安装了 bluepy 版本 1.3.0。

Bluno Beetle 是基于 Arduino Uno 的板,带有蓝牙 4.0 Raspi 3B+ 具有蓝牙 HCI 版本:5.0 (0x9)

我试图通过重新连接来处理断开连接,它工作正常。但是重新连接所需的时间需要一段时间(4-5 秒),我会从 Bluno 甲虫方面获得数据。

如何进一步增强 BLE 的鲁棒性?这是我下面的 python 代码,我只收听从 Bluno Beetle 发送的数据。

from bluepy import btle
from bluepy.btle import BTLEException, Scanner, BTLEDisconnectError
import threading

# Global Vars
connectionObjects = []      # Total of 6 Connections expected
connectedThreads = []       # Total of 6 Connections expected

threads = list()

class MyDelegate(btle. DefaultDelegate):
    def __init__(self, connection_index):
        btle.DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print("handling notification...")
        data_string = str(data)
        print(data_string)

class ConnectionHandlerThread (threading.Thread):

    def __init__(self, connection_index, BTAddress):
        threading.Thread.__init__(self)
        self.connection_index = connection_index
        self.BTAddress = BTAddress
        self.connection = connectionObjects[self.connection_index]

    def connect(self):
        self.connection.setDelegate(MyDelegate(self.connection_index))

    def run(self):
        self.connect()
        while True:
            try:
                if self.connection.waitForNotifications(1.0):
                    continue
                print("Waiting...")
            except:
                try:
                    self.connection.disconnect()
                except:
                    pass
                finally:
                    reestablish_connection(self.connection, self.BTAddress, self.connection_index)

def reestablish_connection (connection, BTAddr, index):
    while True:
        try:
            print("trying to reconnect with " + str(BTAddr) )
            connection.connect(str(BTAddr))
            print("re-connected to "+ str(BTAddr) +", index = " + str(index))
            return
        except:
            continue

BTAddress = ['1c:ba:8c:1d:3a:c1']

for index in range(len(BTAddress)):
    while True:
        try:
            p = btle.Peripheral()
            p.connect(BTAddress[index], btle.ADDR_TYPE_PUBLIC)
            break
        except btle.BTLEException as e:
            print("Connection Fail. Retrying now...")
            continue

    print ("Successful Connection to BLE " + str(BTAddress[index]))
    connectionObjects.append(p) # first index is 0, first connected is beetle num 1
    thread = ConnectionHandlerThread(len(connectionObjects)-1, BTAddress[index])
    thread.setName("BLE-Thread-" + str(len(connectionObjects)-1))
    thread.start()
    connectedThreads.append(thread)

我的 hciconfig:

hci0:   Type: Primary  Bus: UART
        BD Address: B8:27:EB:D4:EC:5D  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING
        RX bytes:204543 acl:2383 sco:0 events:8342 errors:0
        TX bytes:49523 acl:152 sco:0 commands:4072 errors:0

通过 bluetoothctl 连接似乎很好:

root@raspberrypi:~# bluetoothctl
Agent registered
[bluetooth]# connect 1c:ba:8c:1d:3a:c1
Attempting to connect to 1c:ba:8c:1d:3a:c1
[CHG] Device 1C:BA:8C:1D:3A:C1 Connected: yes
Connection successful

我一直在网上寻找一个强大的解决方案,但找不到防止断开连接或增加重新连接时间的方法。如果您能帮我解决这个问题,将不胜感激!

原因可能是 RPI 固件使 BLE 不稳定?https://github.com/IanHarvey/bluepy/issues/396

4

1 回答 1

0

在用另一块板测试后,我可以得出结论,RPI 固件有问题。

原因可能是 RPI 固件使 BLE 不稳定? https://github.com/IanHarvey/bluepy/issues/396

使用在 Ubuntu 18.04 仿生上运行的 Ultra96 v2 测试相同的设置时,无需断开连接即可工作。我能够每 1ms 读/写一次 bluno 设备,并且连接很可靠。

因此,问题似乎出在 RPI 固件上。我在 Raspberry pi 3b+ 上使用 Rasbian Buster。

于 2020-03-13T13:01:52.400 回答