简体   繁体   English

BLE设备随机断开

[英]BLE device randomly disconnecting

I'm using a BLE device and connecting it to an via using swift. 我正在使用BLE设备,并使用swift将其连接到通孔。 When I turn it on it'll connect, disconnect, then reconnect. 当我打开它时,它将连接,断开连接,然后重新连接。 I have no idea why it's disconnecting in the first place, battery is at 100% and I have nothing that triggers a disconnect, anybody have an idea of what could be happening? 我不知道为什么首先要断开连接,电池电量为100%,而且我什么也没有触发断开连接,任何人都知道会发生什么情况? here's a few of my functions for reference 这是我的一些功能供参考

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        kestrelPeripheral = peripheral
        kestrelPeripheral.delegate = self
        manager.connect(kestrelPeripheral)
        manager.stopScan()
        self.kestrelIsConnected = true

    }
    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {

        delegate?.didDisconnect()
        cancelReading()
        self.kestrelIsConnected = false
        self.manager = CBCentralManager(delegate: self, queue: nil)
    }

    func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        self.isInitialReading = false
        kestrelPeripheral.discoverServices(nil)
        delegate?.didConnect()
    }

for more context: 有关更多上下文:

func startReading() {
        self.manager = CBCentralManager(delegate: self, queue: nil)
        self.takeReading = true
        progressHUD = ReadingProgressHUD(text: "Taking Reading")
        self.vc!.view.addSubview(self.progressHUD)
    }

This would start the reading of values 这将开始读取值

First, I agree with CodeBender that having a lot of devices in the area can be challenging, but several things about your code make me suspicious. 首先,我同意CodeBender的观点,即在该区域拥有许多设备可能具有挑战性,但是关于您的代码的几件事使我感到怀疑。

First, you're not logging anything, so it's hard to know exactly what's going on. 首先,您没有记录任何内容,因此很难确切知道发生了什么。 You definitely want to log each step. 您肯定要记录每个步骤。

Make sure you're scanning for exactly the service you want; 确保您正在扫描所需的服务; do not pass nil in scanForPeripherals . 不要在scanForPeripherals传递nil Similarly, do not pass nil to discoverServices . 同样,不要将nil传递给discoverServices

But the most suspicious piece is here, and I'm suspecting it might be the cause: 但是最可疑的部分在这里,我怀疑这可能是原因:

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
    ...
    self.manager = CBCentralManager(delegate: self, queue: nil)
    ...
}

You shouldn't be creating a new manager every time any peripheral disconnects. 您不应该在每次外围设备断开连接时都创建新的管理器。 A central manager handles all the peripherals; 中央管理人员处理所有外围设备; not just one connection. 不只是一种联系。 You should be setting manager one time for the entire run of the program, and you should generally avoid having multiple CBCentralManager objects in the system. 您应该为程序的整个运行时间设置一次manager ,并且通常应该避免在系统中具有多个CBCentralManager对象。 It's not impossible to make multiple managers work, but I've found they often get in each others' way a bit. 让多个经理一起工作不是没有可能,但是我发现他们经常会互相干扰。

My suspicion is that you're connecting to more devices than you think you are, and when you disconnect from one of them, you reset your manager and disrupt the others. 我的怀疑是,您连接的设备数量超出了您的想象,并且当您断开其中一台设备的连接时,您会重置管理器并破坏其他设备。 It may not be that, it could be a lot of things, but that's the most suspicious part of the code you've posted here. 可能并非如此,可能有很多事情,但这是您在此处发布的代码中最可疑的部分。

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

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