广播如果一个蓝牙设备连接/断开?蓝牙、设备

2023-09-03 22:01:52 作者:记得善良

我想开始一种服务,当特定BluetoothDevice类连接,并停止该服务时,它的断开。 很显然,我不希望有一个后台服务,如果一个蓝牙设备连接,就是始终检查,所以我想用一个接收器来实现这一点。

这是实际可能吗? android.bluetooth.device.action.ACL_CONNECTED 中提到here,但显然没有奏效。

谢谢!

解决方案

 <意向滤光器> <作用机器人:名称=android.bluetooth.device.action.ACL_CONNECTED/>
    <作用机器人:名称=android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED/>
    <作用机器人:名称=android.bluetooth.device.action.ACL_DISCONNECTED/>
&所述; /意图滤光器>
 

这些都是你必须与你的撒施接收器添加过滤器。

ACL_CONNECTED 信号,蓝牙连接,当 ACL_DISCONNECTED 蓝牙信号断线

有关你必须检查的意图/背景广播接收器专用设备

两个权限你要广告是

 <使用-权限的Andr​​oid:名称=android.permission.BLUETOOTH_ADMIN/>
<使用-权限的Andr​​oid:名称=android.permission.BLUETOOTH/>
 
MacOS上 Bluetooth Low Energy BLE 应用的Swift语言开发笔记

使用此我想,这一定解决你的问题。

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.

Is this actually possible? android.bluetooth.device.action.ACL_CONNECTED is mentioned here, but apparently it didn't work.

Thanks!

解决方案

<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 broad cast receiver.

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

Two permission you have to ad are

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />

Use this I think this must solve your problem