蓝牙打开流程

通常两种打开方式,一个是在菜单Setting上打开,一种就是自动打开(default:on)

两种都是最终调用IBluetooth.enable()来打开的,

启动蓝牙BluetoothService

/frameworks/base/services/java/com/android/server/SystemServer.java

startOtherServices()

   mSystemServiceManager.startService(BluetoothService.class);

/frameworks/base/services/core/java/com/android/server/BluetoothService.java

    public BluetoothService(Context context) {

        super(context);

        mBluetoothManagerService = new BluetoothManagerService(context);

    }

/frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java

构造函数,读取保存在Settings里的BT状态值

 loadStoredNameAndAddress();

  if (isBluetoothPersistedStateOn()) {

      if (DBG) {

          Slog.d(TAG, "Startup: Bluetooth persisted state is ON.");

      }

      mEnableExternal = true;

  }

/frameworks/base/services/core/java/com/android/server/BluetoothService.java

public void onBootPhase(int phase) {

      if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {

          publishBinderService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE,

                  mBluetoothManagerService);

      } else if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {

          mBluetoothManagerService.handleOnBootPhase();

      }

  }

/frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java

public void handleOnBootPhase() {

userManagerInternal =

           LocalServices.getService(UserManagerInternal.class);

        userManagerInternal.addUserRestrictionsListener(mUserRestrictionsListener);

        final boolean isBluetoothDisallowed = isBluetoothDisallowed();

        if (isBluetoothDisallowed) {

            return;

        }

        if (mEnableExternal && isBluetoothPersistedStateOnBluetooth()) {

            if (DBG) {

                Slog.d(TAG, "Auto-enabling Bluetooth.");

            }

            sendEnableMsg(mQuietEnableExternal,

                    BluetoothProtoEnums.ENABLE_DISABLE_REASON_SYSTEM_BOOT,

                    mContext.getPackageName());

private void sendEnableMsg(boolean quietMode, int reason, String packageName

       mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_ENABLE, quietMode ? 1 : 0, 0));

BluetoothHandler handleMessage

    case MESSAGE_ENABLE:

  handleEnable(mQuietEnable)

handleEnable

 mHandler.sendMessageDelayed(timeoutMsg, TIMEOUT_BIND_MS);

     Intent i = new Intent(IBluetooth.class.getName());

     if (!doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,

             UserHandle.CURRENT)) {

         mHandler.removeMessages(MESSAGE_TIMEOUT_BIND);

     } else {

         mBinding = true;

     }

 } else if (mBluetooth != null) {

     //Enable bluetooth

     try {

         if (!mQuietEnable) {

             if (!mBluetooth.enable()) {

                 Slog.e(TAG, "IBluetooth.enable() returned false");

             }

         } else {

             if (!mBluetooth.enableNoAutoConnect()) {

                 Slog.e(TAG, "IBluetooth.enableNoAutoConnect() returned false");

             }

         }

你可能感兴趣的:(Bluetooth,android,BT打开流程)