从Bluno Beetle收到几个数据包后,Bluepy BLE断开连接

问题描述 投票:0回答:1

我需要将6颗蓝绿色的甲虫连接到我的Raspi 3B +,才能同时接收一些数据。但是,只有一个蓝甲虫有1个连接,在接收到一些数据包之后,我已经处于不断断开连接的状态。有时在断开连接之前我可以接收20个数据包,而有时在断开连接之前我可以接收5个数据包。接收的数据包数量波动。

这应该正常吗?

我的raspi 3B +已与Raspbian GNU / Linux 10(破坏程序)一起安装。我安装了安装了bluepy版本1.3.0的python3。

The 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

通过蓝牙连接似乎很好:

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

bluetooth-lowenergy raspberry-pi3 bluno
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。

© www.soinside.com 2019 - 2024. All rights reserved.