How Can One Detect Airplane Mode on Android

How can one detect airplane mode on Android?

/**
* Gets the state of Airplane Mode.
*
* @param context
* @return true if enabled.
*/
private static boolean isAirplaneModeOn(Context context) {

return Settings.System.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;

}

How can I detect if device is in Airplane mode?

You can use this method:

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isAirplaneModeOn(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(context.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}
}

How to detect airplane mode on whole application in android

You can add this code where you want to detect whether connection is available.

for example:

@Override
public void onClick(View v) {
boolean isonair = myApp.isAirplaneModeOn(this);
Toast.makeText(this, "IS on AIR? " + isonair, Toast.LENGTH_LONG).show();
}

If you want to have an access for static function use them in Application class:

public class myApp extends Application {
public static function isAirplaneModeOn() {
...
}
}

in any activity use this access: myApp.isAirplaneModeOn()

do not forget update your AndroidManifest.xml:

<application
android:largeHeap="true"
android:name=".myApp" <<<<<<<<<<<<<<<<<<< this is your class name
android:icon="@drawable/somedrawable"
android:label="@string/app_alias"
android:theme="@style/AppTheme" >

Is there anyway to mimic turn airplane mode on and off? (BluetoothLeScanner sometimes not detecting any signals)

Unfortunately turning on/off Airplan mode on Android v4.2 is not supported. Have a look at the links below:-

  • https://developer.android.com/about/versions/android-4.2.html
  • How to programmatically enable/disable flight mode
  • Programmatically setting ON/OFF airplane mode

Is there any reason for using Android 4.2? It is a very outdated OS (10 years old) and Android has improved vastly since then especially when it comes to BLE API and functionality. If you have the option of upgrading to a newer OS, I would recommend that you start with this as your first priority.

If upgrading to a newer OS is not an option, then I would personally try to fix the actual problem instead of finding a workaround. One possibility of this happening is that you are scanning too often, which can sometimes result in no scan results being returned for about 30 seconds. This is a tricky one as the Android OS doesn't return an error - it just doesn't return any results which seems to be similar to what you are seeing. Have a look at this link and search for the string "notable scan errors". I would also look at the Logcat log as you can sometimes find more info there.

Finally, it is possible that this is somehow a problem with the hardware (especially if it is an old device). Try installing nRF Connect and scanning for BLE devices. If the issue exists over there as well, then that proves that it's a hardware/device problem. If the issue doesn't exist over there, then this is a problem with your app.

Airplane Mode Receiver in Manifest?

With a better understanding of Intents and XML defined receivers I was able to figure this out. This has been tested on API 17 only. Here is the code for the manifest:

<receiver android:name="com.example.i2c.AirplaneModeReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE"/>
</intent-filter>
</receiver>

And then create a java class file called AirplaneModeReceiver and put the following in it:

public class AirplaneModeReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
}
}

Now this will start the activity whenever there is a change in Airplane Mode, be it on or off. I didn't pursue writing code for the check whether or not Airplane Mode was off and then starting the activity any further since switching to a home screen launcher style application instead.



Related Topics



Leave a reply



Submit