Corebluetooth中央管理器回调didDiscoverPeripheral两次 [英] Corebluetooth central manager callback didDiscoverPeripheral twice
问题描述
我扫描我的外围设备像这样:
I scan for my peripheral like this:
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
回调触发两次。
No problem there, I find the peripheral and are able to connect to it. As you can see I give it CBCentralManagerScanOptionAllowDuplicatesKey
with bool NO
to not allow for more than one peripheral, but sometimes the didDiscoverPeripheral
callback fires twice.
- (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
消息。
as output in my console, and most of the times only the Discovered
message shows.
在我的外围委托中,我首先发现服务,然后调用 [peripheral discoverCharacteristics
/ p>
In my peripheral delegate I first discover services, which then call [peripheral discoverCharacteristics
and the callback always occurs:
- (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 发生两次,在此方法中服务
变为 nil
,即使 peripheral
不是(UUID,名称仍然正确)。
When didDiscoverPeripheral
occur twice, service
becomes nil
in this method, even though peripheral
is not (UUID, name is still correct).
重新启动手机或重置网络设置会暂时解决问题。
Rebooting the phone or resetting the network settings fixes the problem temporarily.
我真的需要得到这个固定的!谢谢
I really need to get this fixed! Thank you
推荐答案
设备可能会在广告期间返回其他数据。这些可以以单独的分组到达,在不同的时间到达。在这种情况下,didDiscoverPeripheral在设备初次被看到时被调用,然后当有其他信息可用时被调用。
Devices may return additional data while advertising. These may arrive in separate packets, arriving at different times. In this case, didDiscoverPeripheral is called first when the device is initially seen, and then again when additional information becomes available for it.
CBCentralManagerScanOptionAllowDuplicatesKey是不同的。它告诉CoreBluetooth当设备再次广告时是否要接收重复的结果。它不阻止针对相同发现序列的多次调用didDiscoverPeripheral;
CBCentralManagerScanOptionAllowDuplicatesKey is different. It tells CoreBluetooth whether you want to receive duplicate results when the device advertises itself again. It doesn't prevent multiple calls to didDiscoverPeripheral for the same discovery sequence; it prevents it for repeated discovery sequences.
资料来源: http://lists.apple.com/archives/bluetooth-dev/2012/Apr/msg00047.html (Apple rep on bluetooth-dev)。
Source: http://lists.apple.com/archives/bluetooth-dev/2012/Apr/msg00047.html (the Apple rep on bluetooth-dev).
这篇关于Corebluetooth中央管理器回调didDiscoverPeripheral两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!