Android4.4 Bluetooth Source code Snippet Analysis

Source: Internet
Author: User

Recently Google release time Android4.4, I looked at the source code. 4.4 Bluetooth Open process or some changes in this section, judging the Bluetooth switch is from the interface settings settings on the inside switch switch, widget of course, it can switch, maybe start different process is the same. First glance Systemserver.java the code: In the real case we are concerned with 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 where you read Bluetooth to open the default name. Isbluetoothpersistedstateon () is inferred if Bluetooth is turned on, assuming it is turned on. You might want to run the Bluetooth-enabled action.

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;
}
}


Additional Registerforairplanemode methods, such as the following

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);
}
}

Of

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 to assume that Bluetooth is also affected by airplane mode. Changes in flight mode will also enable the Bluetooth service to receive a corresponding broadcast.



Interface switch is Bluetoothenabler.java this class, and setbluetoothenabled () is the detailed switch action. There is a callback function for the switch, such as the following code:

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);
}

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Android4.4 Bluetooth Source code Snippet Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.