How to Programmatically Enable and Disable Flight Mode on Android 4.2

Toggle airplane mode in Android

This answer contains code necessary to do this. Also make sure you have the WRITE_SETTINGS permission.

Adapted from Controlling Airplane Mode:

// read the airplane mode setting
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;

// toggle airplane mode
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

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.

How to toggle Airplane Mode on Android 4.2 and above using root?

There is a new "settings" binary in Android 4.2
You can also use it without su, then your app needs the permission required to change this setting declared in your app's manifest (which would be WRITE_SECURE_SETTINGS for flight mode in 4.2 - which is only granted to apps installed on system partition).

Activate

su 
settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

Deactivate

su
settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

For other android versions and other settings (flight mode can be activated without root before 4.2) you can use sql injects in settings.db

su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into global values(null, 'airplane_mode_on', 1);

Replace "global" with "system" or "secure" and 'airplane_mode_on' with the key of your desired table entry. For some settings you need to send certain broadcasts afterwards, see example above for flight mode.

To explore your settings.db run this in a terminal app:

su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
.tables
select * from global
select * from secure
select * from system

How to set the AIRPLANE_MODE_ON to True or ON?

See the blog article Android: Controlling Airplane Mode ,

Works only upto API 16

// Toggle airplane mode.
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload.
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

where isEnabled is whether airplane mode is enabled or not.

How to put a phone in Airplane mode programmatically in Android?

See the blog article http://dustinbreese.blogspot.in/2009/04/andoid-controlling-airplane-mode.html ,

Works only upto API 16

// Toggle airplane mode.
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

// Post an intent to reload.
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);

where isEnabled is whether airplane mode is enabled or not.



Related Topics



Leave a reply



Submit