![](/img/trans.png)
[英]Continuous scanning for iOS CoreBluetooth Central Manager?
[英]iOS CoreBluetooth not scanning for services in iPad Air
我正在開發一個連接到BLE外設並從中接收數據的應用程序。
它掃描外圍設備,查找外圍設備,發現服務,如果找到了正確的服務,它將接收數據。
它在iPhone 5上運行良好,但是當我在iPad Air上運行它時,它會連接,但不會發現任何服務,也不會收到任何數據。 兩台設備都運行iOS 7.0.4
這是代碼的一些相關部分
- (void)startScan
{
[manager scanForPeripheralsWithServices:nil options:nil];
}
- (void)stopScan
{
[manager stopScan];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did discover peripheral. peripheral: %@ rssi: %@, id: %@ advertisementData: %@ ", peripheral, RSSI, peripheral.identifier, advertisementData);
if(![self.dicoveredPeripherals containsObject:peripheral])
[self.dicoveredPeripherals addObject:peripheral];
[manager retrievePeripheralsWithIdentifiers:[NSArray arrayWithObject:(id)peripheral.identifier]];
[manager connectPeripheral:peripheral options:[NSDictionary dictionaryWithObject: [NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
NSLog(@"Retrieved peripheral: %lu - %@", (unsigned long)[peripherals count], peripherals);
[self stopScan];
/* If there are any known devices, automatically connect to it.*/
if([peripherals count] >= 1)
{
testPeripheral = [peripherals objectAtIndex:0];
[manager connectPeripheral:testPeripheral
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];
}
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"Did connect to peripheral: %@", peripheral);
[self.delegate statusMessage:[NSString stringWithFormat:@"Did connect to peripheral: %@\n", peripheral]];
[peripheral setDelegate:self];
[peripheral discoverServices:nil];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
for (CBService *service in peripheral.services) {
NSLog(@"discovered service [%@]",service.UUID);
[peripheral discoverCharacteristics:nil forService:service];
}
}
iPhone 5上的日志輸出是:
Did discover peripheral. peripheral: <CBPeripheral: 0x14587320 identifier = 3EE10DD6-99CD- 7E9C-C492-087CE3B980E6, Name = "BLE Sens", state = disconnected> rssi: -53, id: <__NSConcreteUUID 0x145f82f0> 3EE10DD6-99CD-7E9C-C492-087CE3B980E6 advertisementData: {
kCBAdvDataChannel = 38;
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = BLESensor;
kCBAdvDataServiceUUIDs = (
"Unknown (<fff0>)"
);
kCBAdvDataTxPowerLevel = 0;
}
Did connect to peripheral: <CBPeripheral: 0x14587320 identifier = 3EE10DD6-99CD-7E9C-C492-087CE3B980E6, Name = "BLE Sens", state = connected>
Connected
discovered service [Device Information]
discovered service [Unknown (<fff0>)]
discovered service [Unknown (<ffe0>)]
Service found with UUID: Device Information
在iPadAir上,它只是到達連接的部分,沒有找到任何服務:
Did discover peripheral. peripheral: <CBPeripheral: 0x1700a1440 identifier = 23890BB4-FB07-AA1A-8D4F-B1427C859447, Name = "BLE Sens", state = disconnected> rssi: -60, id: <__NSConcreteUUID 0x17802f740> 23890BB4-FB07-AA1A-8D4F-B1427C859447 advertisementData: {
kCBAdvDataChannel = 39;
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = BLESensor;
kCBAdvDataServiceUUIDs = (
"Unknown (<fff0>)"
);
kCBAdvDataTxPowerLevel = 0;
}
Did connect to peripheral: <CBPeripheral: 0x1700a1440 identifier = 23890BB4-FB07-AA1A-8D4F-B1427C859447, Name = "BLE Sens", state = connected>
Connected
Apple承認這是iOS 7.0中的一個問題。 他們承諾在iOS 7.1中解決它。 我用iOS 7.1測試了它,它就解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.