0

I am trying to write a java plugin for the Android Monkeyrunner to check the WiFi state. I would like to use the Android API WiFiManager to get the current WiFi state and return it back.

The error message in Eclipse is: The method getWifiState() is undefined for the type MonkeyWifi

How can I change the code to be able to get the current WiFi status?

This is the code I have so far:

package com.my.android.wifi;

import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

import com.android.monkeyrunner.MonkeyDevice;
import com.google.common.base.Predicate;

import android.net.wifi.WifiManager;

public class MonkeyWifi implements Predicate<PythonInterpreter> {

public class NewActivity extends Activity {
    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
}

public int WifiStatus(){
    int state;
    state = wifiManager.getWifiState();
    return state;
}


@Override
public boolean apply(PythonInterpreter arg0)    {
    return false;
}

}

Empario
  • 402
  • 6
  • 19

2 Answers2

1

You can use AndroidViewClient instead (version 8.26.0 or higher), then you would be able to access basic wifi information.

A basic script to check wifi state would be along these lines

#! /usr/bin/env python
# -*- coding: utf-8 -*-

from com.dtmilano.android.viewclient import ViewClient
from com.dtmilano.android.adb.adbclient import WIFI_SERVICE, WifiManager


device, serialno = ViewClient.connectToDeviceOrExit()
wifiManager = device.getSystemService(WIFI_SERVICE)
print "wifi state:", wifiManager.getWifiState()
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You must create an instance of WifiManager. According to this page, you can do it by calling:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 

And then you can get the state by:

state = wifiManager.getWifiState();
Bubletan
  • 3,833
  • 6
  • 25
  • 33
  • Thanks for the quick response. I am new to Java and trying to figure it out. I have changed it according to your input. I get the following message: The method getSystemService(String) is undefined for the type MonkeyWifi – Empario Dec 18 '14 at 21:47
  • The method `getSystemService(String)` is part of the `Activity` class, so an easy solution would be creating the `wifiManager` instance inside a class that extends `Activity`. – Bubletan Dec 18 '14 at 22:00
  • You should have a class that `extends Activity`. If the `MonkeyWifi` is created from there, you can create a constructor for it `public MonkeyWifi(WifiManager wifiManager)` and then save the wifiManager as an instance `this.wifiManager = wifiManager;` to be able to use it. Then in the `Activity` class, create the `MonkeyWifi` instance using `new MonkeyWifi((WifiManager) getSystemService(Context.WIFI_SERVICE))`. Other way is making a public instance `public static WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);` in your `Activity` class. – Bubletan Dec 18 '14 at 23:09
  • I tried few thing but couldnt get to a solution. I appreaciate all your help. Do you mind looking at the code above in the questions? **wifiManager** in: **state = wifiManager.getWifiState();** is not being recognized. – Empario Dec 26 '14 at 19:03