I'm using corebluetooth framework to connect my iphone to BLE devices. i have successfully achieved connecting to a peripheral and discovering services and characteristics as well as update value for a characteristic.
my problem begins when my BLE device is disconnected. when that happen i do the following:
- retrievePeripheralsWithIdentifiers
- cancelPeripheralConnection (just in case)
- connectPeripheral once again
- activePeripheral = peripheral
- [activePeripheral discoverServices:services]; (i check first if the peripheral has services , but it doesn't have)
when i try to discoverServices my app crashes!!
CODE:
NSArray *retrievePeripherals = [[PRCentralDiscoverManger sharedInstance] retrivePeripheral:@[peripheral.identifier]];
NSLog(@"retrievePeripherals: %@",retrievePeripherals);
for (CBPeripheral *p in retrievePeripherals)
{
if (peripheral == p )
{
for (PRPeripheralManger *pm in _serviceArray)
{
if (p == pm.peripheral)
{
NSLog(@"PRPeripheralManger in _serviceArray");
//[[PRCentralDiscoverManger sharedInstance] disconnectPeripheral:p];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Here your non-main thread.
[NSThread sleepForTimeInterval:2.0f];
dispatch_async(dispatch_get_main_queue(), ^{
//Here you returns to main thread.
NSLog(@"Here you returns to main thread");
[[PRCentralDiscoverManger sharedInstance] connectPeripheral:peripheral];
service = [[PRPeripheralManger alloc]initWithPeripheral:peripheral controller:self];
[service startDiscoveringServicesWithUUIDString:uuid];
});
});
break;
}
}
}
}
- (id) initWithPeripheral:(CBPeripheral *)peripheral controller:(id<PRPeripheralMangerProtocol>)controller
{
self = [super init];
if (self)
{
activePeripheral = peripheral;
[activePeripheral setDelegate:self];
_peripheralDelegate = controller;
}
return self;
}
- (void) startDiscoveringServicesWithUUIDString:(NSString *)uuidString
{
NSLog(@"startDiscoveringServicesWithUUIDString peripheral:%@",activePeripheral); //EXSIST
NSLog(@"startDiscoveringServicesWithUUIDString services :%@",activePeripheral.services); // ALWAYS NULL
if (activePeripheral.services)
{
NSLog(@"activePeripheral.services: %@",activePeripheral.services);
[self peripheral:activePeripheral didDiscoverServices:nil];
}
else
{
if (uuidString)
{
NSLog(@"discoverServices: %@",uuidString);
NSArray *services = @[[CBUUID UUIDWithString:uuidString]];
[activePeripheral discoverServices:services];//CRASHES HERE
}
else
{
NSLog(@"discoverServices:nil");
[activePeripheral discoverServices:nil];
}
}
}
CRASH LOG:
* thread #1: tid = 0x58f4, 0x30b00f46 libobjc.A.dylib`objc_msgSend + 6, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xc000000c)
frame #0: 0x30b00f46 libobjc.A.dylib`objc_msgSend + 6
frame #1: 0x230142ac CoreBluetooth`-[CBPeripheral handleServicesDiscovered:] + 620
frame #2: 0x230122b6 CoreBluetooth`-[CBPeripheral handleMsg:args:] + 282
frame #3: 0x2300e674 CoreBluetooth`-[CBCentralManager xpcConnection:didReceiveMsg:args:] + 256
frame #4: 0x23019356 CoreBluetooth`__34-[CBXpcConnection handleMsg:args:]_block_invoke + 54
frame #5: 0x002fb9da libdispatch.dylib`_dispatch_call_block_and_release + 10
frame #6: 0x002fb9c6 libdispatch.dylib`_dispatch_client_callout + 22
frame #7: 0x00303e28 libdispatch.dylib`_dispatch_queue_drain + 1092
frame #8: 0x002fe2c8 libdispatch.dylib`_dispatch_queue_invoke + 88
frame #9: 0x002ff21e libdispatch.dylib`_dispatch_main_queue_callback_4CF + 346
frame #10: 0x233003b0 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
frame #11: 0x232feab0 CoreFoundation`__CFRunLoopRun + 1512
frame #12: 0x2324c3c0 CoreFoundation`CFRunLoopRunSpecific + 476
frame #13: 0x2324c1d2 CoreFoundation`CFRunLoopRunInMode + 106
frame #14: 0x2a64a0a8 GraphicsServices`GSEventRunModal + 136
frame #15: 0x2685a7b0 UIKit`UIApplicationMain + 1440
im really new to stackoverflow sorry if this is not the way to present the question.
nil
to this method:discoverServices:
, as far as I understood. – gran33Exception Breakpoint
: developer.apple.com/library/ios/recipes/… – gran33NSArray
– gran33