ProfileService Initialization

2022-05-03  本文已影响0人  MelanDawn

1. ProfileService 启动方式

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

public class AdapterService extends Service {

    void bringUpBle() {
......
        //Start Gatt service
        setProfileServiceState(GattService.class, BluetoothAdapter.STATE_ON);
    }


    private void setProfileServiceState(Class service, int state) {
        Intent intent = new Intent(this, service);
        intent.putExtra(EXTRA_ACTION, ACTION_SERVICE_STATE_CHANGED);
        intent.putExtra(BluetoothAdapter.EXTRA_STATE, state);
        startService(intent);
    }
}
  1. 上方代码为以 GattService 为例,实际上分析的是所有具体的 profile service 的通用启动流程。
  2. 启动方式是 startService(),因此执行的 Service 的生命周期方法是 onCreate() 和 onStartCommand()。
  3. 启动服务携带的 action 信息是 ACTION_SERVICE_STATE_CHANGED,state 是 BluetoothAdapter.STATE_ON。

2. ProfileService 启动流程

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/ProfileService.java

public abstract class ProfileService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mBinder = initBinder();            // 由实现类来创建 Binder 客户端代理对象的方法
        create();                          // 执行实现类需要在 onCreate() 中执行的事务
    }



    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
......
        String action = intent.getStringExtra(AdapterService.EXTRA_ACTION);
        if (AdapterService.ACTION_SERVICE_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            if (state == BluetoothAdapter.STATE_OFF) {
                doStop();
            } else if (state == BluetoothAdapter.STATE_ON) {
                doStart();
            }
        }
        return PROFILE_SERVICE_MODE;
    }
}
  1. 在 onCreate() 中执行的内容,已经在注释中分析;
  2. 在 onStartCommand() 中执行的内容是 doStart() 方法。

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/ProfileService.java

    private void doStart() {
......
        mAdapterService = AdapterService.getAdapterService();
        // 1
        mAdapterService.addProfile(this); 

        // 2  user 相关处理
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_SWITCHED);
        filter.addAction(Intent.ACTION_USER_UNLOCKED);
        mUserSwitchedReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                final String action = intent.getAction();
                final int userId =
                        intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
                if (userId == UserHandle.USER_NULL) {
                    Log.e(mName, "userChangeReceiver received an invalid EXTRA_USER_HANDLE");
                    return;
                }
                if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                    Log.d(mName, "User switched to userId " + userId);
                    setCurrentUser(userId);
                } else if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) {
                    Log.d(mName, "Unlocked userId " + userId);
                    setUserUnlocked(userId);
                }
            }
        };
        getApplicationContext().registerReceiver(mUserSwitchedReceiver, filter);
        int currentUserId = ActivityManager.getCurrentUser();
        setCurrentUser(currentUserId);
        UserManager userManager = UserManager.get(getApplicationContext());
        if (userManager.isUserUnlocked(currentUserId)) {
            setUserUnlocked(currentUserId);
        }

        // 3 启动子类Service,该方法由子类去实现
        mProfileStarted = start();

        // 4 ProfileService 状态变化更新
        mAdapterService.onProfileServiceStateChanged(this, BluetoothAdapter.STATE_ON);
    }

对于 ProfileService 的启动而言:

  1. 首先,需要在 AdapterService 中记录其已注册状态;
  2. 然后,启动该服务;
  3. 最后,需要在 AdapterService 中记录其已启动状态,然后执行下一步操作。

2.1 addProfile

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

public class AdapterService extends Service {

    public void addProfile(ProfileService profile) {
        mHandler.obtainMessage(MESSAGE_PROFILE_SERVICE_REGISTERED, profile).sendToTarget();
    }



    class AdapterServiceHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_PROFILE_SERVICE_REGISTERED:
                    registerProfileService((ProfileService) msg.obj);
                    break;
                ......
            }
        }



        private void registerProfileService(ProfileService profile) {
            ......
            mRegisteredProfiles.add(profile);
        }

    }
}

2.2 user 相关内容

暂不分析

2.3 start()

start() 方法由子类去实现,去启动具体的蓝牙profile服务。

2.4 onProfileServiceStateChanged

packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java

public class AdapterService extends Service {

    public void onProfileServiceStateChanged(ProfileService profile, int state) {
        ......
        Message m = mHandler.obtainMessage(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);
        m.obj = profile;
        m.arg1 = state;               // BluetoothAdapter.STATE_ON
        mHandler.sendMessage(m);
    }



    class AdapterServiceHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_PROFILE_SERVICE_STATE_CHANGED:
                    processProfileServiceStateChanged((ProfileService) msg.obj, msg.arg1);
                    break;
                ......
            }
        }



        private void processProfileServiceStateChanged(ProfileService profile, int state) {
            switch (state) {
                case BluetoothAdapter.STATE_ON:
                    ......
                    mRunningProfiles.add(profile);

                    if (GattService.class.getSimpleName().equals(profile.getName())) {
                        // 启动 GATT service 时,走该分支,继续执行 enable
                        enableNative();
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
                            && mRegisteredProfiles.size() == mRunningProfiles.size()) {
                        // 所有支持的 profile service 启动后,走该分支
                        mAdapterProperties.onBluetoothReady();
                        updateUuids();
                        setBluetoothClassFromConfig();
                        initProfileServices();
                        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_LOCAL_IO_CAPS);
                        getAdapterPropertyNative(AbstractionLayer.BT_PROPERTY_LOCAL_IO_CAPS_BLE);
                        mAdapterStateMachine.sendMessage(AdapterState.BREDR_STARTED);
                    }
                    break;
......
            }
        }
    }
}

以上代码内容,不仅是 ProfileService 的通用流程,还是蓝牙enable的整体流程的内容。

  1. 若 GattService 启动完成,则执行 enableNative();
  2. 若所有支持的 profile 启动完成,则执行 TurningBleOn 的善后工作、并开始 TurningOn 流程,此处留待其他文章分析。

3. Summary

ProfileService Arch
  1. ProfileService 的执行流程如上图所示;
  2. initBinder() 与 create() 主要是初始化Binder和子类自身业务;
  3. ProfileService子类启动之前,需要先将 service 注册到 AdapterService 中;
  4. 其中最重要的方法是 start(),由其子类去实现,用于启动子类;
  5. ProfileService子类启动完成后,在 AdapterServcie 将其更新到 STATE_ON 状态;
  6. 除此之外,若是 GattService 启动完成 或者 所有支持的 Profile 启动完成,则还会执行蓝牙 enable 整体流程。
上一篇 下一篇

猜你喜欢

热点阅读