我扫描我的周围是这样的:
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
不允许超过一个周,但有时didDiscoverPeripheral
回调闪光两次。
- (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,名字仍然是正确的)。
重新启动手机或重置网络设置暂时修复该问题。
我真的需要这个固定! 谢谢