简体   繁体   中英

Android Monkeyrunner plugin for Wifi state

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;
}

}

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()

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM