Recently Google released Android4.4, looked at the source, 4.4 of the Bluetooth open process this part or some changes, from the interface to see the Bluetooth switch is set settings The switch switch, widget switch can of course, the starting point of the following process is the same. First look at the Systemserver.java code, the Bluetooth service opens the place, the real machine case we are concerned about the last else branch.
if (Systemproperties.get ("Ro.kernel.qemu"). Equals ("1")) {
SLOG.I (TAG, "No bluetooh Service (emulator)");
} else if (factorytest = = Systemserver.factory_test_low_level) {
SLOG.I (TAG, "No Bluetooth Service (Factory test)");
} else if (!context.getpackagemanager (). hassystemfeature
(Packagemanager.feature_bluetooth)) {
SLOG.I (TAG, "No bluetooth Service (bluetooth Hardware not Present)");
} else if (Disablebluetooth) {
SLOG.I (TAG, "Bluetooth Service disabled by config");
} else {
SLOG.I (TAG, "Bluetooth Manager Service");
bluetooth = new Bluetoothmanagerservice (context);
Servicemanager.addservice (Bluetoothadapter.bluetooth_manager_service, BLUETOOTH);
}
Looking at the construction method of Bluetoothmanagerservice, we look at three places, Loadstorednameandaddress () is the place to read Bluetooth open default name, Isbluetoothpersistedstateon () is to determine if Bluetooth is turned on, and if it is turned on, follow the action to turn on Bluetooth
Bluetoothmanagerservice (Context context) {
Mhandler = new Bluetoothhandler (Iothread.get (). Getlooper ());
Mcontext = context;
Mbluetooth = null;
Mqbluetooth = null;
Mbinding = false;
Munbinding = false;
Menable = false;
Mstate = Bluetoothadapter.state_off;
Mquietenableexternal = false;
Menableexternal = false;
Maddress = null;
Mname = null;
Merrorrecoveryretrycounter = 0;
Mcontentresolver = Context.getcontentresolver ();
Mcallbacks = new remotecallbacklist<ibluetoothmanagercallback> ();
Mqcallbacks = new remotecallbacklist<iqbluetoothmanagercallback> ();
Mstatechangecallbacks = new remotecallbacklist<ibluetoothstatechangecallback> ();
Intentfilter filter = new Intentfilter (intent.action_boot_completed);
Filter.addaction (bluetoothadapter.action_local_name_changed);
Filter.addaction (intent.action_user_switched);
Registerforairplanemode (filter);
Mcontext.registerreceiver (mreceiver, filter);
Loadstorednameandaddress ();
if (Isbluetoothpersistedstateon ()) {
Menableexternal = true;
}
}
Another method of Registerforairplanemode is as follows
private void Registerforairplanemode (Intentfilter filter) {
Final Contentresolver resolver = Mcontext.getcontentresolver ();
Final String Airplanemoderadios = Settings.Global.getString (resolver,
Settings.Global.AIRPLANE_MODE_RADIOS);
Final String Toggleableradios = Settings.Global.getString (resolver,
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
Boolean misairplanesensitive = Airplanemoderadios = = null? true:
Airplanemoderadios.contains (Settings.Global.RADIO_BLUETOOTH);
if (misairplanesensitive) {
Filter.addaction (intent.action_airplane_mode_changed);
}
}
which
Settings.Global.getString (Resolver,
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS)
The value obtained is defined in a string file, such as:
<!--comma-separated List of Bluetooth, WiFi, and cell. -
<string name= "Def_airplane_mode_radios" translatable= "false" >cell,bluetooth,wifi,nfc,wimax</string>
Indicates which services will be turned off if airplane mode is turned on. So the Registerforairplanemode method is that if Bluetooth is also affected by flight mode, then the change in flight mode will also enable the Bluetooth service to receive the corresponding broadcast.
Interface switch is Bluetoothenabler.java this class, and setbluetoothenabled () is the specific switch action. There is a callback function for the switch, the code is as follows:
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
Show Toast Message If Bluetooth is not allowed in airplane mode
if (isChecked
&& (Wifisettings.needprompt (mcontext) | |! Wirelesssettings.isradioallowed (
Mcontext, Settings.Global.RADIO_BLUETOOTH))) {
Toast.maketext (Mcontext, R.string.wifi_in_airplane_mode,
Toast.length_short). Show ();
Reset Switch to OFF
Buttonview.setchecked (FALSE);
}
Shouldn ' t setbluetoothenabled (true) in airplane mode.
if (mlocaladapter! = null) {
if (isChecked && wifisettings.needprompt (mcontext)) {
Return
}
Mlocaladapter.setbluetoothenabled (isChecked);
}
Mswitch.setenabled (FALSE);
}