【发布时间】:2017-07-27 07:43:08
【问题描述】:
我有一些蓝牙设备可以连接到 Android 手机,但是我无法检测到断开连接。除非需要,否则蓝牙设备不会发送数据包,因此不能选择在数据包接收时使用看门狗来检测断开连接。我读过你可以使用 ACLDisconnected 广播,但这个事件永远不会为我触发(我已经等了几分钟)。在 Android 6 中检测断开连接的可靠方法是什么?
这是我的 AclDisconnect 注册代码:
_filter = new IntentFilter();
_filter.AddAction(BluetoothDevice.ActionFound);
_filter.AddAction(BluetoothDevice.ActionBondStateChanged);
_filter.AddAction(BluetoothAdapter.ActionDiscoveryStarted);
_filter.AddAction(BluetoothDevice.ActionAclDisconnected);
_filter.AddAction(BluetoothDevice.ActionAclDisconnectRequested);
context.RegisterReceiver(_bluetoothDeviceReceiver, _filter);
还有回调(在断开连接时不会触发)
public override void OnReceive(Context context, Intent intent)
{
string action = intent.Action;
if (action == BluetoothDevice.ActionAclDisconnected || action == BluetoothDevice.ActionAclDisconnectRequested)
{
Interlocked.CompareExchange(ref Disconnected, null, null)?.Invoke();
}
}
【问题讨论】:
-
BluetoothGattCallback 有回调吗?
-
蓝牙 Gatt 与低能量有关。 SPP 代表串行端口协议,它是蓝牙经典的一部分。