Appium 请问大神,appium 如何控制开启和关闭 android 手机的蓝牙功能

jay · 2015年08月12日 · 最后由 杨豆豆 回复于 2016年05月04日 · 3020 次阅读

现在项目有一个功能需要开启/关闭蓝牙和 wifi 的功能,了解到 appium 可以设置手机的网络设置(python)

下面的代码是摘自 webdriver.py 中,但是只能设置 wifi 开关,不能设置蓝牙开关。

找到资料说 Java 中可以用 setNetworkConnection 方式来设置 wifi 和蓝牙,但是 python 中不知道如何设置蓝牙,求大神指教!!

setNetworkConnection(NetworkConnectionSetting connection)
设置手机的网络连接状态,可以开关蓝牙、wifi、数据流量。通过 NetworkConnectionSetting 中的属性来设置各个网络连接的状态。

def set_network_connection(self, connectionType):
    """Sets the network connection type. Android only.
    Possible values:
        Value (Alias)      | Data | Wifi | Airplane Mode
        -------------------------------------------------
        0 (None)           | 0    | 0    | 0
        1 (Airplane Mode)  | 0    | 0    | 1
        2 (Wifi only)      | 0    | 1    | 0
        4 (Data only)      | 1    | 0    | 0
        6 (All network on) | 1    | 1    | 0
    These are available through the enumeration `appium.webdriver.ConnectionType`

:Args:
- connectionType - a member of the enum appium.webdriver.ConnectionType
"""
data = {
'parameters': {
'type': connectionType
}
}
return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']

共收到 8 条回复 时间 点赞

应该好像说只支持 Java 客户端吧?

我去看了下 Java 客户端的 setNetworkConnection 的用法

https://github.com/appium/java-client/blob/48813418248be00dc3ca4af9821bb07dd01ab38a/src/test/java/io/appium/java_client/android/AndroidDriverTest.java

public void networkConnectionTest() {
  NetworkConnectionSetting networkConnection = new NetworkConnectionSetting(false, true, true);

  networkConnection.setData(false);
  networkConnection.setWifi(false);


  driver.setNetworkConnection(networkConnection);
  networkConnection = driver.getNetworkConnection();

  assertEquals(new NetworkConnectionSetting(false, false, false), networkConnection);

}

用起来和 python 版的一样啊,三个值分别代表数据连接、wifi 和飞行模式。
没见到有控制蓝牙的功能。

appium 设定 network 是通过 settings.apk 设定的,它的项目地址是 https://github.com/appium/io.appium.settings

虽然要求有点高,但如果你懂得基本的 android app 开发的话可以把它写到这个 app 里。
相关的文章http://blog.csdn.net/stevenhu_223/article/details/9052083

#3 楼 @chenhengjie123 是不是说我二次开发的 apk 只要丢到 appium 的那个 setting 的路径下就可以了?

