從Android 4.2開始,Bluetooth stack發生了重大改變:從Bluez換成了由Google和Broadcom聯合開發的Bluedroid(固然,核心的部分仍是Broadcom 的,Google主要是作了和上層Framework相關的部分)。經過http://source.android.com/devices/bluetooth.html能夠大概瞭解新的Bluetooth stack的架構,總的來講相關文檔不多,主要靠閱讀代碼進行深刻了解。 Bluedroid和Bluez相比,有以下優勢:
html
層次結構清晰。各個profile對上層接口統一,便於增長新的profile;增長了HAL層,便於移植。 java
去掉了DBus,Framework的Java代碼直接調用到Bluedroid的Native代碼。
android
可是Android 4.2中的Bluedroid與Android 4.1中的Bluez相比,功能要少,例如不支持AVRCP 1.3; Bug較多,例如某些藍牙耳機不能重撥最後一個電話。最重要的是4.2的Bluedroid不支持BLE。不過在剛剛發佈的Android 4.3中已經有了不少改進,AVRCP 1.3和BLE都獲得了支持。
目前有一些Android 4.1或4.2的設備是支持BLE的,可是都是採用的Vendor本身的解決方案,好比Bluetooth stack採用Bluez 5.x,再提供Vendor BLE Android SDK. 如今Android 4.3已經發布,從將來發展趨勢來看,若是有人要學習Bluetooth in Android,建議不要再研究Bluez,最好轉向Bluedroid。
如下是Android 4.2中Bluetooth相關代碼之分佈:
架構
android.bluetooth | frameworks/base/core/java/android/bluetooth | implements public API for the Bluetooth adapter and profiles |
Bluetooth system service | packages/apps/Bluetooth/src | implements service and profiles at the Android fraework layer |
Bluetooth JNI | packages/apps/Bluetooth/jni | defines Bluetooth adapter and profiles service JNI: calls into HAL and receives callback from HAL |
Bluetooth HAL | hardware/libhardware/include/hardware/bt_*.h files | defines the standard interface that the android.bluetooth adapter and profiles APIs |
Bluetooth stack | external/bluetooth/bluedroid | implement bluetooth stack: core and profiles |
以Pan profile爲例,咱們能夠看看代碼的具體分佈和類及文件的命名方式:
app
android.bluetooth | frameworks | public class BluetoothPan implements BluetoothProfile |
Bluetooth System Service | packages/apps | public class PanService extends ProfileService |
Bluetooth JNI | packages/apps | com_android_bluetooth_pan.cpp |
Bluetooth HAL | hardware/libhardware | include/hardware/bt_pan.h |
Bluetooth stack | external/bluetooth | bluedroid/btif/src/btif_pan.c(implements bt_pan.h) |
bluedroid/bta/pan (Broadcom BTA) | ||
bluedroid/stack/pan (Broadcom BTE) |