[英]How to get Paired BLE device in iOS?
我將BLE與CoreBluetooth連接並配對。
現在當我回到我的應用程序屏幕時,我想確保BLE已經與iOS設備配對。
如果我將值存儲在默認值中並刪除應用程序,則此情況不適用於獲取設備。
如果用戶從設置 - >藍牙 - >設備列表中刪除配對的藍牙外設,則此情況也無法識別。
NSArray *ary = [self.bleMgr retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:@"180A"]]]; NSUUID *nsUUID = [[NSUUID alloc] initWithUUIDString:identifier]; NSArray *temp = [self.bleMgr retrievePeripheralsWithIdentifiers:@[nsUUID]];
在兩個代碼行之上沒有給出穩健的結果。
如何在應用程序中獲得配對的BLE設備?
第一次發現外設時,系統會生成一個標識符(UUID,由NSUUID對象表示)以標識外設。
然后,您可以存儲此標識符(例如,使用NSUserDefaults類的資源),稍后使用它來嘗試使用CBCentralManager類的retrievePeripheralsWithIdentifiers:方法重新連接到外圍設備。
下面介紹了使用此方法重新連接到先前連接的外圍設備的一種方法。
knownPeripherals = [myCentralManager retrievePeripheralsWithIdentifiers:savedIdentifiers];
NSUUID類與CoreFoundation的CFUUID沒有免費橋接。 如果需要,使用UUID字符串在CFUUIDRef和NSUUID之間進行轉換。 兩個NSUUID對象不能保證與指針值相當(如CFUUID所示); 使用isEqual(_ :)來比較兩個NSUUID實例。
請參閱: https : //developer.apple.com/documentation/foundation/nsuuid
請參閱最后兩節: 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.