我需要获得与 iOS 设置中“蓝牙”部分中的列表相同的配对蓝牙设备(iOS 设备)列表,如下图所示。
可能吗?
您是否见过任何执行此类功能的应用程序?
我尝试了以下方法:
link1、link2、link3、link4、link5、link6
但是没有什么能帮助我清楚地获得确切的清单。我希望应该有一种方法来实现这一点。请通过分享您的经验来帮助我。谢谢你。
无法从 iOS 检索配对外围设备列表。也无法检查特定外围设备是否已配对。
有两种情况需要考虑:
外围设备可能已经连接到系统中(iOS 会自动连接一些外围设备,例如显示电池电量)。在这种情况下,外围设备将不会广播并且检测使用scanForPeripherals
将不起作用。
外围设备已配对,但已断开连接。在这种情况下retrieveConnectedPeripherals(withServices:)
是行不通的。
因此,要检索您的外围设备,您需要将两者结合起来。首先,您需要检查它是否在从retrieveConnectedPeripherals(withServices:)
. 如果不是,你应该scanForPeripherals
。
如果您想检索超出范围的外围设备,您可以尝试使用retrievePeripherals(withIdentifiers:)
,但它也可能返回未配对的设备,并且它依赖于UUID
您必须在配对后保存的外围设备。
有一种方法可以检测特定外围设备是否已配对。您需要尝试从受保护的特征(需要加密 - 绑定)中读取。如果您收到预期的数据,则表示用户接受了配对请求。否则,您将收到空响应或没有响应。
您可以在我的文章中阅读有关蓝牙的更多信息:
ShowBluetoothAccessoryPicker
如果您将 Classic Bluetooh 用于 MFi 设备,则可以使用。
下面的代码是 C#,但您可以轻松地将其转换为 IOS(对象 c 你的 swift)
EAAccessoryManager.SharedAccessoryManager.ShowBluetoothAccessoryPicker(null, completion: ((Foundation.NSError error) => {
Console.WriteLine("My callback");
if (error != null && (uint)error.Code != (uint)EABluetoothAccessoryPickerError.AlreadyConnected)
{
Console.WriteLine(String.Format("Error code: {0} Desc: {1}", error.Code, error.DebugDescription));
Console.WriteLine("Failed? " + EABluetoothAccessoryPickerError.Failed.ToString());
Console.WriteLine("Failed? " + Convert.ToInt64(EABluetoothAccessoryPickerError.Failed));
}
}));
您需要找到您感兴趣的服务 UUID,在我的情况下它可以完美运行,
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:SERVICE_UUID]]
options:options];
当它找到任何宣传相同服务 UUID 的设备时,它将出现在您上面指出的屏幕中。
这样处理 didDiscoverperipherel :
-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
_discoveredPeripheral = peripheral;
if(![self.mRemoteDevices containsObject:_discoveredPeripheral])
{
NSArray *peripherels = [self.centralManager retrievePeripheralsWithIdentifiers:@[_discoveredPeripheral.identifier]];
[self.mRemoteDevices addObject:[peripherels objectAtIndex:0]];
}
}