通过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也是一个不错的选择;更容易的,是通过一些脚本来实现一些操作来完成相应的测试,这取决于测试任务的目标。