I am creating an iOS application that connects to a bluetooth headset (BLE).
- I search & connect the headset to my iPhone
- I pair the device with my iPhone
- I open my application, it searches for bluetooth devices
- When the application finds my device, it requests to connect to it.
- The iOS pops up a message that asks the user to press the button "Pair now" to connect to the device
Since I have already paired the device before using my application, is there any way to connect without the "Pair now" popup within the application?
--------------- EDIT 1 ---------
I changed my code a bit. I save my device's UUID when I first connect to it and when I reconnect my device the application finds the saved UUID and tries to find the "known peripheral" and reconnect to it. The code actually finds the "known peripheral" but after I try to reconnect to it, it asks again to pair. Is there any way to avoid the "pair now" popup when the device reconnects?
Snippets:
-(void) connectToPeripheral : (CBPeripheral*) peripheral {
[self.centralManager stopScan];
self.peripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
self.peripheral = peripheral;
}
-(void) searchForDevices {
// Scan for all available CoreBluetooth LE devices
if (self.centralManager == nil ) {
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
self.centralManager = centralManager;
}
//check if previous peripheral exists
NSArray *knownPeripherals = nil;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* knownPeripheralID = [defaults stringForKey:@"knownPeripheralID"];
if ( knownPeripheralID != nil ) {
self.connectedPeripheralUUID = [[NSUUID alloc] initWithUUIDString:knownPeripheralID];
knownPeripherals = [self.centralManager retrievePeripheralsWithIdentifiers:[NSArray arrayWithObjects:self.connectedPeripheralUUID, nil]];
}
if ( knownPeripherals != nil && [knownPeripherals count] > 0 ) {
NSLog(@"knownPeripherals Peripherals");
CBPeripheral *foundPeripheral = [knownPeripherals objectAtIndex:0];
[self connectToPeripheral:foundPeripheral];
} else {
NSArray *connectedPeripherals = [self.centralManager retrieveConnectedPeripheralsWithServices:[NSArray arrayWithObjects:UUID_SERIAL_SERVICE_STR, nil]];
NSLog(@"Connected Peripherals");
if ( connectedPeripherals != nil && [connectedPeripherals count] > 0 ) {
} else {
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
}
}