它似乎是 iOS 6 中的一个错误/功能增强,我认为由于代码的异步性质,它在 iPhone 5 上更加明显。我也有适用于 4S 但不适用于 5 的代码。
大多数人发布的解决方法是确保您在找到时连接到您找到的每台设备。解释是在建立连接之前不会分配 UUID。
您可以在 TI 传感器标签代码中看到一个示例,您可以从 TI 网站下载该代码
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
NSLog(@"Found a BLE Device : %@",peripheral);
/* iOS 6.0 bug workaround : connect to device before displaying UUID !
The reason for this is that the CFUUID .UUID property of CBPeripheral
here is null the first time an unkown (never connected before in any app)
peripheral is connected. So therefore we connect to all peripherals we find.
*/
peripheral.delegate = self;
[central connectPeripheral:peripheral options:nil];
[self.nDevices addObject:peripheral];
}
但是值得注意的是,您还可以执行以下操作
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"didDiscoverPeripheral");
if (![foundPeripherals containsObject:peripheral]) {
NSLog(@"foundPeripherals addObject %@", peripheral.name);
[foundPeripherals addObject:peripheral];
} else {
NSInteger index = [foundPeripherals indexOfObject:peripheral];
NSLog(@"foundPeripherals replaceObject %@", peripheral.name);
[foundPeripherals replaceObjectAtIndex:index withObject:peripheral];
}
}
asdidDiscoverPeripheral
为每个设备调用不止一次(我在 iPhone 5 上看到它调用了两次),第二次外围设备详细信息完成。