Using the following code to re-publish my Bluetooth service, I intermittently get a crash with the error message below.
self.stopAdvertising()
peripheralManager.removeAllServices()
if let service = self.cbService {
NSThread.sleepForTimeInterval(0.1)
peripheralManager.addService(service)
self.startAdvertising()
}
2016-06-03 09:32:10.262 BottleSimulator[180:3587] * Assertion failure in -[CBPeripheralManager addService:], /SourceCache/CoreBluetooth/CoreBluetooth-256/CBPeripheralManager.m:307 2016-06-03 09:32:10.270 BottleSimulator[180:3587] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Services cannot be added more than once'
Why am I getting this? I would think that the call to removeAllServices()
would protect against this. I even added a sleep for testing, but it didn't stop the intermittent crashes.
Is there a more reliable way to re-publish a service?