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.

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.

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.

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.

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”

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.