ADB命令实战:手机蓝牙与热点控制的自动化技巧
1. ADB命令入门手机控制的瑞士军刀第一次接触ADB命令时我正被几十台测试手机折磨得焦头烂额。每次手动开关蓝牙和热点要花费半小时直到发现这个藏在Android SDK里的神器。ADBAndroid Debug Bridge就像连接电脑和手机的隐形数据线通过命令行就能完成所有触屏操作。你可能见过测试工程师对着黑乎乎的终端敲代码其实他们正在用ADB完成这些操作批量开关200台手机的蓝牙功能凌晨三点自动开启热点进行压力测试远程控制展示机循环播放演示模式最让我惊喜的是普通用户也能轻松上手。只需要在电脑安装Android SDK Platform Tools手机开启开发者选项和USB调试用数据线连接后执行adb devices验证连接# 基础检查命令 adb devices # 输出示例 List of devices attached emulator-5554 device遇到连接问题时试试这几个排查步骤检查USB线是否支持数据传输很多充电线只有电力传输在开发者选项里撤销USB调试授权后重新连接使用adb kill-server adb start-server重启服务2. 蓝牙控制全攻略从开关到深度管理2.1 基础开关操作测试蓝牙音箱时我总要在设置菜单里来回切换开关。后来发现这两个命令比点按快十倍# 检查蓝牙当前状态 adb shell settings get global bluetooth_on # 返回1表示开启0表示关闭 # 通过系统弹窗请求开启需手动确认 adb shell am start -a android.bluetooth.adapter.action.REQUEST_ENABLE # 通过系统弹窗请求关闭 adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISABLE但弹窗确认很烦人Root设备可以用更暴力的方式adb root adb shell svc bluetooth enable # 无提示强制开启 adb shell svc bluetooth disable # 无提示强制关闭2.2 设备连接与扫描调试智能家居时最头疼的是反复配对设备。这套组合拳帮我节省了80%时间# 开启蓝牙扫描发现新设备 adb shell am startservice -n com.android.bluetooth/.btservice.AdapterService --es command start_discovery # 停止扫描 adb shell am startservice -n com.android.bluetooth/.btservice.AdapterService --es command cancel_discovery # 连接指定设备替换MAC地址 adb shell am startservice -n com.android.bluetooth/.btservice.AdapterService --es command connect --es device 11:22:33:44:55:66 # 断开连接 adb shell am startservice -n com.android.bluetooth/.btservice.AdapterService --es command disconnect --es device 11:22:33:44:55:66获取已配对设备列表有个隐藏技巧adb shell service call bluetooth_manager 6 | grep -Eo [0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}2.3 高级配置技巧做蓝牙 beacon 测试时这些命令成了救命稻草# 设置设备可见时间单位秒 adb shell am start -a android.bluetooth.adapter.action.REQUEST_DISCOVERABLE --ei android.bluetooth.adapter.extra.DISCOVERABLE_DURATION 300 # 获取蓝牙适配器详细信息 adb shell dumpsys bluetooth | grep -A 10 Adapter Properties遇到连接不稳定时我会检查HCI日志adb shell dumpsys bluetooth_manager | grep -i error3. 热点控制秘籍从基础到企业级方案3.1 常规开启方式不同品牌手机的热点设置路径千奇百怪。vivo系手机可以用adb shell am start -n com.android.settings/com.android.settings.Settings$\VivoTetherSettingsActivity但更通用的方法是直接操作系统服务# 开启热点需Root adb shell settings put global tether_dun_required 0 adb shell service call connectivity 33 i32 1 # 关闭热点 adb shell service call connectivity 33 i32 03.2 配置热点参数给公司配置访客网络时这套命令组合特别实用# 设置热点SSIDAndroid 10 adb shell settings put global tether_softap_ssid MyHotspot # 设置密码WPA2加密 adb shell settings put global tether_softap_password 12345678 # 设置最大连接数 adb shell settings put global tether_softap_client_control 5注意部分命令需要系统级权限普通应用需添加权限uses-permission android:nameandroid.permission.TETHER_PRIVILEGED /3.3 企业级自动化方案在设备租赁业务中我们开发了这样的工作流通过ADB预配置热点参数使用Tasker创建定时任务结合AutoInput自动处理系统弹窗用Python脚本批量管理设备群组典型的生产环境代码片段import subprocess def set_hotspot(device_ip, state): cmd fadb -s {device_ip} shell service call connectivity 33 i32 {1 if state else 0} try: subprocess.run(cmd, checkTrue, shellTrue) return True except subprocess.CalledProcessError: return False4. 实战案例自动化测试系统搭建去年为智能家居公司搭建的测试平台核心功能都依赖ADB命令4.1 蓝牙压力测试方案#!/bin/bash for i in {1..100} do adb shell svc bluetooth enable sleep 2 adb shell svc bluetooth disable sleep 2 echo Cycle $i completed done配合这个脚本可以检测蓝牙模块的开关稳定性内存泄漏情况连接中断后的恢复能力4.2 多设备热点接力测试模拟移动场景的典型配置# 设备1开启热点 adb -s 192.168.1.101 shell settings put global tether_softap_ssid TestAP1 # 设备2连接热点 adb -s 192.168.1.102 shell am start -a android.intent.action.MAIN -n com.android.settings/.wifi.WifiSettings adb -s 192.168.1.102 shell input text TestAP1 adb -s 192.168.1.102 shell input keyevent 664.3 异常处理技巧在长时间测试中我总结了这些保命命令# 蓝牙卡死时重置服务 adb shell am stopservice com.android.bluetooth adb shell am startservice com.android.bluetooth # 热点无法关闭时的强制方案 adb shell settings delete global tether_dun_required adb shell reboot记得在关键操作前备份配置adb shell settings list global | grep tether hotspot_backup.txt
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2434323.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!