3

i'm currently using CoreBluetooth to scan for peripheral that i have connected before with specific identifier then only connect to the peripheral. I know i can do this by scan for peripheral with service, then compare with the identifier if the condition meet then connect to the peripheral as the code below

func discoverDevices(){
    centralManager.scanForPeripheralsWithServices([CBUUID(string: "1234")], options: nil)
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
    if peripheral.identifier == NSUUID(UUIDString: "AD4612DG87-7512-683D-79RT-234DFG987RUI{
    self.centralManager.connectPeripheral(peripheral, options: nil)
    }
}

is there any other proper way to directly scan for the specific peripheral? Thank you

wes. i
  • 627
  • 9
  • 26
  • https://developer.apple.com/library/ios/documentation/CoreBluetooth/Reference/CBCentralManager_Class/#//apple_ref/occ/instm/CBCentralManager/retrievePeripheralsWithIdentifiers: – Paulw11 Jan 29 '16 at 07:38

1 Answers1

1

CoreBluetooth does not allow you to scan directly for a specific peripheral, as far as I know.

However you can check if it's already a known peripheral to the system by calling retrievePeripheralsWithIdentifiers:.

You only need to scan once and keep a reference to the CBPeripheral object after discovering it. Then you can request a connection to it later using connectPeripheral:options:

p2pkit
  • 1,159
  • 8
  • 11