Android bluetooth

来源:互联网 发布:js只能输入数字 编辑:程序博客网 时间:2024/04/26 15:56

在SystemServer启动的时候,会生成一个BluetoothDeviceService的实例

 

//skip bluetooth if we have an emulator kernel

//TODO:Use a more reliable check to see if this product should
// support Bluetooth
if(SystemProperties.get("ro.kernel.qemu").equals("1")){
    Log.i(TAG, "Registering null Bluetooth Service(Emulator)");
    ServiceManager.addService(Context.BLUETOOTH_SERVICE,null);
} else if(factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL) {
    Log.i(TAG, "Registering null Bluetooth Service(factory test)");
    ServiceManager.addService(Context.BLUETOOTH_SERVICE,null);
} else {
    Log.i(TAG, "Starting Bluetooth Service");
    bluetooth = new BluetoothDeviceService(context);
    bluetooth.init();
    ServiceManager.addService(Context.BLUETOOTH_SERVICE,bluetooth);
    
    int bluetoothOn = Settings.System.getInt(mContentResolver,Settings.System.BLUETOOTH_ON, 0);
    if( bluetoothOn > 0){
        bluetooth.enable(null);
    }
}

 

BluetoothDeviceService会生成一个BluetoothEventLoop的实例,它们两者均通过DBUS来和
BlusZ通信。BluetoothDeviceService是通过DBUS向BlueZ发送命令,儿命令的返回结果则是
由BlueZ通过DBUS传回给BluetoothEventLoop的(具体交互请参加BlueZ的dbus_api.txt),
BlueZ也会通过DBUS向BluetoothEventLoop发送一些事件通知。BluetoothEventLoop和外部
的接口是通过预先定义的Intent,
初始的时候蓝牙是没有使能的,是通过BluetoothSettings或者WirelessSettings来打开蓝牙设备
,然后通过BluetoothSettings去查找附近的其他蓝牙设备,找到后可以建立RFCOMM连接和配对。