ReadRSSI doesn't call the delegate method

后端 未结 3 985
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-08 15:52

I got a problem since the iOS 8 update, right now my app is connected to a BLE device and periodically reads the RSSI thanks to a timer and the ReadRSSI method.

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 16:11

    I recently ran into this issue and I had multiple issues that was causing it. Here's the solutions in checklist fashion, from simplest to most complex:

    1. The CBCentralManager won't hold a strong reference to the peripheral, you need to keep it yourself.
    2. Make sure you actually are the peripheral.delegate.
    3. Make sure you're implementing the new method peripheral(peripheral:didReadRSSI:error:) and not the old one.
    4. iOS 8.0.2 introduced issues with the above method, any version after that 8.1, 8.2, 8.3, works without problems (What @Gamma-Point was mentioning).
    5. You can only readRSSI for devices that are connected to your central, so:
      1. For devices that retrieved by discovery, you can pass [CBCentralManagerScanOptionAllowDuplicatesKey : true] when doing scanForPeripheralsWithServices(_:options:). As seen in this answer.
      2. Also, there's a gotcha with the method central.retrieveConnectedPeripheralsWithServices. this method returns "connected" devices, but the readRSSI nor service discovery work until you actually call connectPeripheral(_:options:) on them, so even though they are connected to the iPhone/iPad/AppleWatch, they are not connected to your central, very annoying.

    This last one was the big gotcha for me, I was hoping to "pick the closest" connected or discovered device, but couldn't keep the RSSI updated on them. Documentation doesn't say anything either.

    What I ended up doing, was to build a big dictionary with all the devices indexed by [UUID : Device] (device being a wrapper for the CBPeripheral). Devices added via discovery get their RSSI updated via de discover method, and the connected ones via a GCD timer on the bluetooth queue that calls readRSSI and update their own RSSI reading.

提交回复
热议问题