Home > android, wifi > Debugging Wifi in the emulator

Debugging Wifi in the emulator

The Android emulator has no Wifi support. While it’s possible to obtain an instance of WifiManager by calling getSystemService(Context.WIFI_SERVICE), the returned object’s methods fail when trying to enable Wifi radio, or start a nework scan.

Still, sometimes it’s useful to debug in the emulator – such as when testing support for various screen sizes.

This is the way I do it for my application.

First, we need some way to enable special case code when running in the emulator.

There is information floating around on how to determine if running in the Android emulator, but I took a more simple approach. I have a global boolean in a utility class that I can quicky set to true before starting an emulator debugging session.

public class Util {

	// set to true before debugging in the emulator
	public static boolean gEmulatorMode = false;

	.....
};

Create an abstraction for scan results

Since we can’t change the way WifiManager works in the emulator, we have to add a layer of abstraction that can behave differently for the emulator vs. real device.

public class WifiNetworkList {

	public static class Item {
		public String ssid;
		public String features;
		public int freq;
		public int level;
		public String bss;

		// ......
	}

	// List of networks
	private HashMap<String, Item> mNetworkList;

	public HashMap<String, Item> getNetworkList() {
		return mNetworkList;
	}
}

On the real device, the WifiNetworkList class is initialized with WifiManager‘s scan results:

public class WifiNetworkList {

	public void initialize(List<ScanResult> liveList) {
		mNetworkList = new HashMap<String, Item>();

		for (ScanResult sr : liveList) {
			Item item = new Item;

			item.ssid = sr.SSID;
			item.features = sr.capabilities;
			item.freq = sr.frequency;
			item.level = sr.level;
			item.bss = sr.BSSID;

			// Save for later use
			mNetworkList.put(item.ssid, item);
		}
	}
}

The code that uses this class is supposed to start a Wifi network scan, and, when results are available, pass them to the initialize method above.

Any code that wishes to access the list of live networks does so by calling WifiNetworkList.getNetworkList() and working with the returned list.

class SomeActivityOrNotActivity extends ActivityOrSomethingElse {

	WifiNetworkList mNetworkList = new WifiNetworkList();

	// Set up to be called when the user presses "Scan" button
	
	public void startScan() {
		WifiManager wfMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		wfMgr.startScan();
	}

	// Set up to be called when WifiManager.SCAN_RESULTS_AVAILABLE_ACTION is broadcast

	public void onScanDone() {
		// Update the live network list...
		WifiManager wfMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		List<ScanResult> srList = wfMgr.getScanResults();

		// ... by calling our network list object
		mNetworkList.initialize(srList);

		// Update the UI - do whatever is needed to display scan results
		updateUI();
	}

	public void updateUI() {
		// Application specific
	}
}

A special version of WifiNetworkList.initialize

When running in the emulator, we won’t have a way to obtain List srList from WifiManager. Therefore, we need a special version of initialize to create dummy “live” networks for the rest of the application to use.

public class WifiNetworkList {

	public void initializeEmulator() {
		mNetworkList = new HashMap<String, Item>();

		Item item = new Item;

		item.ssid = "Exotica";
		item.features = "[WPA2-PSK-CCMP]";
		item.freq = 2422; // Wifi channel 3
		item.level = -40;
		item.bss = "01:02:03:04:05:06";

		// Save for later use
		mNetworkList.put(item.ssid, item);

		item = new Item;

		item.ssid = "Despotica";
		item.features = "[WPA2-PSK-TKIP]";
		item.freq = 2427; // Wifi channel 4
		item.level = -45;
		item.bss = "01:02:03:04:05:07";

		// Save for later use
		mNetworkList.put(item.ssid, item);

		item.ssid = "Hypnotica";
		item.features = "[WEP]";
		item.freq = 2432; // Wifi channel 5
		item.level = -50;
		item.bss = "01:02:03:04:05:08";

		// Save for later use
		mNetworkList.put(item.ssid, item);
	}
}

Last step: calling the right method

Now we are ready to put it all together. Change the code that normally starts Wifi scans and passes results to WifiNetworkList to look like this:

class SomeActivityOrNotActivity extends ActivityOrSomethingElse {

	WifiNetworkList mNetworkList = new WifiNetworkList();

