如何获得swift中配对的蓝牙设备列表? [英] How get the list of paired bluetooth devices in swift?

查看:1459
本文介绍了如何获得swift中配对的蓝牙设备列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将配对的蓝牙设备(iOS设备)列表与iOS设置中蓝牙部分的列表相同,如下图所示。

I need to get the list of paired bluetooth devices(iOS Devices) as same as the list in 'Bluetooth' section in iOS settings as shown in below picture.





有可能吗?

您是否看过任何使用这种功能的应用程序?< br>

我尝试过以下方法:
link1 link2 link3 link4 link5 link6

但没有任何帮助我清楚地得到确切的清单。我希望应该有办法实现这一目标。请分享您的经验,帮助我。
谢谢。

But nothing helped me clearly to get the exact list. I hope there should be a way to achieve this. Please help me by sharing your experience. Thank you.

推荐答案

无法从iOS检索配对的外围设备列表。无论是否可以检查特定外设是否配对。

It's not possible to retrieve list of paired peripherals from iOS. Neither it's possible to check if specific peripheral is paired.

有两种情况需要考虑:


  1. 系统中已连接外围设备(iOS会自动连接某些外围设备以显示电池电量)。在这种情况下,外围设备将不会使用 scanForPeripherals 进行广播和检测。

外围设备配对,但断开连接。在这种情况下, retrieveConnectedPeripherals(withServices :) 将无效。

Peripheral is paired, but disconnected. In this case retrieveConnectedPeripherals(withServices:) won't work.

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

Therefore to retrieve your peripheral you need to combine both things. First you need to check if it's in peripherals returned from retrieveConnectedPeripherals(withServices:). If not you should scanForPeripherals.

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

If you want to retrieve peripheral which is out of range, you can try to use retrievePeripherals(withIdentifiers:), however it may return also not paired devices and it relies on peripheral's UUID which you have to save after pairing.

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

There is one way to detect if the specific peripheral is paired. You need to try to read from protected characteristic (which requires encryption - bonding). If you receive expected data, it means that user accepted pairing request. Otherwise you will receive empty response or none.

您可以在我的文章中阅读有关蓝牙的更多信息:

You can read more about Bluetooth in my articles:

  • Original answer
  • Swift – Bluetooth Low Energy communication using Flow Controllers
  • How to communicate with Bluetooth Low Energy devices on iOS
  • Bluetooth Low Energy – background mode on iOS

这篇关于如何获得swift中配对的蓝牙设备列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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