ProfileService Initialization

1. ProfileService 啟動(dòng)方式

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 為例,實(shí)際上分析的是所有具體的 profile service 的通用啟動(dòng)流程。
  2. 啟動(dòng)方式是 startService(),因此執(zhí)行的 Service 的生命周期方法是 onCreate() 和 onStartCommand()。
  3. 啟動(dòng)服務(wù)攜帶的 action 信息是 ACTION_SERVICE_STATE_CHANGED,state 是 BluetoothAdapter.STATE_ON。

2. ProfileService 啟動(dòng)流程

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();            // 由實(shí)現(xiàn)類(lèi)來(lái)創(chuàng)建 Binder 客戶(hù)端代理對(duì)象的方法
        create();                          // 執(zhí)行實(shí)現(xiàn)類(lèi)需要在 onCreate() 中執(zhí)行的事務(wù)
    }



    @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() 中執(zhí)行的內(nèi)容,已經(jīng)在注釋中分析;
  2. 在 onStartCommand() 中執(zhí)行的內(nèi)容是 doStart() 方法。

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

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

        // 2  user 相關(guān)處理
        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 啟動(dòng)子類(lèi)Service,該方法由子類(lèi)去實(shí)現(xiàn)
        mProfileStarted = start();

        // 4 ProfileService 狀態(tài)變化更新
        mAdapterService.onProfileServiceStateChanged(this, BluetoothAdapter.STATE_ON);
    }

對(duì)于 ProfileService 的啟動(dòng)而言:

  1. 首先,需要在 AdapterService 中記錄其已注冊(cè)狀態(tài);
  2. 然后,啟動(dòng)該服務(wù);
  3. 最后,需要在 AdapterService 中記錄其已啟動(dòng)狀態(tài),然后執(zhí)行下一步操作。

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 相關(guān)內(nèi)容

暫不分析

2.3 start()

start() 方法由子類(lèi)去實(shí)現(xiàn),去啟動(dòng)具體的藍(lán)牙profile服務(wù)。

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())) {
                        // 啟動(dòng) GATT service 時(shí),走該分支,繼續(xù)執(zhí)行 enable
                        enableNative();
                    } else if (mRegisteredProfiles.size() == Config.getSupportedProfiles().length
                            && mRegisteredProfiles.size() == mRunningProfiles.size()) {
                        // 所有支持的 profile service 啟動(dòng)后,走該分支
                        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;
......
            }
        }
    }
}

以上代碼內(nèi)容,不僅是 ProfileService 的通用流程,還是藍(lán)牙enable的整體流程的內(nèi)容。

  1. 若 GattService 啟動(dòng)完成,則執(zhí)行 enableNative();
  2. 若所有支持的 profile 啟動(dòng)完成,則執(zhí)行 TurningBleOn 的善后工作、并開(kāi)始 TurningOn 流程,此處留待其他文章分析。

3. Summary

ProfileService Arch
  1. ProfileService 的執(zhí)行流程如上圖所示;
  2. initBinder() 與 create() 主要是初始化Binder和子類(lèi)自身業(yè)務(wù);
  3. ProfileService子類(lèi)啟動(dòng)之前,需要先將 service 注冊(cè)到 AdapterService 中;
  4. 其中最重要的方法是 start(),由其子類(lèi)去實(shí)現(xiàn),用于啟動(dòng)子類(lèi);
  5. ProfileService子類(lèi)啟動(dòng)完成后,在 AdapterServcie 將其更新到 STATE_ON 狀態(tài);
  6. 除此之外,若是 GattService 啟動(dòng)完成 或者 所有支持的 Profile 啟動(dòng)完成,則還會(huì)執(zhí)行藍(lán)牙 enable 整體流程。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容