【android bluetooth 协议分析 05】【蓝牙连接详解3】【app侧该如何知道蓝牙设备的acl状态】
一、背景
好多app 开发的朋友,其实不知道这个接口。 我特意分享一下。
今天开会遇到一个问题,在车机的状态栏里面,有个小蓝牙图标,在有设备连接和无设备连接的情况下显示的内容有差异。
- 应用侧 的同事,居然在拿 hfp 有无连接成功,来改变 这个图标的显示状态。
- 但是产品需要 在某种场景下 断开 hfp, 只连 a2dp , 然后这个问题, app 侧的同事就搞不定了。
介于这种情况,那我就分享一下吧!
app 侧该如何获取 当前蓝牙设备的 acl 状态?
二、app 侧
2.1 监听 acl 状态
private final ConnectionCallback mConnectionCallback;private static final int ERROR_DISCONNECT_REASON_TIMEOUT = 1104;class ConnectionCallback extends BluetoothAdapter.BluetoothConnectionCallback {// 当有设备 acl 连接成功后回调@Overridepublic void onDeviceConnected(BluetoothDevice device) {super.onDeviceConnected(device);logi("onDeviceConnected: " + BluetoothUtils.getDeviceDebugInfo(device));}// 当有设备 acl 断开后回调@Overridepublic void onDeviceDisconnected(BluetoothDevice device, int reason) {super.onDeviceDisconnected(device, reason);logi("onDeviceDisconnected: "+ BluetoothUtils.getDeviceDebugInfo(device)+ " reason:" + reason + ":" + disconnectReasonToString(reason));switch (reason) {case ERROR_DISCONNECT_REASON_TIMEOUT: // 1104 表示,远离后自动断开, 这里可以实现 自动尝试重连的逻辑logi("handle timeout.");reconnectDevice(device);break;default:logi("onDeviceDisconnected default no handle.");break;};}public ConnectionCallback() {}};
1. acl 断开的原因
// framework/java/android/bluetooth/BluetoothAdapter.javapublic static String disconnectReasonToString(@DisconnectReason int reason) {switch (reason) {case BluetoothStatusCodes.ERROR_UNKNOWN:return "Reason unknown";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_LOCAL_REQUEST:return "Local request";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_REMOTE_REQUEST:return "Remote request";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_LOCAL:return "Local error";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_REMOTE:return "Remote error";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_TIMEOUT:return "Timeout";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_SECURITY:return "Security";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_SYSTEM_POLICY:return "System policy";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_RESOURCE_LIMIT_REACHED:return "Resource constrained";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_CONNECTION_ALREADY_EXISTS:return "Connection already exists";case BluetoothStatusCodes.ERROR_DISCONNECT_REASON_BAD_PARAMETERS:return "Bad parameters";default:return "Unrecognized disconnect reason: " + reason;}}
2.2 注册 acl 监听
mConnectionCallback = new ConnectionCallback();if (mBluetoothAdapter != null) {mBluetoothAdapter.registerBluetoothConnectionCallback(mContext.getMainExecutor(), mConnectionCallback);logi("registerBluetoothConnectionCallback ok.");} else {loge("registerBluetoothConnectionCallback error.");}
2.3 log
【bt.server acl 连接成功】
08-01 09:28:32.261114 28336 28394 I BluetoothRemoteDevices: aclStateChangeCallback: Adapter State: ON Connected: C0:6C:0C:8F:BF:E6 【app 监听到设备 acl 连接成功】
08-01 09:28:32.263199 2360 2360 D CAR.BluetoothDeviceConnectionPolicy: onDeviceConnected: (addr = C0:6C:0C:8F:BF:E6) 【bt.server acl 断开】
08-01 09:28:35.861677 28336 28394 I BluetoothRemoteDevices: aclStateChangeCallback: Adapter State: ON Disconnected: C0:6C:0C:8F:BF:E6 transportLinkType: 1 hciReason: 19 【app 监听到设备acl 断开】
08-01 09:28:35.863093 2360 2360 D CAR.BluetoothDeviceConnectionPolicy: onDeviceDisconnected: (addr = C0:6C:0C:8F:BF:E6) reason:1101:Remote request
三、应用场景
除了 引言里面的一种场景外, 还有就是 我上面demo 中提到的, 当蓝牙设备 远离车机后,acl 会自动断连,此时就可以 用于发起 重连尝试。