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);
}
}
- 上方代碼為以 GattService 為例粥脚,實(shí)際上分析的是所有具體的 profile service 的通用啟動流程蔗牡。
- 啟動方式是 startService(),因此執(zhí)行的 Service 的生命周期方法是 onCreate() 和 onStartCommand()瓦胎。
- 啟動服務(wù)攜帶的 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(); // 由實(shí)現(xiàn)類來創(chuàng)建 Binder 客戶端代理對象的方法
create(); // 執(zhí)行實(shí)現(xiàn)類需要在 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;
}
}
- 在 onCreate() 中執(zhí)行的內(nèi)容,已經(jīng)在注釋中分析姨俩;
- 在 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 啟動子類Service师郑,該方法由子類去實(shí)現(xiàn)
mProfileStarted = start();
// 4 ProfileService 狀態(tài)變化更新
mAdapterService.onProfileServiceStateChanged(this, BluetoothAdapter.STATE_ON);
}
對于 ProfileService 的啟動而言:
- 首先,需要在 AdapterService 中記錄其已注冊狀態(tài)调窍;
- 然后宝冕,啟動該服務(wù);
- 最后邓萨,需要在 AdapterService 中記錄其已啟動狀態(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() 方法由子類去實(shí)現(xiàn)缔恳,去啟動具體的藍(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())) {
// 啟動 GATT service 時,走該分支歉甚,繼續(xù)執(zhí)行 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;
......
}
}
}
}
以上代碼內(nèi)容,不僅是 ProfileService 的通用流程铃芦,還是藍(lán)牙enable的整體流程的內(nèi)容雅镊。
- 若 GattService 啟動完成,則執(zhí)行 enableNative()刃滓;
- 若所有支持的 profile 啟動完成仁烹,則執(zhí)行 TurningBleOn 的善后工作、并開始 TurningOn 流程咧虎,此處留待其他文章分析卓缰。
3. Summary
ProfileService Arch
- ProfileService 的執(zhí)行流程如上圖所示;
- initBinder() 與 create() 主要是初始化Binder和子類自身業(yè)務(wù)砰诵;
- ProfileService子類啟動之前征唬,需要先將 service 注冊到 AdapterService 中;
- 其中最重要的方法是 start()茁彭,由其子類去實(shí)現(xiàn)总寒,用于啟動子類;
- ProfileService子類啟動完成后理肺,在 AdapterServcie 將其更新到 STATE_ON 狀態(tài)摄闸;
- 除此之外,若是 GattService 啟動完成 或者 所有支持的 Profile 啟動完成妹萨,則還會執(zhí)行藍(lán)牙 enable 整體流程年枕。