当前位置: 首页 > 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.dtcms.com/a/328752.html

相关文章:

  • 【KO】Android 面试高频词
  • 从内核数据结构的角度理解socket
  • Android Activity 的对话框(Dialog)样式
  • RxJava 在 Android 中的深入解析:使用、原理与最佳实践
  • 基于Apache Flink的实时数据处理架构设计与高可用性实战经验分享
  • 【cs336学习笔记】[第5课]详解GPU架构,性能优化
  • 深入 Linux 线程:从内核实现到用户态实践,解锁线程创建、同步、调度与性能优化的完整指南
  • iscc2025区域赛wp
  • 服务器通过生成公钥和私钥安全登录
  • Android 在 2020-2025 都做哪些更新?
  • 如何提供对外访问的IP(内网穿透工具)
  • 【Android】ChatRoom App 技术分析
  • OpenAI 回应“ChatGPT 用多了会变傻”
  • Control Center 安卓版:个性化手机控制中心
  • ClickHouse从入门到企业级实战全解析课程简介
  • 1688商品数据抓取:Python爬虫+动态页面解析
  • 基于elk实现分布式日志
  • Windows11 运行IsaacSim GPU Vulkan崩溃
  • 三极管的基极为什么需要下拉电阻
  • Pycharm选好的env有包,但是IDE环境显示无包
  • Excel多级数据结构导入导出工具
  • Nuxt 3 跨域问题完整解决方案(开发 + 生产环境)
  • Appium-移动端自动测试框架详解
  • 【MCP开发】Nodejs+Typescript+pnpm+Studio搭建Mcp服务
  • 【数据可视化-88】航空公司航班数据分析与可视化:Python + pyecharts洞察航空旅行趋势
  • 通用安全指南
  • 关于在img标签的src里面直接使用“~/assets/images/xxx“可以,但是若将这个路径写成变量的形式就会报错
  • Java Stream API 中常用方法复习及项目实战示例
  • BGP综合实验_Te. BGP笔记
  • 七大排序算法全解析:从入门到精通