简体   繁体   English

如何在iOS中获得配对BLE设备?

[英]How to get Paired BLE device in iOS?

I connect BLE with CoreBluetooth and paired. 我将BLE与CoreBluetooth连接并配对。

Now when I back to my app screen, I want to make sure that BLE already paired with iOS device. 现在当我回到我的应用程序屏幕时,我想确保BLE已经与iOS设备配对。

  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? 如何在应用程序中获得配对的BLE设备?

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral. 第一次发现外设时,系统会生成一个标识符(UUID,由NSUUID对象表示)以标识外设。

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. 然后,您可以存储此标识符(例如,使用NSUserDefaults类的资源),稍后使用它来尝试使用CBCentralManager类的retrievePeripheralsWithIdentifiers:方法重新连接到外围设备。

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. NSUUID类与CoreFoundation的CFUUID没有免费桥接。 Use UUID strings to convert between CFUUIDRef and NSUUID, if needed. 如果需要,使用UUID字符串在CFUUIDRef和NSUUID之间进行转换。 Two NSUUID objects are not guaranteed to be comparable by pointer value (as CFUUID is); 两个NSUUID对象不能保证与指针值相当(如CFUUID所示); use isEqual(_:) to compare two NSUUID instances. 使用isEqual(_ :)来比较两个NSUUID实例。

Refer this: https://developer.apple.com/documentation/foundation/nsuuid 请参阅: 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 请参阅最后两节: https//developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html#//apple_ref/doc/uid/TP40013257-CH6-SW1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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