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

ala*_*478 11 macos bluetooth ios swift

我正在尝试确定iOS中通过蓝牙连接的设备,但似乎无法弄明白.理想情况下,我想生成一个连接的蓝牙设备列表.

我尝试过使用"retrieveConnectedPeripheralsWithServices",但这需要一个特定的服务来搜索.我想生成所有连接蓝牙设备的列表,而不仅仅是特定服务的蓝牙设备.有没有办法只搜索所有服务而不循环所有可能的服务?

有任何想法吗?

mgy*_*yky 5

这是iOS的解决方案(谢谢Larme):

NSArray *connectedAccessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories]; 
Run Code Online (Sandbox Code Playgroud)

说明文件:

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];
Run Code Online (Sandbox Code Playgroud)

对于alan478的BLE问题:

核心蓝牙框架提供了您的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]
Run Code Online (Sandbox Code Playgroud)

请在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]];
Run Code Online (Sandbox Code Playgroud)

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

也有此评论:

“认为Apple禁止使用此设备。我们只能获取具有特定CBUUID的设备列表。因此,如果您要列出所有设备(与Bluetooth设置相同),则不可能。如果我错了,请更正我。 – Mrug 3月11日13:24“

在这个问题下:

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

  • Gyer,我感谢您的所有帮助和指导,但仍在努力解决这个问题...这似乎是在特定应用程序中使用蓝牙的好方法(搜索、发现、连接等),但是有什么方法可以生成**已经**连接到 iOS 设备的设备列表,即查找通过“设置”>“蓝牙”连接的设备?我感兴趣的设备可能已经连接。有什么方法可以通过我们自己的应用程序与那些系统连接的设备进行交互吗? (2认同)