【发布时间】:2015-07-07 07:30:10
【问题描述】:
我正在使用 corebluetooth 框架将我的 iphone 连接到 BLE 设备。 我已经成功地连接到外围设备并发现服务和特性以及更新特性的值。
当我的 BLE 设备断开连接时,我的问题就开始了。发生这种情况时,我会执行以下操作:
- retrievePeripheralsWithIdentifiers
- cancelPeripheralConnection(以防万一)
- 再次连接外围设备
- activePeripheral = 外围设备
- [主动外设发现服务:服务]; (我先检查外设是否有服务,但没有)
当我尝试发现服务时,我的应用崩溃了!!
代码:
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];
}
}
}
崩溃日志:
* 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
如果这不是提出问题的方式,我对 stackoverflow 真的很陌生。
【问题讨论】:
-
我已经编辑了我的问题并添加了代码
-
甜蜜!据我了解,我认为原因是您将
nil传递给此方法:discoverServices:。 -
随便加个
Exception Breakpoint:developer.apple.com/library/ios/recipes/… -
它在这个方法中崩溃:当我输入 nil 时发现服务,或者如果我输入一个 CBUUID 数组
-
见底部here,他们通过
NSArray
标签: ios core-bluetooth