繁体   English   中英

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

[英]Possible to generate a list of connected bluetooth devices for iOS?

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

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

有任何想法吗?

iOS的解决方案:

(谢谢拉梅)

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:@"1800"];

// 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"

在这个问题下:

如何获取可用蓝牙设备的列表?

斯威夫特 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: "1800"),
// 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])

您需要考虑两种情况:

  1. 外围设备可能已经在系统中连接(iOS 自动与一些外围设备连接,例如显示电池电量)。 在这种情况下,外围设备将不会广播,并且使用 scanForPeripherals 进行检测将不起作用。

  2. 外围设备已配对,但已断开连接。 在这种情况下,retrieveConnectedPeripherals(withServices:) 将不起作用。

因此,要检索您的外围设备,您需要将两者结合起来。 首先,您需要检查它是否在从retrieveConnectedPeripherals(withServices:) 返回的外设中。 如果不是,您应该 scanForPeripherals。

如果您想检索超出范围的外设,您可以尝试使用retrievePeripherals(withIdentifiers:),但是它也可能返回未配对的设备,并且它依赖于您必须在配对后保存的外设的UUID。

检测外设是否配对有一种方法可以检测特定外设是否配对。 您需要尝试从受保护的特征中读取(这需要加密 - 绑定)。 如果您收到预期数据,则表示用户接受了配对请求。 否则,您将收到空响应或没有响应。

暂无
暂无

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

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