可以为 iOS 生成连接的蓝牙设备列表吗? [英] Possible to generate a list of connected bluetooth devices for iOS?

查看:33
本文介绍了可以为 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 代码片段是:

//在这种情况下,您需要告诉 UUID 以搜索特定设备CBUUID *hrate = [CBUUID UUIDWithString:@180D"];//创建一个字典,用于传递给使用服务方法的扫描NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];//告诉中央管理器(cm)扫描心率服务[cm scanForPeripheralsWithServices:[NSArray arrayWithObject:hrate] options:scanOptions]

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

.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html

这是一个有趣的段落:

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

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

[peripheraldiscoverServices:@[firstServiceUUID, secondServiceUUID]];

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

还有这个评论:

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

在这个问题下:

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

斯威夫特 5.3

EAAccessoryManager.shared().connectedAccessories让设备 = IOBluetoothDevice.pairedDevices()//在这种情况下,您需要告诉 UUID 以搜索特定设备让 hrate = CBUUID(string: "180D"),//创建一个字典,用于传递给使用服务方法的扫描让 scanOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : NSNumber(value: false)]//告诉中央管理器(cm)扫描心率服务cm.scanForPeripherals(withServices: [hrate] as? [CBUUID], options: scanOptions)外设.发现服务([firstServiceUUID,secondServiceUUID])

I'm trying to determine what devices are connected via bluetooth in iOS but can't seem to figure it out. Ideally I'd like to generate a list of connected bluetooth devices.

I've tried using "retrieveConnectedPeripheralsWithServices" but this requires a specific service to search for. I'd like to generate a list of all connected bluetooth devices, not just specific-service bluetooth devices. Is there a way to just search for all services without looping through all possible services?

Any ideas?

解决方案

The Solution for iOS:

(Thank you Larme)

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

documentation :

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

Also if someone needs, this is documentation for Mac :

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

and code snippet for Mac

NSArray *devices = [IOBluetoothDevice pairedDevices];

For alan478's BLE question :

The Core Bluetooth framework provides the classes needed for your iOS and Mac apps to communicate with devices that are equipped with Bluetooth low energy wireless technology. You can take a look this tutorial :

http://www.raywenderlich.com/52080/introduction-core-bluetooth-building-heart-rate-monitor

and BLE code snippet is :

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

Please read this document on developer.apple.com :

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

here is an interesting paragraph for you :

Explore a Peripheral’s Data Wisely A peripheral device may have many more services and characteristics than you may be interested in when you are developing an app to fulfill a specific use case. Discovering all of a peripheral’s services and associated characteristics can negatively affect battery life and your app’s performance. Therefore, you should look for and discover only the services and associated characteristics your app needs.

For example, imagine that you are connected to a peripheral device that has many services available, but your app needs access to only two of them. You can look for and discover these two services only, by passing in an array of their service UUIDs (represented by CBUUID objects) to the discoverServices: method of the CBPeripheral class, like this:

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

After you have discovered the two services you are interested in, you can similarly look for and discover only the characteristics of these services that you are interested in. Again, simply pass in an array of the UUIDs that identify the characteristics you want to discover (for each service) to the discoverCharacteristics:forService: method of the CBPeripheral class.

Also there is this comment :

"think Apple forbids this thing. We can only get list of Devices with specific CBUUID. so if you want to list all the devices(same as the Bluetooth settings does natively) then It is not possible. Please correct me if i am wrong. – Mrug Mar 11 at 13:24"

under this question :

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 生成连接的蓝牙设备列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