ADB打开Android机器的蓝牙扫描功能

通过PC控制Android比较理想的方式是Python+UiAutomator,如果条件限制无法使用这种方式,也可以考虑其他方式。本文整理部分实践操作,但在实践项目中,依然强烈推荐Python+UiAutomator的方式!

1. 打开和关闭Bluetooth (注意需要root权限)

adb root
adb shell svc bluetooth enable
adb shell svc bluetooth disable

查询Bluetooth的开关状态

adb shell settings get global bluetooth_on

获取Bluetooth的MAC Address

adb shell settings get secure bluetooth_address

或者
(13 是IBluetoothManager接口中的第13个方法:String getAddress();(注:该值不同平台可能不一样,最终根据IBluetoothManager.aidl确定。 在/system/bt/binder/android/bluetooth/ 或 /frameworks/base/core/java/android/bluetooth/下))

adb shell service call bluetooth_manager 13

2. 打开蓝牙扫描页面

adb shell am start -n com.android.settings/.bluetooth.DevicePickerActivity

或是如下命令,

adb shell am start -n com.android.settings/com.android.settings.SubSettings -e :settings:show_fragment com.android.settings.bluetooth.BluetoothPairingDetail

注意,Settings中的很多页面都是“.SubSettings” activity,需要指定fragment的名称来打开,如上面的例子的fragment的名字是“com.android.settings.bluetooth.BluetoothPairingDetail”。

获取fragment最便捷的方式是从logcat中查找目前打开这个页面的fragment名称,搜索关键字“fragment”即可!

3. 通过uiautomator获取屏幕内容,对获取的内容进行分析
    比如,查询xml是否包含某一个特定名称的蓝牙机器。

adb shell uiautomator dump
adb pull /sdcard/window_dump.xml

小结:

在blueZ(hciconfig/hcitool)无法在Android使用的情况下,PC端尽可能考虑Python+UiAutomator方式对蓝牙进行操作,当然开发新的APK也是一个不错的选择;更容易的,是通过一些脚本来实现一些操作来完成相应的测试,这取决于测试任务的目标。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值