Android Bluetooth 框架简读 <1>

下面大致看看Android 蓝牙部分.

先大致走读一遍,android系统蓝牙相关代码基本分布:

<1> : 应用层:

Setting工程,Bluetooth工程,Phone工程,这三个工程代码都在~/packages/app目录下(主要是这几个见得多,面熟).

Settings : 这个是用户见过最多的,蓝牙的开关,扫描,配对.

Bluetooth : 实现一部分用户交互(比如蓝牙发送接收文件,一般都会在通知栏出现),重点是它提供了蓝牙各种协议,比如A2DP,HFP,HSP,HID,OPP...的各项服务,所以展开这个工程时,可以发现,工程源码下的每个包基本上都有一个向应用层提供的一个Service.据我自己使用情况,A2DP是播放音乐之类的,HFP/HSP是蓝牙耳机/音响类型,比如HFP就可以支持拨打电话挂机按键(蓝牙耳机上面有按键),当然HSP也有一些简单按键,比如V=/V-.不过这个提供的主要是给Phone那边使用.

这个工程的jni主要是去调用蓝牙的协议栈(android4.2及以后就是bluedroid协议栈),所这个工程是上下跨度,对上提供一些应用操作和服务,对下调用协议栈操作.放在应用层不是很准确.

Phone : RFComm的通信对接的接口和使用时在这边,一些AT命令,这些可以控制电话的挂机取线,音量调节等等,当然会因为HFP和HSP的不同有所差别,一般这个地方需要到实验认证,自己以前为产品去实验室做过一次.

<2> : framework 层 : 看多了android framework源代码,framework最多的就是Service和Manager,所以蓝牙主要的是BluetoothManagerService类在处理蓝牙的操作.即然这是一个服务,那么启动呢?其实和其他服务启动方式都是差不多的.前面涉及打Audio部分的,对于大部分android服务都是在System server中启动:

// Skip Bluetooth if we have an emulator kernel
            // TODO: Use a more reliable check to see if this product should
            // support Bluetooth - see bug 988521
            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 {
                Slog.i(TAG, "Bluetooth Manager Service");
                bluetooth = new BluetoothManagerService(context);
                ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);
            }

如果你对源码稍微好奇一点的话,看了这部分程序,继续看这段程序的上面部分,会发现,基本上有N个服务都在这里一起启动:

AccountManagerService accountManager = null;
        ContentService contentService = null;
        LightsService lights = null;
        PowerManagerService power = null;
        DisplayManagerService display = null;
        BatteryService battery = null;
        VibratorService vibrator = null;
        AlarmManagerService alarm = null;
        MountService mountService = null;
        NetworkManagementService networkManagement = null;
        NetworkStatsService networkStats = null;
        NetworkPolicyManagerService networkPolicy = null;
        ConnectivityService connectivity = null;
        WifiP2pService wifiP2p = null;
        WifiService wifi = null;
        NsdService serviceDiscovery= null;
        IPackageManager pm = null;
        Context context = null;
        WindowManagerService wm = null;
        BluetoothManagerService bluetooth = null;
        DockObserver dock = null;
        UsbService usb = null;
        SerialService serial = null;
        TwilightService twilight = null;
        UiModeManagerService uiMode = null;
        RecognitionManagerService recognition = null;
        ThrottleService throttle = null;
        NetworkTimeUpdateService networkTimeUpdater = null;
        CommonTimeManagementService commonTimeMgmtService = null;
        InputManagerService inputManager = null;
        TelephonyRegistry telephonyRegistry = null;

上面都在这里启动.

具体参看:

framework/base/core/java/android/bluetooth


<3> : 接口层Bluetooth工程 : 基本上上面扯得再多,最终还是有它来和协议层以及底层

<4> : bluedroid协议栈,这个协议栈在4.2开始出现,没有特别研究过.


最后贴一张框架图:copy网上的.












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值