歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 4.4 藍牙源碼部分分析

Android 4.4 藍牙源碼部分分析

日期:2017/3/1 9:41:06   编辑:Linux編程

最近GOOGLE發布了Android4.4,看了一下源碼;4.4的藍牙打開流程這一部分還是有些變化的,從界面上看藍牙開關就是設置settings裡那個switch開關,widget開關當然也可以,起點不同後續的流程是一樣的。先來看systemServer.java的代碼,藍牙服務開啟的地方,真機情況下我們關心的是最後一個else分支。

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

看下bluetoothManagerService的構造方法,我們看三個地方, loadStoredNameAndAddress()是讀取藍牙打開默認名稱的地方,isBluetoothPersistedStateOn()是判斷是否已打開藍牙的,如果已打開,後續操作要執行開啟藍牙的動作

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

另外的registerForAirplaneMode方法,如下

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

其中

Settings.Global.getString(resolver,
Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS)


獲取到的值是在String文件中定義的,如:

<!-- Comma-separated list of bluetooth, wifi, and cell. -->
<string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>

表示如果開啟飛行模式下,哪些服務將會被關閉。所以registerForAirplaneMode方法就是在如果藍牙也受飛行模式影響,那麼飛行模式的變化也將使藍牙服務收到相應廣播。

界面上開關就是BluetoothEnabler.java這個類了,而setBluetoothEnabled()則是具體開關動作。其中有開關的回調函數,代碼如下:

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

Android 4.4.4 發布下載 http://www.linuxidc.com/Linux/2014-06/103467.htm

最簡單的Ubuntu Touch & Android 雙系統安裝方式 http://www.linuxidc.com/Linux/2014-01/94881.htm

在Nexus上實現Ubuntu和Android 4.4.2 雙啟動 http://www.linuxidc.com/Linux/2014-05/101849.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved