我想在连接特定蓝牙设备时启动一项服务,并在断开连接时停止该服务。 显然,我不希望后台服务始终检查 BT 设备是否已连接,因此我想使用 Receiver 来实现这一点。
这真的可能吗? android.bluetooth.device.action.ACL_CONNECTED
被提及 here , 但显然它没有用。
谢谢!
最佳答案
<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>
这些是您必须添加到广播接收器的过滤器。
ACL_CONNECTED
蓝牙连接时发出信号,ACL_DISCONNECTED
蓝牙断开时发出信号
对于特定设备,您必须检查广播接收器中的 Intent/上下文
您必须添加的两个权限是:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
我认为这应该可以解决您的问题。
关于android - 如果蓝牙设备正在连接/断开连接则广播?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8006407/