当前位置: 首页 > news >正文

【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 会自动断连,此时就可以 用于发起 重连尝试。

http://www.lryc.cn/news/620049.html

相关文章:

  • 如何理解vue组件失活与激活及导航全流程
  • 免费专业PDF文档扫描效果生成器
  • PHP imagick扩展安装以及应用
  • GISBox工具处理:将高斯泼溅模型导出为3DTiles
  • 【Android】四种不同类型的ViewHolder的xml布局
  • transforms的使用 小土堆pytorch记录
  • 专题:2025抖音电商与微短剧行业研究报告|附150+份报告PDF汇总下载
  • MATLAB 绘图速查笔记
  • 如何通过 Actor 网络压缩为概率分布实现
  • 使用 python-pptx 完成 ppt 页面的复制
  • 终端安全检测与防御
  • Open3d:从mesh中采样点云的两个函数
  • round robin轮询仲裁器
  • 2025 开源语音合成模型全景解析:从工业级性能到创新架构的技术图谱
  • hutool 作为http 客户端工具调用的一点点总结
  • 理解RESTful架构:构建优雅高效的Web服务
  • 《Unity Shader入门精要》学习笔记一
  • Dimensional Analysis量纲分析入门
  • 【Excel】被保护的文档如何显示隐藏的行或列
  • MongoDB 入门指南二:索引 —— 让查询速度飞起来
  • 随想记-excel报表美化
  • 选择排序专栏
  • 使用 6 种方法将文件从 Android 无缝传输到iPad
  • C# 反射和特性(获取Type对象)
  • 攒钱学概论:5、创业术
  • window显示驱动开发—DirectX 9 资源创建
  • 《AVL树的原理与C++实现:详解平衡二叉搜索树的高效构建与操作》
  • 【自动化运维神器Ansible】playbook主机清单变量深度解析:主机变量与组变量的实战应用
  • JavaWeb-Servlet基础
  • CodeBuddy在AI开发方面的一些特色