核心蓝牙。如何检查外围设备已经连接?
在开发期间,我遇到了与连接中心和外围关系有关的问题。当一个已经与perepheral相连的中央之一时,我必须不允许连接其他中央。也许有人现在如何检查外围已经连接了?
During development I got a problem witch related to the connection central and peripheral. When one of the central already connected with perepheral I have to don't allow to connect other central. Maybe someone nows how can I check does the peripheral already connected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
致电 noreflow noreferrer“>获取给定服务UUID列表的当前连接外围设备的列表。这些连接到手机,而不是与您的应用程序连接。您仍然需要调用
连接
将它们连接到当前运行的应用程序(就像发现它们一样)。此通话是同步的,因为OS已经知道所有连接的设备,并且可以返回它们。通常,此列表为空。最常见的生产原因是非空的,是另一个应用程序已连接到该设备(例如LightBlue或其他蓝牙扫描仪)。但是,在开发过程中,该应用程序被杀死非常普遍,在这种情况下,当您重新启动应用程序时,手机可能还没有暂停连接,这将解决这种情况。
虽然您可以通过将
nil
传递给scanforperipherals
来扫描所有设备,但没有等效的方法来检索“所有连接的设备”,您必须列出所需的服务UUID。在您的情况下,我不认为这应该是一个问题。Call
retrieveConnectedPeripherals(withServices:)
to get a list of currently-connected peripherals for a given list of service UUIDs. These are connected to the phone, not to your app. You still need to callconnect
to connect them the currently running app (exactly like you would if you discovered them). This call is synchronous, since the OS already knows all the connected devices and can just return them.Typically this list is empty. The most common production reason for it to be non-empty is that another app has connected to the device (LightBlue or some other Bluetooth scanner for example). During development, however, it is very common that the app gets killed, and in that case the phone may not have dropped the connection yet when you relaunch the app, and this will handle that situation.
While you can scan for all devices by passing
nil
toscanForPeripherals
, there is no equivalent way to retrieve "all connected devices" You must list the desired service UUIDs. I don't believe this should be a problem in your case.如果您只是想知道外围是否连接到中央,则可以简单地检查其状态。您可以在发现期间立即执行此操作。
If you simply want to know whether a peripheral is connected to a central, then you can simply check its state. You can do this immediately during discovery.