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的事件处理机制中实现鼠标按键事件和移动事件。键盘默认已经实现。