1

I am facing crash issue on iOS CoreBluetooth's [CBPeripheralManager AddService:].

Seems it occurred due to assertion failure in addService method. Tried so many ways, still can't figure out the issue.

As per my understanding this is not null pointer exception as I already tested passing nil value as AddService parameter which yield different issue.

I have implemented all the delegates of CBPeripheralManager including CBCentral delegates.

In general it works fine. Normally I can't reproduce this issue with intention. But it occurs suddenly.

Please help . Crash log:

0   CoreFoundation                 0x18b3cafe0 __exceptionPreprocess + 124
1   libobjc.A.dylib                0x189e2c538 objc_exception_throw + 56
2   CoreFoundation                 0x18b3caeb4 +[NSException raise:format:arguments:] + 104
3   Foundation                     0x18be62720 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 112
4   CoreBluetooth                  0x1922ab420 -[CBPeripheralManager addService:] + 820
5   MyApp                        0x10110e1f4 -[MyPeripheralManager addServiceIntoPeripheralManager] (MyPeripheralManager.m:202)

Code Snipet:

-(void)addService{
if (!isServiceAdded) {
    CLLog("Add Service timer started");
    [backgroundTaskManager manageBackgroundRunningTask];
    /*
     AddService will be called after 5 seconds
     when application launching finished or iOS BT on
     */
    [self performSelector:@selector(addServiceIntoPeripheralManager) withObject:self afterDelay:SERVICE_ADDING_DELAY]; // 5 secs

} else {
    CLDbg("Service already added");
}
}
- (void)addServiceIntoPeripheralManager{
CLLog("Add Service timer expired");
CLDbg("Service Adding: %@", [UUIDString]);
[cbPeripheralManager addService:Service];
}

Thanks in advance.

taserghar
  • 340
  • 3
  • 15
  • share some code which you try – Brijesh Shiroya Jul 17 '17 at 04:47
  • please share your code – Dhiru Jul 17 '17 at 04:54
  • code snippet is added, would you please share the reason behind this type of assertion failure. – taserghar Jul 17 '17 at 05:03
  • What is the text of the assertion failure? This will describe what went wrong. Using timers to delay operations is a code smell. Why are you doing that? – Paulw11 Jul 17 '17 at 06:09
  • @Paulw11, I already added the crash log. It says, only Assertion failure. Delay is added to reduce frequent addService call when user kill app frequently after turned on. – taserghar Jul 17 '17 at 06:35
  • 1
    There will be a text description that was passed to the assertion failure; you need to find this in your console log or you are just guessing. You should only add the service in response to a poweredOn state (or poweredOn state plus subsequent user action). It should not be possible for your app to attempt to add the service more than once – Paulw11 Jul 17 '17 at 06:40
  • @Paulw11, given AddService() is called from another file's addService() where I checked BT status before calling addService(). Like below: `__ another file __ - (void)addService{ if( serviceUUID && bleManagerState.peripheralPowerState == BLE_MANAGER_STATE_POWER_ON && (bleManagerState.serviceState != BLE_MANAGER_STATE_SERVICE_ADDED || bleManagerState.serviceState != BLE_MANAGER_STATE_SERVICE_ADDING) ) { bleManagerState.serviceState = BLE_MANAGER_STATE_SERVICE_ADDING; [peripheralManager addService]; } } ` – taserghar Jul 17 '17 at 06:53
  • But what calls that addservice? Checking state variables like that is asking for timing related race condition issues; in particular there should be no "adding" state. Either you have added the service or you haven't. In `didDetermineState` just add the service when you get the `poweredOn` state. – Paulw11 Jul 17 '17 at 06:57
  • yes, there is no adding step, I added this step to maintain internal state and for debugging. Actually this is a sometimes issue. In general case, I didn't find that issue. Everything works fine. But testers reporting this crash issue where they don't have console level access. – taserghar Jul 17 '17 at 09:05

1 Answers1

1

You are not implementing the callback updates of your BluetoothManager

see this answer

and This answer

I hope this will help you

Dhiru
  • 3,040
  • 3
  • 25
  • 69
  • I have implemented all the delegates of CBPeripheralManager including CBCentral delegates. In general it works fine. Normally I can't reproduce this issue with intention. But it occurs suddenly. – taserghar Jul 17 '17 at 05:45