1 蓝牙服务的启动
蓝牙服务作为系统服务,启动是从systemserver里面启动的,systemserver里面的main方法被调用,该方法运行run方法,代码如下所示:
public static void main(String[] args) {
new SystemServer().run();
}
if (isEmulator) {
Slog.i(TAG, "No Bluetooh Service (emulator)");
} else if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
Slog.i(TAG, "No Bluetooth Service (factory test)");
} else if (!context.getPackageManager().hasSystemFeature
(PackageManager.FEATURE_BLUETOOTH)) {
Slog.i(TAG, "No Bluetooth Service (Bluetooth Hardware Not Present)");
} else if (disableBluetooth) {
Slog.i(TAG, "Bluetooth Service disabled by config");
} else {
Slog.i(TAG, "Bluetooth Service");
mSystemServiceManager.startService(BluetoothService.class); // 启动蓝牙服务
bluetoothservice里面的onstart方法是将蓝牙服务注册到servicemanager,如下代码
public BluetoothService(Context context) {
super(context);
mBluetoothManagerService = new BluetoothManagerService(context);
//构造BluetoothManagerService对象
}
@Override
public void onStart() {
Log.d(TAG, "onStart: publishing BluetoothManagerService");
publishBinderService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, mBluetoothManagerService);// 将蓝牙服务注册到servicemanager
}
至此,蓝牙服务已经启动,即蓝牙服务已经被注册到servicemanager里面了,使用蓝牙服务的时候,调用getsystemservice方法获取蓝牙服务的binder引用。
2 打开蓝牙
不管是第三方应用,还是用户从设置,systemui打开蓝牙,最终调用到BluetoothManagerService里面的enable方法
在该函数中,会向BluetoothHandler发送MESSAGE_ENABLE消息,在BluetoothHandler中处理MESSAGE_ENABLE消息
处理