Corebluetooth central manager callback didDiscover

2019-01-18 05:21发布

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]

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 didDiscoverPeripheralcallback 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");
}
}

Some times I get

Discovered
Already discovered

as output in my console, and most of the times only the Discoveredmessage shows.

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;

    }
}

When didDiscoverPeripheral occur twice, service becomes nilin 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

2条回答
走好不送
2楼-- · 2019-01-18 05:57

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 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.

Source: http://lists.apple.com/archives/bluetooth-dev/2012/Apr/msg00047.html (the Apple rep on bluetooth-dev).

查看更多
Viruses.
3楼-- · 2019-01-18 05:59

I don't think this parameter does what you think it does. My understanding from looking at how it is used in Apple samples like the Health Thermometer is that turning this flag on allows discovery of multiple different peripherals with the same UUID. For example, if you want to write an app that looks at four different thermometers in the same room, and finds all of them, you would need the parameter so the scan didn't stop after finding the first one.

In their code, Apple avoids duplicates like this:

NSMutableArray *peripherals = [self mutableArrayValueForKey:@"thermometers"];
if( ![self.thermometers containsObject:peripheral] )
    [peripherals addObject:peripheral];

If the device already exists in the array, it is not added a second time.

It would be nice if the documentation was clearer on this point. I admit I'm guessing based on how the parameter is used in context.

查看更多
登录 后发表回答