#4 楼 @zsx10110 看你想做到什么程度吧。

  • 方案一(开发量少,不依赖 appium client,但需要直接调用 adb 命令):
  1. 给 apk 增加控制蓝牙的命令接口
  2. 把更新后的 apk 放到 appium/build/settings_apk/settings_apk-debug.apk
  3. 脚本中通过 adb 命令启动这个 apk 来开关蓝牙
  • 方案二(开发量相对多一些,但封装到了 appium server 中,更方便所有人使用):
  1. 给 apk 增加控制蓝牙的命令接口
  2. 把更新后的 apk 放到 appium/build/settings_apk/settings_apk-debug.apk
  3. 更新 appium server 相关代码,增加控制蓝牙开关的 api(主要修改 appium/lib/devices/android/android-common.jsappium-adbappium-adb/lib/adb.js
  4. 更新 appium client 相关代码,增加控制蓝牙开关的 api 。

建议先用方案一,稳定后再做方案二的后两步吧。主要的重点难点都在 apk 和 adb 命令那里。

jay #6 · 2015年08月13日 Author

#5 楼 @chenhengjie123 多谢大神指的思路,我先按你说的去研究一下。

我在项目里遇到一个问题,就是设置后,程序跑不动了。
部分 ruby 代码如下:

it 'login with the work mode is STA' do
    fail_sum = pass_sum = 0
    set_STA_mode_before_login()
    login_with_password_remember(@login_username,@login_password)
 @total_num.times do
    begin
        @login_button.click
        sleep 2
        enter_wkzx_page()
        sleep 2
        if exists{id('com.iceboxcontrol:id/home')} == true #设备不在线
            fail_sum += 1
            puts "fail num is:#{fail_sum}"
        else pass_sum += 1
            puts "pass num is:#{pass_sum}"                  
        end
        enter_setting_page()
        id('com.iceboxcontrol:id/unlogin').click
        button('确定').click
        sleep 1
    ensure          
        @driver.set_network_connection (2) #(ConnectionType:Open Wifi only) 
        sleep 3
        start_activity app_package: 'com.iceboxcontrol', app_activity: 'com.iceboxcontrol.activitys.LoginActivity'
        sleep 1
    end 
 end
    puts "------------------------------"
    Fail_Rate = (fail_sum/@total_num.to_f * 100).round(2)
    Pass_Rate = (pass_sum/@total_num.to_f * 100).round(2)
    puts "Fail total number is: #{fail_sum},the Fail Rate is: #{Fail_Rate}%"
    puts "Pass total number is: #{pass_sum},the Pass Rate is: #{Pass_Rate}%"
end

实际 Appium Server 执行网络设置的 log 如下:

info: --> POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/network_connection {"type":2}
info: Setting network connection
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "settings put global airplane_mode_on 0"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 kill-server
error: Unhandled error: Error: read ECONNRESET
    at errnoException (net.js:905:11)
    at TCP.onread (net.js:559:19) context: [POST /wd/hub/session {"desiredCapabilities":{"app":"C:/Appium_Script/iceboxcontrol/IceBoxControl_CHiQ1U_debug.apk","
appActivity":"com.iceboxcontrol.activitys.LoginActivity","appPackage":"com.iceboxcontrol","platformName":]
info: [debug] UiAutomator exited
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 wait-for-device
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "echo 'ready'"
info: [debug] Starting logcat capture
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 forward tcp:4724 tcp:4724
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.and
roid.bootstrap.Bootstrap -e pkg com.iceboxcontrol -e disableAndroidWatchers false
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
info: [debug] [BOOTSTRAP] [debug] Loading json...
info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
info: [debug] [BOOTSTRAP] [debug] json loading complete.
info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
info: [debug] [BOOTSTRAP] [debug] Client connected
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"compressedLayoutHierarchy","params":{"compressLayout":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "am broadcast -a android.intent.action.AIRPLANE_MOD
E --ez state false"
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 kill-server
error: Unhandled error: Error: read ECONNRESET
    at errnoException (net.js:905:11)
    at TCP.onread (net.js:559:19) context: [POST /wd/hub/session {"desiredCapabilities":{"app":"C:/Appium_Script/iceboxcontrol/IceBoxControl_CHiQ1U_debug.apk","
appActivity":"com.iceboxcontrol.activitys.LoginActivity","appPackage":"com.iceboxcontrol","platformName":]
info: [debug] UiAutomator exited
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 wait-for-device
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "echo 'ready'"
info: [debug] Starting logcat capture
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 forward tcp:4724 tcp:4724
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.and
roid.bootstrap.Bootstrap -e pkg com.iceboxcontrol -e disableAndroidWatchers false
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
info: [debug] [BOOTSTRAP] [debug] Loading json...
info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
info: [debug] [BOOTSTRAP] [debug] json loading complete.
info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
info: [debug] [BOOTSTRAP] [debug] Client connected
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"compressedLayoutHierarchy","params":{"compressLayout":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "am start -n io.appium.settings/.Settings -e wifi o
n -e data off"
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 kill-server
error: Unhandled error: Error: read ECONNRESET
    at errnoException (net.js:905:11)
    at TCP.onread (net.js:559:19) context: [POST /wd/hub/session {"desiredCapabilities":{"app":"C:/Appium_Script/iceboxcontrol/IceBoxControl_CHiQ1U_debug.apk","
appActivity":"com.iceboxcontrol.activitys.LoginActivity","appPackage":"com.iceboxcontrol","platformName":]
info: [debug] UiAutomator exited
info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 wait-for-device
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "echo 'ready'"
info: [debug] Starting logcat capture
info: [debug] Forwarding system:4724 to device:4724
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 forward tcp:4724 tcp:4724
info: Starting App
info: [debug] Attempting to kill all 'uiautomator' processes
info: [debug] Getting all processes with 'uiautomator'
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "ps 'uiautomator'"
info: [debug] No matching processes found
info: [debug] Running bootstrap
info: [debug] spawning: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell uiautomator runtest AppiumBootstrap.jar -c io.appium.and
roid.bootstrap.Bootstrap -e pkg com.iceboxcontrol -e disableAndroidWatchers false
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
info: [debug] [BOOTSTRAP] [debug] Loading json...
info: [debug] Pushing command to appium work queue: ["compressedLayoutHierarchy",{"compressLayout":false}]
info: [debug] [BOOTSTRAP] [debug] json loading complete.
info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.
info: [debug] [BOOTSTRAP] [debug] Client connected
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"compressedLayoutHierarchy","params":{"compressLayout":false}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: compressedLayoutHierarchy
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":false,"status":0}
info: Getting network connection
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "settings get global airplane_mode_on"
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "settings get global wifi_on"
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "settings get global mobile_data"
info: [debug] Responding to client with success: {"status":0,"value":6,"sessionId":"72a9a501-64e9-4def-a64f-9266d6aa121b"}
info: <-- POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/network_connection 200 24747.456 ms - 73 {"status":0,"value":6,"sessionId":"72a9a501-64e9-4d
ef-a64f-9266d6aa121b"}
info: --> POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/appium/device/start_activity {"appPackage":"com.iceboxcontrol","appActivity":"com.iceboxcont
rol.activitys.LoginActivity","appWaitPackage":"","appWaitActivity":""}
info: [debug] Getting device API level
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "getprop ro.build.version.sdk"
info: [debug] Device is at API Level 19
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "am start -S -n com.iceboxcontrol/com.iceboxcontrol
.activitys.LoginActivity"
info: [debug] Responding to client with success: {"status":0,"value":"Successfully launched the app.","sessionId":"72a9a501-64e9-4def-a64f-9266d6aa121b"}
info: <-- POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/appium/device/start_activity 200 1522.236 ms - 104 {"status":0,"value":"Successfully launche
d the app.","sessionId":"72a9a501-64e9-4def-a64f-9266d6aa121b"}
info: --> POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/element/3/click {}
info: [debug] Pushing command to appium work queue: ["element:click",{"elementId":"3"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:click","params":{"elementId":"3"}}
info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue"
:"Unknown error"},"sessionId":"72a9a501-64e9-4def-a64f-9266d6aa121b"}
info: <-- POST /wd/hub/session/72a9a501-64e9-4def-a64f-9266d6aa121b/element/3/click 500 30.234 ms - 182
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: click
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"Unknown error","status":13}
info: [debug] [BOOTSTRAP] [debug] Emitting system alert message
info: [debug] Emitting alert message...
info: [debug] [BOOTSTRAP] [debug] Emitting system alert message
info: [debug] Emitting alert message...
info: [debug] Didn't get a new command in 60 secs, shutting down...
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "input keyevent 3"
info: [debug] Resetting IME to 'com.iflytek.inputmethod/.FlyIME'
info: [debug] executing cmd: E:\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s 274b3f06 shell "ime set com.iflytek.inputmethod/.FlyIME"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"value":"OK, shutting down","status":0}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 67.984
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects
info: [debug] Cleaning up appium session
info: [debug] We shut down because no new commands came in

实际执行结果如下图:

而如果代码中去掉下面这一行,则可以正常跑通,没搞明白为什么。

@driver.set_network_connection (2) #(ConnectionType:Open Wifi only) 

厉害啊

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册