Android 源码分析 - 蓝牙 - Java层

core/bluetooth

        源码位于:framework/base/core/java/android/bluetooth

        BluetoothManager管理BluetoothAdapter。

        BluetoothAdapter通过IBluetoothManager(“bluetooth_manager”)控制蓝牙打开关闭,获取名称、地址;通过IBluetooth(AdapterService)管理本地蓝牙设备,搜索,配对远程设备。BluetoothAdapter管理搜索请求LeScanCallback(通过GattCallbackWrapper封装)。

        BluetoothProfile代表蓝牙的一个规范,响应蓝牙开关状态,与对应的Profile服务建立、断开连接,转发用户请求。通过ServiceListener接口返回服务连接状态。

        BluetoothProfile子类:

规范

说明

HSP

BluetoothHeadset

A2DP

BluetoothA2dp

HID

BluetoothInputDevice

PAN

BluetoothPan

HDP

BluetoothHealth

MAP

BluetoothMap

GATT

BluetoothGatt

扫描、连接远端服务

GATTS

BluetoothGattServer

本地注册服务

PBAP

BluetoothPbap

        BluetoothClass代表一个蓝牙设备分类,内部有一个设备(Device)分类、子分类属性,以及若干服务(Service)分类。

        BluetoothDevice 远程设备,配对,创建套接字连接。

        BluetoothSocket保存蓝牙设备对象、套接字类型(RFCOMM、SCO、L2CAP)、端口、uuid、连接的文件描述符。通过IBluetooth创建侦听端口,建立连接,使用LocalSocket包装返回的文件描述符。

        BluetoothInputStream包装BluetoothSocket,实现InputStream接口。

        BluetoothOutputStream包装BluetoothSocket,实现OutputStream接口。

        BluetoothServerSocket包装侦听BluetoothSocket。

        BluetoothTetheringDataTracker继承BaseNetworkStateTracker,跟踪蓝牙Modem数据连接状态,由ConnectivityService创建。内部获取BluetoothPan(个人局域网)代理对象。

services/bluetooth_manager

        源代码位于:framework/base/services/java/com/android/server。

        BluetoothManagerService实现IBluetoothManager接口,与   AdapterService和GattService 建立连接,使用其提供的底层功能。通过动作过滤器(action filter)搜索,名称为IBluetooth 、IBluetoothGatt的类名。这两个服务都实现在Bluetooth.apk 中:

        com.android.bluetooth.btservice.AdapterService

        com.android.bluetooth.gatt.GattService

        BluetoothManagerService通过RemoteCallbackList协助管理BluetoothAdapter客户端(IBluetoothManagerCallback)、IBluetoothStateChangeCallback(BluetoothProfile相关)。在IO线程(IoThread)处理事件。

        服务由SystemServer启动,运行在system_server进程中。

framework/base/services/java/com/android/server/SystemServer.java:

-------------------------------------------------------------------------

public void initAndLoop() {

    // ……

        if (SystemProperties.get("ro.kernel.qemu").equals("1")) {

            Slog.i(TAG, "No Bluetooh Service (emulator)");

        } else if (factoryTest == SystemServer.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 Manager Service");

            bluetooth = new BluetoothManagerService(context);

            ServiceManager.addService(

                BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);

        }

    // ……

}

Bluetooth.apk

        源代码位于:packages/apps/Bluetooth。

btservice

        AdapterServiceBinder实现IBluetooth接口,转发给AdapterService。

        AdapterService通过AdapterServiceBinder暴露服务功能,创建AdapterState、BondStateMachine处理请求。

        AdapterProperties管理本地蓝牙设备信息,状态变化时发送广播。

        AdapterState管理蓝牙打开、关闭。

        BondStateMachine配对远程设备,能够同时处理多个配对、解除配对请求。

        ProfileService协助实现各个Profile相关的服务。

a2dp

        A2dpService通过BluetoothA2dpBinder暴露服务功能,创建A2dpStateMachine处理请求。

        A2dpStateMachine。

        Avrcp支持AVRCP规范。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fighting Horse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值