(一)蓝牙之打开前准备工作

时间:2024-03-13 22:27:24

一.简单粗暴就直接上代码(有机会可以看源码就是这么幸福,我所看的是android 5.0的)

android开机时经过一系列启动进入到SystemServer.java,这里的一系列过程是

Init进程-Zygote进程-SystemServer进程-》各种应用进程具体自己去搜吧,这里就不讲!

SystemServer.java代码在frameworks/base/services/java/com/android/server/,现在我们来看看为什么讲这个呢?别忘了我写这个是有关蓝牙的,这里是开机是启动蓝牙服务的,

import android.bluetooth.BluetoothAdapter;

public final class SystemServer {
  private void startOtherServices() {
    BluetoothManagerService bluetooth = null;
    boolean disableBluetooth = SystemProperties.getBoolean("config.disable_bluetooth", false);
boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");//这里暂时不深入看   try { if (isEmulator) { Slog.i(TAG, "No Bluetooh Service (emulator)"); } else if (mFactoryTestMode == FactoryTest.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);//并添加到android的服务管理中 } } catch (RuntimeException e) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting core service", e); } } static final void startSystemUi(Context context) { Intent intent = new Intent(); intent.setComponent(new ComponentName("com.android.systemui", "com.android.systemui.SystemUIService")); //Slog.d(TAG, "Starting service: " + intent); context.startServiceAsUser(intent, UserHandle.OWNER);//这里调用上面的方法 } }

  接下来我们看一下BluetoothManagerService.java用来做什么?frameworks/base/services/core/java/com/android/server


private String mAddress;
private String mName;
private final ContentResolver mContentResolver;
private final RemoteCallbackList<IBluetoothManagerCallback> mCallbacks;
private final RemoteCallbackList<IBluetoothStateChangeCallback> mStateChangeCallbacks;
private IBluetooth mBluetooth;
private IBluetoothGatt mBluetoothGatt;
private boolean mBinding;
private boolean mUnbinding;
// used inside handler thread
private boolean mQuietEnable = false;
// configuarion from external IBinder call which is used to
// synchronize with broadcast receiver.
private boolean mQuietEnableExternal;
// configuarion from external IBinder call which is used to
// synchronize with broadcast receiver.
private boolean mEnableExternal;
// used inside handler thread
private boolean mEnable;
private int mState;
private final BluetoothHandler mHandler;
private int mErrorRecoveryRetryCounter;
private final int mSystemUiUid;


BluetoothManagerService(Context context) { mHandler
= new BluetoothHandler(IoThread.get().getLooper()); mContext = context; mBluetooth = 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>(); 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); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); mContext.registerReceiver(mReceiver, filter); loadStoredNameAndAddress(); if (isBluetoothPersistedStateOn()) {//看这里,android开机打开蓝牙 mEnableExternal = true; }int sysUiUid = -1; try { sysUiUid = mContext.getPackageManager().getPackageUid("com.android.systemui", UserHandle.USER_OWNER); } catch (PackageManager.NameNotFoundException e) { Log.wtf(TAG, "Unable to resolve SystemUI\'s UID.", e); } mSystemUiUid = sysUiUid; }

  private final boolean isBluetoothPersistedStateOn() {
    return Settings.Global.getInt(mContentResolver,
    Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;  

  }

这个类所做的事初始化与蓝牙相关的准备工作,蓝牙状态保存,蓝牙配置,蓝牙设备信息保存,供后面打开蓝牙使用;初始化完了会调用到app层

下一篇讲bluetooth enable