The process of reconnecting to known peripherals is described in the Bluetooth Core Programming Guide .
Essentially, if you know the UUID of the device you want to connect to (this is the identifier property of the CBPeripheral object), you can use the retrievePeripheralsWithIdentifiers: method for CBCentralManager to get CBPeripheral , and then try to establish a connection -
NSUUID *pid=[[NSUUID alloc]initWithUUIDString:@"45D956BB-75E4-6CEB-C06C-D531B00174E4" ]; NSArray *peripherals=[self.centralManager retrievePeripheralsWithIdentifiers:@[pid]]; if ([peripherals count]>0) { CBPeripheral *peripheral=[peripherals objectAtIndex:0]; peripheral.delegate=self; [self.connectingPeripherals addObject:peripheral]; NSLog(@"Found peripheral %@ with name %@",peripheral.identifier,peripheral.name); [self.centralManager connectPeripheral:peripheral options:nil]; }
This may not work, and you may need to scan your peripheral device as an identifier for some peripheral devices (in particular, iOS devices), periodically changing their UUIDs.
Programming Notes -
Note. The peripheral device may not be available for connection for several reasons. For example, a device cannot be near Central. In addition, some low-power Bluetooth devices use a random device address that changes periodically. Therefore, even if a device is nearby, the device address may have changed since the last time it was detected by the system, in which case the CBPeripheral object you are trying to connect to does not match the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must reopen it using scanForPeripheralsWithServices: options: method.
You will also have to scan the first time you encounter peripherals.
source share