Reconnect a CBPeripheral faster

[亡魂溺海] 提交于 2019-12-03 03:56:54

I've tried caching peripheral, service and characteristic objects in memory and manual caching does not work. Once you have disconnected from the peripheral, the service and characteristic objects are no longer valid for use. In fact, even the CBPeripheral can change out from under you – CoreBluetooth has an internal behavior where the device UDID will change every few minutes (if the CBPeripheral is another iOS device).

However, if you are running developing for iOS 6, there is a way to speed things up. If you watch the Advanced Bluetooth talk at WWDC 2012, you'll see a slide towards the end about caching services and characteristics. Essentially, the OS can cache them all for you but only for paired devices. To pair, you need to respond to a write request with an insufficient authentication error. For example, for an iOS peripheral you would write something like:

- (void)peripheralManager:(CBPeripheralManager *)peripheralManager didReceiveWriteRequests:(NSArray *)requests {
      ...

      [peripheralManager respondToRequest:request withResult:CBATTErrorInsufficientAuthentication];

      ...
}

That will pop up a pairing dialog on the iOS peripheral, after which point you will be paired. Other than that you don't have to change your code – just call discoverServices etc as normal and they will respond more quickly (ie instantly).

I have also tested this behavior on 10.8.3 and it does not appear to work. So, I don't know of a way to speed things up on OS X (other than staying connected to the peripheral).

iOS 7 adds additional caching (iOS 7: What's New in Bluetooth LE). I'm seeing times less than 100ms (~80ms average) total to reconnect to a peripheral, discover services, discover characteristics, and read the value of a single characteristic.

My test configuration was an iPad Air connecting to an iPad 3.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!