相关疑难解决方法(0)

Corebluetooth中央管理器回调确实发现了两次外围设备

我像这样扫描我的外围设备:

NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] 
                                                            forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
        // Scan for peripherals with given UUID
        [cm scanForPeripheralsWithServices:[NSArray arrayWithObject:HeliController.serviceUUID] options:scanOptions]
Run Code Online (Sandbox Code Playgroud)

没问题,我找到外围设备并能够连接到它.正如你可以看到我给它CBCentralManagerScanOptionAllowDuplicatesKeybool 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");
}
}
Run Code Online (Sandbox Code Playgroud)

有时我得到

Discovered
Already discovered
Run Code Online (Sandbox Code Playgroud)

作为我的控制台中的输出,大多数时候只Discovered显示消息.

在我的外围代理中,我首先发现服务,然后调用[peripheral discoverCharacteristics并始终发生​​回调:

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

NSLog(@"Did discover characteristic …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c callback core-bluetooth bluetooth-lowenergy

10
推荐指数
2
解决办法
1万
查看次数