0

I am writing MonkeyRunner script which checks state of wifi and enables it.

For doing so I am importing android API and its classes. Below is code snippet

from android.net.wifi import WifiManager

global state

state = WifiManager.isWifiEnabled()

print state

While executing I am getting an error as:

state = WifiManager.isWifiEnabled()

TypeError: isWifiEnabled(): expected 1 args; got 0

Then I realized I need permission for doing wifi operation and found in some AndroidManifest.XML file, where they have used

user-permission android:name="android.permission.ACCESS_WIFI_STATE"

My doubt is I am running the script on real device and how can I use

user-permission android:name="android.permission.ACCESS_WIFI_STATE"

in my MonkeyRunner script file.

Is there any way to set the permission using android.Manifest.permission class in python script file.

Thanks in Advance

Surendra

Pratik
  • 30,639
  • 18
  • 84
  • 159
surendra
  • 1
  • 2

2 Answers2

0

android.jar is part of the Android SDK and contains only stub implementations of the SDK classes, not implementations that are found on real devices.

Furthermore, if for a moment we suppose it contained the real implementations, you won't be able to do what you intend because your monkeyrunner script is running on your development computer, not on the device, so if you were lucky enough to find a WifiManager that works you will be changing your host computer Wifi settings.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • you mean WifiManager.isWifiEnabled() is executing in my dev m/c not on device?and getting an error as TypeError isWifiEnabled() expected 1 args got 0. Here I didn't understood why it expecting one more argument when function prototype doesn't have any.With some of the MonkeyRunner commands was able to open the Main setting menu on real device,then Wifi Setting where was able to toggle Wifi.Here I want to make my script to check wifi state before toggling wifi.Could you please tell me is there any way to check the state if I cannot use WifiManager. – surendra Jan 23 '13 at 06:16
  • I found this in http://developer.android.com/tools/help/monkeyrunner_concepts.html. The monkey tool runs in an adb shell directly on the device or emulator and generates pseudo-random streams of user and system events. In comparison, the monkeyrunner tool controls devices and emulators from a workstation by sending specific commands and events from an API. By this guess I should be able to use WifiManager. Could you please help me get rid of that error. – surendra Jan 23 '13 at 06:32
  • That's the important part: "from a workstation by sending specific commands..." – Diego Torres Milano Jan 23 '13 at 07:29
  • Hi dtmilano, is there any other way in monkeyrunner tool to test state of the Wifi/Bluetooth before toggling?? – surendra Jan 24 '13 at 05:20
  • Your lucky day, take a look at one of AndroidViewClient's examples: https://github.com/dtmilano/AndroidViewClient/blob/master/AndroidViewClient/examples/settings.py it may be what you are looking for... – Diego Torres Milano Jan 24 '13 at 05:31
  • Thanks dtmilano. could you please share the dependent modules(com.dtmilano.android.viewclient) to execute the program successfully. – surendra Jan 24 '13 at 09:03
  • @dlmilano: bad luck... I have two real devices both throwing the error as "raise Exception('Cannot start View server.\n' Exception: Cannot start View server. This only works on emulator and devices running developer versions. Does hierarchyviewer work on your device ?" and the other says "ERROR: Device is secure, AndroidViewClient won't work." Both devices have root permission but still getting these errors. any workaround?? – surendra Jan 25 '13 at 06:05
  • root is not enough: https://github.com/dtmilano/AndroidViewClient/wiki/Secure-mode – Diego Torres Milano Jan 25 '13 at 07:08
  • my one device is [ro.secure]: [0] [ro.debuggable]: [1] and other [ro.secure]: [1] [ro.debuggable]: [0] , so AVC should work at least for first device. Even first device is throwing an error "raise Exception('Cannot start View server.\n' Exception: Cannot start View server. This only works on emulator and devices running developer versions. Does hierarchyviewer work on your device ?" – surendra Jan 25 '13 at 08:56
  • so, as said, the question is: "Does hierarchyviewer work on your device ?" – Diego Torres Milano Jan 25 '13 at 17:03
  • yes I can run hierarchyviewer successfully. in fact hierarchyviewer is deprecated but stil I can run hierarchyviewer/monitor.bat/DDMS – surendra Jan 28 '13 at 06:49
  • AndroidViewClient uses the same method as hierarchyviewer to obtain the dump, that's why (usually) if one works so does the other. You may have a particular situation, I suggest creating an issue in AndroidViewClient so it can be tracked correctly. – Diego Torres Milano Jan 28 '13 at 08:21
  • Hi dtmilano, in viewclient.py file I have commented "raise Exception" in except part & also printing the serialno,localport,remoteport after that. now I can see Setting Menu is open, Highlighted on Wi-Fi submenu on device. but on command prompt I see "subprocess.CalledProcessError: Command '['adb.exe', '-s', u'TA191057FG', 'forward', 'tcp:4939', 'tcp:4939']' returned non-zero exit status 1. and also after printing all my info it is displaying "error: device not found" Is there anything still wrong? – surendra Jan 29 '13 at 08:50
0

NOT answer to your question, But your aim works.

If you just want to check wifi is enabled or not you can use below code:-

import re
import os
def is_wifi_enabled(deviceid):
    wifistate = os.popen('adb -s %s shell getprop wlan.driver.status'%deviceid).read()
    wifion=re.search(r'ok',wifistate)
    return wifion
 is_wifi_enabled(deviceid)

I am not sure about all android devices, i have tried in some of the devices.

Rilwan
  • 2,251
  • 2
  • 19
  • 28