You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Appium Python Client启用/禁用蓝牙或定位服务?

如何使用Appium Python Client启用/禁用蓝牙或定位服务?

嘿,我刚好在项目里折腾过类似的需求,给你拆解下Android和iOS平台的具体操作方法,都是用Appium Python Client直接实现的:

Android 平台操作

1. 启用/禁用蓝牙

Android可以通过执行adb shell命令来控制蓝牙,Appium提供了调用shell命令的接口,直接用execute_script执行mobile: shell命令就行:

# 启用蓝牙
driver.execute_script('mobile: shell', {
    'command': 'service call bluetooth_manager 4'
})

# 禁用蓝牙
driver.execute_script('mobile: shell', {
    'command': 'service call bluetooth_manager 6'
})

注:不同Android版本的service call编号可能略有差异,如果上面的命令无效,可以尝试把4换成5、6换成7,或者查对应版本的蓝牙服务调用参数

2. 启用/禁用定位服务(GPS)

同样用adb命令修改系统设置,通过Appium执行:

# 启用定位(高精准模式,包含GPS、WiFi等)
driver.execute_script('mobile: shell', {
    'command': 'settings put secure location_mode 3'
})

# 禁用定位
driver.execute_script('mobile: shell', {
    'command': 'settings put secure location_mode 0'
})

location_mode的参数说明:0=关闭,1=仅设备(纯GPS),2=仅网络,3=高精准模式

iOS 平台操作

iOS的操作依赖Appium的XCUITest驱动提供的专属mobile命令,建议用最新稳定版的Appium来避免兼容性问题:

1. 启用/禁用蓝牙

使用mobile: setBluetooth命令:

# 启用蓝牙
driver.execute_script('mobile: setBluetooth', {'enabled': True})

# 禁用蓝牙
driver.execute_script('mobile: setBluetooth', {'enabled': False})

注意:iOS 13及以上版本,需要在测试设备的「设置-隐私与安全性-自动化」里给你的测试工具开启权限,否则命令会直接失效

2. 启用/禁用定位服务

使用mobile: setLocationServicesEnabled命令:

# 启用定位服务
driver.execute_script('mobile: setLocationServicesEnabled', {'enabled': True})

# 禁用定位服务
driver.execute_script('mobile: setLocationServicesEnabled', {'enabled': False})

如果你的测试App需要特定的定位权限(比如始终允许),得提前在设备设置里配置好,这个命令只是控制系统级的定位总开关

额外小提示

  • 执行这些操作前,确保Appium会话已经正常连接到目标设备
  • Android设备要开启USB调试,并且Appium有足够权限执行adb命令
  • iOS设备需要信任连接的电脑,且已经安装好对应的WebDriverAgent

备注:内容来源于stack exchange,提问作者Jinal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 10:48:16