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