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:Controller.serviceUUID] options:scanOptions]
No problem there, I find the peripheral and are able to connect to it. I also give it the parameter to not scan for more than one peripheral (CBCentralManagerScanOptionAllowDuplicatesKey FALSE), 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");
}
}
Some times I get
Discovered
Already discovered
as output in my console, and most of the times only the `Discovered` message shows.
In my peripherals delegate I have
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
NSLog(@"Did discover services on peripheral %@", [peripheral name]);
for (CBService *s in [peripheral services]){
// DOES NOT ENTER THIS LOOP WHEN didDiscoverPeripheral FIRE TWICE
if ([[s UUID] isEqual:Controller.serviceUUID]){
NSLog(@"Found service on %@", [peripheral name]);
self.service = s;
[peripheral discoverCharacteristics:[NSArray arrayWithObject:Controller.throttleCharacteristicUUID] forService:self.service];
}
}
if (self.service == nil){ // THIS HAPPENS WHEN didDiscoverPeripheral FIRE TWICE
[delegate didDiscoverCharacteristic:NO];
}
}
When both occur (didDiscover twice) I am unable to establish the connection, since the for loop is not running as expected (self.service = nil). It works if I reboot the phone again.
I could also wipe out all stored data from previous connections before running my application, and as far as I know, this can only be done by rebooting the phone. Is there any other way, preferably programatically, to do this? I desperately need to figure this out.
Thank you very much in advance
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Bluetooth-dev mailing list (email@hidden)
This email sent to email@hidden