2

One of the functions in my app sends data over the internet. Before attempting to send the data, I check whether a connection exists:

private boolean isConnected() {
    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State val1 = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
    NetworkInfo.State val2 = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
    return NetworkInfo.State.CONNECTED.equals(val1) || NetworkInfo.State.CONNECTED.equals(val2);
}

This worked perfectly fine on emulator and a couple of real devices I tested on. Then I received an error report from the client, which on investigation turned out to be a NullPointerException on getState line for TYPE_MOBILE.

Apparently, connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) returned null on the device that didn't have 3G (a WiFi-only tablet). Although I did test on a Nexus 7 emulator, I didn't get this error.

Hence, what I'm interested in is creating an AVD that explicitly does not have 3G (i.e. an AVD for a WiFi-only device) so that I could investigate/test such scenarios. I haven't found anything in the emulator options, but maybe I'm just looking in a wrong place. Is this even possible?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Aleks G
  • 56,435
  • 29
  • 168
  • 265

2 Answers2

3

You should be able to change the hardware settings for an AVD to remove the modem. The option is labeled hw.gsmModem, but I believe it disables all "cell data" connectivity, not just GSM.

If you want to change hardware options without using the AVD Manager, edit the config.ini file found in the avd's folder. By default, the folder is located at:

  • Linux/Mac: ~/.android/avd/yourAvdName

  • Windows Vista/7/(8?): C:\Users\.android\yourAvdName

  • Windows XP: C:\Documents and Settings\.android\yourAvdName

Just add a line that says:

hw.gsmModem=no

See here and here for more detail.


However, you may be able to test it by turning off 3G instead, by pressing F8 in the emulator. I don't know if that will simulate the null you're looking for, but it's worth a shot.

See here for more shortcuts.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • I tried turning off 3G data - this simulates turning off 3G data in the phone - and connection info isn't null, instead its getState returns DISCONNECTED. Clearly this is not what I am after. – Aleks G Mar 07 '13 at 15:45
  • I can't even find where to remove hw.gsmModem. The latest version of the ADV manager doesn't give me those options. I suppose, I could hunt down the xml for the avd, but disabling all cell data isn't an option for me. I guess I'll just add checks for nulls. – Aleks G Mar 11 '13 at 10:46
  • I haven't updated my AVD manager in a bit, but you don't need it to change the options either way. Edited with instructions. – Geobits Mar 11 '13 at 12:40
1

I don't believe there is a simple solution to this. In the mean time, I adopted my code to look like this:

private boolean isConnected(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info1 = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    NetworkInfo info2 = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo.State val1 = (info1 == null ? null : info1.getState());
    NetworkInfo.State val2 = (info2 == null ? null : info2.getState());
    return (info1 != null && NetworkInfo.State.CONNECTED.equals(val1)) || (info2 != null && NetworkInfo.State.CONNECTED.equals(val2));
}

This takes care of null values when an interface is not present.

Aleks G
  • 56,435
  • 29
  • 168
  • 265