本文介绍了iOS检测蓝牙连接/断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我的应用程序处于后台,也可以在蓝牙设备与iOS连接或断开连接时得到通知吗?在Android上,我使用ACTION_ACL_CONNECTEDACTION_ACL_DISCONNECTED事件.但是我找不到适用于iOS的版本.

Is it possible to be notified when a Bluetooth Device is connected or disconnected from iOS even when my app is in background ?On Android, I use the ACTION_ACL_CONNECTED and ACTION_ACL_DISCONNECTED events. But I cannot find equivalents for iOS.

我找到了可用于监视蓝牙事件的CBCentralManager,但是当连接/断开蓝牙设备时,仅当我启用/禁用蓝牙时,才调用我的功能.是我这方面的错误还是正常?

I found the CBCentralManager that can be used to monitor Bluetooth events, but my functions aren't called when a bluetooth device is connected/disconnected, only when I enable/disable the bluetooth. Is it an error on my side or is it normal ?

我还找到了有关音频路由更改的文档,这也是检测蓝牙连接/断开连接的一个主意.只需检查新路由的种类并在那时检测连接的蓝牙设备即可.

I also found the doc about Audio Route changes, that can also be an idea to detect the bluetooth connections/disconnections. Just check the kind of new route and detect the connected bluetooth devices at that time.

在文档中,我还发现了NSNotification类型,如IOBluetoothHostControllerXXX,但没有任何解释.有人已经使用过它们吗?

In the doc, I also found NSNotification types like IOBluetoothHostControllerXXX but nothing is explained about them. Did someone already used them ?

还有更好的东西吗?还是我缺少什么?

Is there something better or am I missing something ?

推荐答案

通常您不会收到有关蓝牙外围设备连接和断开的通知.您可以获取您的应用连接到的BLE外围设备的连接和断开连接事件.

You cannot receive notifications about the connection and disconnection of Bluetooth peripherals generally. You can get connection and disconnection events for BLE peripherals that your app connects to.

例如,如果您的应用启动了与心率传感器的连接,则连接成功后,您将获得对CBCentralManagerDelegate连接功能的调用.如果该设备随后被关闭或超出范围,那么您将获得对断开连接委托方法的调用.

For example, if your app initiates a connection to a heart rate sensor then you will get a call to your CBCentralManagerDelegate connection function when the connection succeeds. If that device subsequently is switched off or goes out of range then you will get a call to the disconnection delegate method.

如果其他一些应用启动并建立了连接,那么您将不会得到回调.

If some other app initiates and makes the connection then you will not get a callback.

如前所述,您可以监视音频路由更改以推断已连接/断开了蓝牙音频设备,但是插入耳机时也会触发.

As you mentioned, you can monitor audio route changes to infer that a Bluetooth audio device has been connected/disconnected, but this will also fire when headphones are plugged in.

这篇关于iOS检测蓝牙连接/断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:24