简体   繁体   中英

How to get Paired BLE device in iOS?

I connect BLE with CoreBluetooth and paired.

Now when I back to my app screen, I want to make sure that BLE already paired with iOS device.

  1. If I store value in defaults and remove app, this case will not work to fetch device.

  2. If user remove paired bluetooth peripheral from Setting -> Bluetooth -> list of devices this case also not work to identify.

     NSArray *ary = [self.bleMgr retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:@"180A"]]]; NSUUID *nsUUID = [[NSUUID alloc] initWithUUIDString:identifier]; NSArray *temp = [self.bleMgr retrievePeripheralsWithIdentifiers:@[nsUUID]]; 

Above both code lines not giving robust result.

How to get that paired BLE device in app?

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral.

You can then store this identifier (using, for instance, the resources of the NSUserDefaults class), and later use it to try to reconnect to the peripheral using the retrievePeripheralsWithIdentifiers: method of the CBCentralManager class.

The following describes one way to use this method to reconnect to a peripheral you've previously connected to.

knownPeripherals = [myCentralManager retrievePeripheralsWithIdentifiers:savedIdentifiers];

The NSUUID class is not toll-free bridged with CoreFoundation's CFUUID. Use UUID strings to convert between CFUUIDRef and NSUUID, if needed. Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUID is); use isEqual(_:) to compare two NSUUID instances.

Refer this: https://developer.apple.com/documentation/foundation/nsuuid

Refer last two sections: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html#//apple_ref/doc/uid/TP40013257-CH6-SW1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM