我扫描我的周围是这样的:Corebluetooth中央管理器回调didDiscoverPeripheral两次
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
// Scan for peripherals with given UUID
[cm scanForPeripheralsWithServices:[NSArray arrayWithObject:HeliController.serviceUUID] options:scanOptions]
没问题的,我发现周围并能够连接到它。正如你可以看到我给它CBCentralManagerScanOptionAllowDuplicatesKey
与bool NO
不允许多个外设,但有时回调触发两次。
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI
{
if(!discovered){
discovered = YES;
NSLog(@"Discovered");
[cm stopScan];
[scanButton setTitle:@"Connect" forState:UIControlStateNormal];
}
else if(discovered){
discovered = YES
NSLog(@"Already discovered");
}
}
有些时候,我得到
Discovered
Already discovered
在我的控制台输出,并且大多数时候只有Discovered
消息显示。
在我的周围委托我第一次发现服务,然后调用[peripheral discoverCharacteristics
和回调总是发生:
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
NSLog(@"Did discover characteristic for service %@", [service.peripheral UUID]);
for(CBCharacteristic *c in [service characteristics]){
// We never get here when peripheral is discovered twice
if([[c UUID] isEqual:myCharacteristicUUID]){
NSLog(@"Found characteristic");
self.throttleCharacteristic = c;
}
}
当didDiscoverPeripheral
发生两次,service
在这种方法变得nil
,即使peripheral
不是(UUID,名字仍然是正确的)。
重启手机或者重置网络设置暂时修复该问题。
我真的需要得到这个固定!谢谢
你给出的“不超过一个设备”参数是什么? – yuklai 2012-07-19 19:58:20
false CBCentralManagerScanOptionAllowDuplicatesKey – chwi 2012-07-20 05:39:38
我的猜测是当信号强度很差时它会被调用两次。 – 2012-07-21 05:53:36