ios - 可以为 iOS 生成连接的蓝牙设备列表吗?

标签 ios swift macos bluetooth

我试图确定哪些设备通过 iOS 中的蓝牙连接,但似乎无法弄清楚。理想情况下,我想生成一个已连接蓝牙设备的列表。

我试过使用“retrieveConnectedPeripheralsWithServices”,但这需要特定的服务来搜索。我想生成所有连接的蓝牙设备的列表,而不仅仅是特定服务的蓝牙设备。有没有一种方法可以只搜索所有服务而无需遍历所有可能的服务?

有什么想法吗?

最佳答案

iOS 解决方案:

(谢谢你 Larme)

NSArray *connectedAccessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories]; 

文档:

https://developer.apple.com/library/prerelease/ios/documentation/ExternalAccessory/Reference/EAAccessoryManager_class/index.html#//apple_ref/occ/instp/EAAccessoryManager/connectedAccessories

此外,如果有人需要,这是 Mac 的文档:

https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/Bluetooth/BT_Intro/BT_Intro.html

和 Mac 的代码片段

NSArray *devices = [IOBluetoothDevice pairedDevices];

对于 alan478 的 BLE 问题:

Core Bluetooth 框架提供了 iOS 和 Mac 应用程序与配备蓝牙低功耗无线技术的设备进行通信所需的类。你可以看看这个教程:

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]

请阅读 developer.apple.com 上的此文档:

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html

这里有一个有趣的段落:

明智地探索外围设备的数据 在开发满足特定用例的应用程序时,外围设备可能具有比您感兴趣的更多的服务和特性。发现外围设备的所有服务和相关特征可能会对电池生命周期和应用程序的性能产生负面影响。因此,您应该只查找和发现您的应用所需的服务和相关特征。

例如,假设您连接到具有许多可用服务的外围设备,但您的​​应用只需要访问其中的两个。您只能查找和发现这两个服务,方法是将它们的服务 UUID 数组(由 CBUUID 对象表示)传递给 CBPeripheral 类的 discoverServices: 方法,如下所示:

[peripheral discoverServices:@[firstServiceUUID, secondServiceUUID]];

在您发现您感兴趣的两个服务后,您可以类似地查找和发现您感兴趣的这些服务的特征。同样,只需传入一个标识您想要的特征的 UUID 数组发现(针对每个服务)CBPeripheral 类的 discoverCharacteristics:forService: 方法。

还有这样的评论:

“认为 Apple 禁止这件事。我们只能获得具有特定 CBUUID 的设备列表。因此,如果您想列出所有设备(与本地蓝牙设置相同),那么这是不可能的。请纠正我,如果我我错了。- Mrug 3 月 11 日 13:24"

在这个问题下:

How to get list of available Bluetooth devices?

swift 5.3

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])

关于ios - 可以为 iOS 生成连接的蓝牙设备列表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30636637/

相关文章:

macos - MacOSX:哪些动态库通过二进制链接?

ios - 在另一个上下文中更新核心数据实体

iphone - 能够在 Facebook 本地共享对话中标记 friend

swift - 从 ViewController 访问 NSDocument

ios - 如何使用事件 channel 将数据从 swift 流式传输到 flutter?

python - 删除某个版本的 python 包

ios 自定义 UIButton 缩放 setImage 向下不起作用

ios UIImage 超出 UIImageView 边框

swift - 尝试在 TableView 中显示图像时出错

python - 无法在 macos 上使用 pyenv 安装 python 版本