	// Set up to be called when the user presses "Scan" button
	
	public void startScan() {

		if (Util.gEmulatorMode) {

			 // Running in the emulator, use fake network list
			 // This is available immediately, so we can update the UI right away

			mNetworkList.initializeEmulator();
		 	updateUI();
		 } else {

			// Running on a real device, can do real Wifi network scan

		 	WifiManager wfMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE);
			wfMgr.startScan();
		 }
	}

	public void updateUI() {
		// Application specific
	}
}

That’s all folks! I hope this is useful.

About these ads
Categories: android, wifi
  1. tlsmith1
    March 9, 2014 at 11:15 am

    http://stackoverflow.com/questions/2298711/android-how-to-set-wifi-settings-to-access-internet-connection-in-android-simul

    if the emulator connects to the network by default and permissions are set correctly as described above, can we obtain a list of available networks? This should automatically bridge the host computers network. Second, above it is mentioned the GPS “fakes” it out, how does GPS do this? The android/gps.c is a character driver.

    • March 9, 2014 at 1:02 pm

      I’m not aware of a way to do this. Yes, the emulator connects to the network, but it emulates a mobile data connection, not WiFi.

  2. Mw777
    September 24, 2013 at 5:45 am

    I wanna run an app that requires wifi on the emulator , even if there’s an internet connection it still requires wifi , will those codes make it work ? sorry im a noob in these things..

    • September 27, 2013 at 2:58 pm

      The emulator has no WiFi support. For your own apps, you can fake it by checking if running in the emulator. For other apps, you’re most likely out of luck.

  3. June 18, 2012 at 3:35 am

    Hi Kostya,

    In my understanding, you modified the Android source code and faked the scan results?

    • June 18, 2012 at 2:08 pm

      No. I’ve written a simple thin wrapper for functions that get scan results and use this wrapper in my app. The wrapper knows when it’s running in the emulator and returns some predefined list networks for debugging.

  4. July 14, 2011 at 9:02 am

    Hi,
    is there any other way to solve this problem without changing emulator source code?

    • July 14, 2011 at 12:15 pm

      Not as far as I can tell.

      I recommend debugging WiFi on a real device.

      • karthick
        July 19, 2011 at 6:48 pm

        Hi,
        I am having one doubt. can we have android application which will return dummy wifi interface when another application trying to find wifi interface in emulator?

  5. karthick
    June 17, 2011 at 6:22 am

    Hi, sorry for disturbing again…. Here so many things are there, not able to find exact things regarding wifi manager. if u know which git is having emulator code, that will be fine for me.. thanks…

  6. karthick
    June 7, 2011 at 5:57 pm

    hi.
    where i can find wifi manager source code file. i downloaded android source code . but not able find the file which contains wifi manager code. pls reply … thanks

  7. Alexey
    November 27, 2010 at 12:58 am

    Fair enough. Google just could be more explicit about it and add it to “Emulator Limitations” section.

  8. Alexey
    November 25, 2010 at 2:39 am

    Its good idea to use abstraction layer.
    How do you know for sure that it is not supposed to work on emulator?
    I just was hoping its because of some bugs that hopefully could be fixed soon.
    Strangly, but Android documentation doestnt state this explicitly “wifi doesnt work on emulator”

    thanks

    • November 25, 2010 at 12:04 pm

      Alex,

      How do I know?

      WifiManager.isEnabled returns false, attempting to enable WiFi fails, and any other methods fail. That seems pretty clear.

      Supporting WiFi in the emulator technically could be done by either bridging to the host computer’s WiFi adapter (if any), or by faking it (like with GPS). I wouldn’t get your hopes high for either happening any time soon.

  9. ndgeneral55
    October 3, 2010 at 8:08 am

    Hi

    I would like to demo an app using the emulator which actually connects to a wifi network and exchanges information with a wifi device. Is it possible to do this somehow?

    • October 5, 2010 at 6:45 pm

      Hi,

      You can’t debug any Wifi connection management code in the emulator.

      On the other hand, your code that exchanges data over the network will work, using the emulator’s network connectivity features.

      What I would recommend is implementing some kind of global switch in your code to skip over Wifi management, so you can debug actual network data exchange.

  10. jim
    July 28, 2010 at 6:22 am

    Thanks for publishing this.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 109 other followers