Reconnect a CBPeripheral faster

眉间皱痕 提交于 2019-12-04 10:03:13

问题


I'm developing an app which connects and disconnects several times to different bluetooth low energy peripherals. After a lot of research on the internet, I didn't have found a way to store already discovered services and characteristics, so that I can reconnect faster. At the moment I'm discovering the required services and characteristics after each reconnection, which costs me between 1.5 and 2 seconds.

Does someone have experience or a solution to store/cache a CBService or a CBCharacteristic or does someone know a way to reconnect faster?

Thanks for any help


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/14870948/reconnect-a-cbperipheral-faster

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