我需要连接6头黑甲虫到我的拉皮3B+同时接收一些数据。然而,只有一个连接到一个钝甲虫,我已经有持续断开后,收到几个包。有时我能够在断开之前接收到20个数据包,而有时我能够在断开之前收到5个数据包。接收到的数据包数量波动。
这算正常吗?
我的raspi 3B+安装在Raspbian /Linux10 (buster)中。我已经安装了带有BlueepyVersion1.3.0的python3。
Beetle是一个基于Arduino Uno的基于蓝牙4.0的板,Raspi 3B+有一个蓝牙HCI版本: 5.0 (0x9)
我试图通过重新连接来处理断开,它工作得很好。但是重新连接所需的时间需要一段时间(4-5秒),这将是我从Bluno甲虫侧得到的数据。
如何进一步增强BLE的健壮性?这是我下面的python代码,我只听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)我的无花果:
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 firmaware使BLE不稳定??https://github.com/IanHarvey/bluepy/issues/396
发布于 2020-03-13 13:01:52
我可以得出结论,RPI固件在与另一个板进行测试后有问题。
的原因是否是RPI使BLE不稳定??https://github.com/IanHarvey/bluepy/issues/396
在测试Ubuntu18.04仿生运行的Ultra96 v2的相同设置时,不需要断开连接。我每隔1ms就能读/写一次bluno设备,而且连接是健壮的。
因此,似乎问题在于RPI固件。我用的是Raspberry pi 3b+的Rasbian。
https://stackoverflow.com/questions/60643892
复制相似问题