问题描述
我像这样扫描我的外围设备:
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.
在我的外设委托中,我首先发现服务,然后调用 [peripheraldiscoverCharacteristics
并且回调总是发生:
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
出现两次时,service
在此方法中变为 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 代表).
Source: http://lists.apple.com/archives/bluetooth-dev/2012/Apr/msg00047.html (the Apple rep on bluetooth-dev).
这篇关于Corebluetooth 中央管理器回调 didDiscoverPeripheral 两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!