简体   繁体   中英

BLE device randomly disconnecting

I'm using a BLE device and connecting it to an via using 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? 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.

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 . Similarly, do not pass nil to 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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