参考:Android -- Bluetooth framework启动过程简析_第一序列丶的博客-CSDN博客
framework层包括 api、service、bluetooth.apk,这里大致记录下它们间的联系
1、mBluetooth和mBluetoothBinder生成的
case MESSAGE_BLUETOOTH_SERVICE_CONNECTED: {
IBinder service = (IBinder) msg.obj;
mBinding = false;
mBluetoothBinder = service;
mBluetooth = IBluetooth.Stub.asInterface(Binder.allowBlocking(service));
2、BluetoothAdapter与BluetoothManagerService.java建立联系,注册回调
BluetoothAdapter.java
system/bt/binder/android/bluetooth/IBluetoothManager.aidl
private IBluetooth mService; 这个是链接bluetooth.apk的service
BluetoothAdapter(IBluetoothManager managerService) {
mService = managerService.registerAdapter(mManagerCallback); //这儿返回的mService有可能是空
}
private final IBluetoothManagerCallback mManagerCallback =
new IBluetoothManagerCallback.Stub() {
public void onBluetoothServiceUp(IBluetooth bluetoothService) {
mService = bluetoothService;
}
}
3、各个模块监听BluetoothState的状态
在BluetoothManagerService.java
回调注册:
public void registerStateChangeCallback(IBluetoothStateChangeCallback callback) {
Message msg = mHandler.obtainMessage(MESSAGE_REGISTER_STATE_CHANGE_CALLBACK);
msg.obj = callback;
mHandler.sendMessage(msg);
}
case MESSAGE_REGISTER_STATE_CHANGE_CALLBACK: {
IBluetoothStateChangeCallback callback =
(IBluetoothStateChangeCallback) msg.obj;
mStateChangeCallbacks.register(callback);
break;
}
回调调用:
private final IBluetoothCallback mBluetoothCallback = new IBluetoothCallback.Stub() {
public void onBluetoothStateChange(int prevState, int newState) throws RemoteException {
Message msg =
mHandler.obtainMessage(MESSAGE_BLUETOOTH_STATE_CHANGE, prevState, newState);
mHandler.sendMessage(msg);
}
};
case MESSAGE_BLUETOOTH_STATE_CHANGE: {
int prevState = msg.arg1;
int newState = msg.arg2;
mState = newState;
bluetoothStateChangeHandler(prevState, newState);
private void sendBluetoothStateCallback(boolean isUp) {
int n = mStateChangeCallbacks.beginBroadcast();
for (int i = 0; i < n; i++) {
try {
mStateChangeCallbacks.getBroadcastItem(i).onBluetoothStateChange(isUp);
} catch (RemoteException e) {
Slog.e(TAG, "Unable to call onBluetoothStateChange() on callback #" + i, e);
}
}
}
各个模块注册监听bluetooth状态
比如:BluetoothProfileConnector.java
void connect(Context context, BluetoothProfile.ServiceListener listener) {
IBluetoothManager mgr = BluetoothAdapter.getDefaultAdapter().getBluetoothManager();
mgr.registerStateChangeCallback(mBluetoothStateChangeCallback);
doBind();
}
4、使能蓝牙开关
BluetoothAdapter.java
enable() , disable(),enableBLE() 可以使能蓝牙,调用到bluetoothmanagerservice.java
BluetoothManagerService.java
public boolean enable(String packageName) throws RemoteException {
sendEnableMsg(false,
BluetoothProtoEnums.ENABLE_DISABLE_REASON_APPLICATION_REQUEST, packageName);
}
case MESSAGE_ENABLE:
handleEnable(mQuietEnable);
5、bluetoothmanagerservice.java与bluetooth.apk建立联系
private class BluetoothServiceConnection implements ServiceConnection {
private BluetoothServiceConnection() {
}
public void onServiceConnected(ComponentName componentName, IBinder service) {
String name = componentName.getClassName();
if (name.equals("com.android.bluetooth.btservice.AdapterService")) {
msg.arg1 = 1;
} else {
if (!name.equals("com.android.bluetooth.gatt.GattService")) {
Slog.e("wqy--BluetoothManagerService", "Unknown service connected: " + name);
}
}
}
public void onServiceDisconnected(ComponentName componentName) {
}
}
private BluetoothManagerService.BluetoothServiceConnection mConnection = new BluetoothManagerService.BluetoothServiceConnection();
private void handleEnable(boolean quietMode) {
if (!doBind(i, mConnection, Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT,
UserHandle.CURRENT)) {}
}
boolean doBind(Intent intent, ServiceConnection conn, int flags, UserHandle user) {
Slog.e(TAG, "doBind....");
ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
intent.setComponent(comp);
if (comp == null || !mContext.bindServiceAsUser(intent, conn, flags, user)) {
Slog.e(TAG, "Fail to bind to: " + intent);
return false;
}
return true;
}