首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝光BLE在收到Bluno甲虫的几个数据包后断开连接

蓝光BLE在收到Bluno甲虫的几个数据包后断开连接
EN

Stack Overflow用户
提问于 2020-03-11 20:20:57
回答 1查看 1.2K关注 0票数 1

我需要连接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发送的数据。

代码语言:javascript
复制
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)

我的无花果:

代码语言:javascript
复制
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

通过蓝牙连接似乎很好:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60643892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档