如果蓝牙设备正在连接/断开连接,则广播? [英] Broadcast if a bluetooth device is connecting/disconnecting?
问题描述
我想在连接特定蓝牙设备时启动服务,并在断开连接时停止该服务.显然,我不想让后台服务总是检查是否连接了 BT 设备,所以我想用接收器来实现这一点.
I want start a service, when a specific bluetoothdevice is connected and stop this service when it's disconnected. Obviously I don't want to have a background service that is always checking if a BT device is connected, so I want to achieve this with a Receiver.
这真的可能吗?android.bluetooth.device.action.ACL_CONNECTED
在此处被提及,但显然没有用.
Is this actually possible? android.bluetooth.device.action.ACL_CONNECTED
is mentioned here, but apparently it didn't work.
谢谢!
推荐答案
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>
这些是您必须为广播接收器添加的过滤器.
These are the filters you have to add with your broadcast receiver.
ACL_CONNECTED
表示蓝牙连接,ACL_DISCONNECTED
表示蓝牙断开
ACL_CONNECTED
signals when bluetooth is connected and ACL_DISCONNECTED
signals bluetooth disconnection
对于特定设备,您必须在广播接收器中检查意图/上下文
For specific device you have to check intents/context in broadcast receiver
您必须添加的两个权限是:
The two permission you have to add are:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
我认为这应该可以解决您的问题.
I think this should solve your problem.
这篇关于如果蓝牙设备正在连接/断开连接,则广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!