Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android4.4 藍牙源碼部分分析

Android4.4 藍牙源碼部分分析

編輯:關於Android編程

最近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();
mQCallbacks = new RemoteCallbackList();
mStateChangeCallbacks = new RemoteCallbackList();
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文件中定義的,如:


cell,bluetooth,wifi,nfc,wimax


表示如果開啟飛行模式下,哪些服務將會被關閉。所以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);
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved