我试图确定iOS中连接的蓝牙设备,但似乎无法找到。理想情况下,我想生成一个已连接蓝牙设备列表。
我尝试使用“retrieveConnectedPeripheralsWithServices”,但这需要指定要搜索的特定服务。我想生成所有已连接的蓝牙设备列表,而不仅仅是特定服务的蓝牙设备。有没有一种方法可以只搜索所有服务而不必循环遍历所有可能的服务?
有什么想法吗?
我试图确定iOS中连接的蓝牙设备,但似乎无法找到。理想情况下,我想生成一个已连接蓝牙设备列表。
我尝试使用“retrieveConnectedPeripheralsWithServices”,但这需要指定要搜索的特定服务。我想生成所有已连接的蓝牙设备列表,而不仅仅是特定服务的蓝牙设备。有没有一种方法可以只搜索所有服务而不必循环遍历所有可能的服务?
有什么想法吗?
(感谢Larme)
NSArray *connectedAccessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories];
文档:
此外,如果有人需要,这是Mac的文档:和 Mac 相关的代码片段
NSArray *devices = [IOBluetoothDevice pairedDevices];
http://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor
BLE代码片段如下:
// In this case you need to tell UUID for serching specific device
CBUUID *hrate = [CBUUID UUIDWithString:@"180D"];
// Create a dictionary for passing down to the scan with service method
NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
// Tell the central manager (cm) to scan for the heart rate service
[cm scanForPeripheralsWithServices:[NSArray arrayWithObject:hrate] options:scanOptions]
这里有一个有趣的段落:
明智地探索外围设备数据 当您开发一个满足特定用例的应用程序时,外围设备可能具有比您感兴趣的服务和特征更多。发现外围设备的所有服务和相关特征可能会对电池寿命和应用程序性能产生负面影响。因此,您应该仅查找和发现应用程序所需的服务和相关特征。
例如,假设您连接到一个可用服务众多的外围设备,但您的应用程序只需要访问其中两个服务。您可以通过将其服务UUID的数组(由CBUUID对象表示)传递给CBPeripheral类的discoverServices:方法,仅查找并发现这两个服务,如下所示:
[peripheral discoverServices:@[firstServiceUUID, secondServiceUUID]];
EAAccessoryManager.shared().connectedAccessories
let devices = IOBluetoothDevice.pairedDevices()
// In this case you need to tell UUID for serching specific device
let hrate = CBUUID(string: "180D"),
// Create a dictionary for passing down to the scan with service method
let scanOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: false)]
// Tell the central manager (cm) to scan for the heart rate service
cm.scanForPeripherals(withServices: [hrate] as? [CBUUID], options: scanOptions)
peripheral.discoverServices([firstServiceUUID, secondServiceUUID])
因此,要检索外设,您需要同时考虑这两个方面。首先,您需要检查它是否在从retrieveConnectedPeripherals(withServices:)返回的外设中。如果没有,则应该使用scanForPeripherals进行扫描。
外设可能已经连接到系统中(例如,iOS会自动连接某些外设以显示电池电量)。在这种情况下,外设将不会广播,并且使用scanForPeripherals进行检测将无法工作。
外设已配对,但未连接。在这种情况下,retrieveConnectedPeripherals(withServices:)无法工作。