Android bluetooth 現狀
截止到android2.2,Android只實現了Handset/Handfree和A2DP/AVRCP等Profile,而其它常用的 Profile如HID/DUN/SPP/OPP/FTP/PAN等卻沒有實現。Android的Handset/Handfree的實現方式和A2DP/AVRCP的方式有很大的不同,Handset/Handfree是直接在bluez的RFCOMM Socket上開發的,沒有利用bluez的audio plugin,而A2DP/AVRCP是在bluez的audio plugin基礎上開發的,所以大大降低了實現的難度。
HID 庫編譯
HID要用到bluez的input plugin,Android已經把它編譯進去了。目錄在system/lib/bluez-plugin/input.so下。
HID JNI層
參照frameworks/base/core/jni/android_server_BluetoothA2dpService.cpp,自己寫一個 HID用的的.cpp文件,其中跟A2DP一樣利用DBUS調用input.so庫的CreateDevice/Connect/Disconnect等函數,具體源碼在external/bluez/utils/input/manager.c和external/bluez/utils/input /device.c中。
HID Java框架層
參照frameworks/base/core/java/android/server/BluetoothA2dpService.java和 frameworks/base/core/java/android/bluetooth/BluetoothA2dp.java及 frameworks/base/core/java/android/bluetooth/IBluetoothA2dp.aidl,自己分別寫兩個 JAVA類及AIDL接口。如下:HID AIDL接口
package android.bluetooth; import android.bluetooth.BluetoothDevice; /** * System private API for Bluetooth HID service * * {@hide} */ interface IBluetoothHid { boolean connectInputDevice(in BluetoothDevice device); boolean disconnectInputDevice(in BluetoothDevice device); BluetoothDevice[] getConnectedInputs(); // change to Set<> once AIDL supports BluetoothDevice[] getNonDisconnectedInputs(); // change to Set<> once AIDL supports int getInputState(in BluetoothDevice device); boolean setInputPriority(in BluetoothDevice device, int priority); int getInputPriority(in BluetoothDevice device); }
餘下的就是在packages/apps/Settings/src/com/android/settings/bluetooth目錄下的各個文件的修改了,搜一下A2DP,只要是A2DP要修改的地方照葫蘆畫瓢添加修改就是了。
打開kernel uinput接口
打開.config配置文件,設置CONFIG_INPUT_UINPUT=y。如果實現藍牙鼠標和鍵盤profile成功,會生成類似/dev/input/mouse1和/dev/input/event5虛擬接口。
實現鍵盤和鼠標事件處理
在android的事件處理機制中實現鼠標按鍵事件和移動事件。鍵盤默認已經實現。