May 6, 2009

Detect Airplaine/Flight Mode on Motorola and Sony Ericsson Mobile Phones in Java

Many MIDlets communicate with a server and/or connects to the Internet. If the user enables airplane/flight mode, the MIDlet should be aware of this. This way it is possible to set the MIDlet in off-line mode automatically and inform the user when trying to access the net. It is much nicer to get a dialog saying "Sorry you are in flight mode, you cannot this and that", instead of "Failed to connect to server". This article describe how to do this in Java ME (on some platforms).

There is no API that enable a Java ME developer to access the flight mode. The key here is to get a system property via the right key, no pun intended :) This is done via a call to System.getProperty("key"). The key differs from platform to platform.

On a Motorola mobile phone, with Motorola OS, this is done like this:

String airplaneMode = System.getProperty("com.mot.network.airplanemode");

if(airplaneMode.equals("true")){
// We are in flight mode
}else{
// We are not in flight mode
}


On a Sony Ericsson mobile phone it is a little bit different, since there is no system property to detect if we are in flight mode or not. The trick here is to use a key that detects the Radio Access Technology (RAT) used at the moment. This returns null if the radio is turned of, which it always is when in flight mode. The code looks like this:

String flightMode = System.getProperty("com.sonyericsson.net.rat");

if(flightMode == null){
// We are in flight mode
}else{
// We are not in flight mode
}


And that is it! Pretty simple, but still powerful. I hope this trick is useful for you.

No comments: