Android Bluetooth HID实现详解

Android 关于蓝牙的部分使用的是BlueZ协议栈。但是直到目前2.3.3都没有扩展HID的profile,只是实现了最基本的Handset和d2dp的profile,所以我们的工作涉及到从应用到jni三层的修改,具体修改文件如图所示,绿色表示新建的类,橙色表示修改的类。

一. 本地层

路径:framework/base/core/jni/

参照android_server_BluetoothA2dpService.cpp新建android_server_bluetoothHidServer.cpp。该类中主要是通过dbus对bluez协议栈的访问,dbus 的通用方法都在android_bluetooth_common.cpp中实现,我们做的仅仅是通过dbus_func_args_async调用到bluez提供的input接口。

主要实现以下两个方法函数:

+ expand source view plain print ? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. staticjbooleanconnectSinkNative(JNIEnv*env,jobjectobject,jstringpath){
  2. #ifdefHAVE_BLUETOOTH
  3. LOGV(__FUNCTION__);
  4. if(nat){
  5. constchar*c_path=env->GetStringUTFChars(path,NULL);
  6. boolret=dbus_func_args_async(env,nat->conn,-1,NULL,NULL,nat,
  7. c_path,"org.bluez.Input","Connect",
  8. DBUS_TYPE_INVALID);
  9. env->ReleaseStringUTFChars(path,c_path);
  10. returnret?JNI_TRUE:JNI_FALSE;
  11. }
  12. #endif
  13. returnJNI_FALSE;
  14. }
  15. staticjbooleandisconnectSinkNative(JNIEnv*env,jobjectobject,
  16. jstringpath){
  17. #ifdefHAVE_BLUETOOTH
  18. LOGV(__FUNCTION__);
  19. if(nat){
  20. constchar*c_path=env->GetStringUTFChars(path,NULL);
  21. boolret=dbus_func_args_async(env,nat->conn,-1,NULL,NULL,nat,
  22. c_path,"org.bluez.Input","Disconnect",
  23. DBUS_TYPE_INVALID);
  24. env->ReleaseStringUTFChars(path,c_path);
  25. returnret?JNI_TRUE:JNI_FALSE;
  26. }
  27. #endif
  28. returnJNI_FALSE;
  29. }
static jboolean connectSinkNative(JNIEnv *env, jobject object, jstring path) {#ifdef HAVE_BLUETOOTH LOGV(__FUNCTION__); if (nat) { const char *c_path = env->GetStringUTFChars(path, NULL); bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat, c_path, "org.bluez.Input", "Connect", DBUS_TYPE_INVALID); env->ReleaseStringUTFChars(path, c_path); return ret ? JNI_TRUE : JNI_FALSE; }#endif return JNI_FALSE;} static jboolean disconnectSinkNative(JNIEnv *env, jobject object, jstring path) {#ifdef HAVE_BLUETOOTH LOGV(__FUNCTION__); if (nat) { const char *c_path = env->GetStringUTFChars(path, NULL); bool ret = dbus_func_args_async(env, nat->conn, -1, NULL, NULL, nat, c_path, "org.bluez.Input", "Disconnect", DBUS_TYPE_INVALID); env->ReleaseStringUTFChars(path, c_path); return ret ? JNI_TRUE : JNI_FALSE; }#endif return JNI_FALSE;}

这里要注意将该文件添加到AndroidRuntime.cpp和Android.mk中,否则不会编译到动态库中。

此部分编译后最终生成libandroid_runtime.so并替换到system/libs下

二.Framework的java部分

路径framework/base/java/android/server/中添加BluetoothHidService.java文件

路径framework/base/java/android/bluetooth/中添加BluetoothHid.java和IBluetoothHid.aidl文件。

view plain print ? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. interfaceIBluetoothHid{
  2. booleanconnect(inBluetoothDevicedevice);
  3. booleandisconnect(inBluetoothDevicedevice);
  4. intgetState(inBluetoothDevicedevice);
  5. booleansetPriority(inBluetoothDevicedevice,intpriority);
  6. intgetPriority(inBluetoothDevicedevice);
  7. }
interface IBluetoothHid { boolean connect(in BluetoothDevice device); boolean disconnect(in BluetoothDevice device); int getState(in BluetoothDevice device); boolean setPriority(in BluetoothDevice device, int priority); int getPriority(in BluetoothDevice device);}

BluetoothHid.java中主要的两个方法connect和disconnect间接地通过aidl访问BluetoothHidService。这里主要是实现跨进程并为上层提供可直接访问的方法。

由此framework的主要部分打包生成framework.Jar并最终部署到system/framework里。

三.应用(Settings.apk)

最后需要修改应用部分,应用部分的修改点比较分散,不想框架层那样整块模仿A2DP的样子那么方便,但也不是说jni部分有多么容易。反而对于我这种对C语言不熟悉的人来说,修改jni是最头疼得事了。好在蓝牙HID 这部分框架层的修改都是整块进行的,理解上还算比价容易。

总的来说在Settings.apk中要修改的文件主要是这么几个:

LocalBluetoothProfileManager.java 这里主要提供一个HID的profile以便应用层访问。建一个HIDProfile的class调用framework中的BluetoothHID。实际上就是通过bender机制调用了BluetoothHidService。

CashedBluetoothDevice中添加显示蓝牙键盘的图标,BluetoothPairingDialog中则需要添加一段蓝牙配对验证处理的代码,我是参照i9000中先弹出一个随机数,然后在键盘中敲入相同的随机数即配对成功,具体实现如下:

+ expand source view plain print ? ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. PrivateviewcreateView(){
  2. if(mType==BluetoothDevice.PAIRING_VARIANT_PIN){
  3. ……
  4. //HID
  5. if(isDeviceKeyboard(mDevice)){
  6. Stringpin=String.format("%06d",Long.valueOf(Math
  7. .abs(newRandom().nextLong()%1000000L)));
  8. mPairingView.setVisibility(View.GONE);
  9. messageView.setText(getString(
  10. R.string.bluetooth_enter_keyboard_pin_msg,pin,name));
  11. byte[]bytePin=BluetoothDevice.convertPinToBytes(pin);
  12. if(bytePin!=null){
  13. mDevice.setPin(bytePin);
  14. }
  15. }
  16. ……
  17. }
Private view createView(){if (mType == BluetoothDevice.PAIRING_VARIANT_PIN) {…… // HID if (isDeviceKeyboard(mDevice)) { String pin = String.format("%06d", Long.valueOf(Math .abs(new Random().nextLong() % 1000000L))); mPairingView.setVisibility(View.GONE); messageView.setText(getString( R.string.bluetooth_enter_keyboard_pin_msg, pin, name)); byte[] bytePin = BluetoothDevice.convertPinToBytes(pin); if (bytePin != null) { mDevice.setPin(bytePin); } }……}

以上为android中实现蓝牙键盘的具体步骤。


更多相关文章

  1. Android(安卓)Studio 开发经验纵览
  2. android manifest 文件Activity配置节中的属性汇总
  3. Android面试笔试集锦
  4. android使用opensl es进行简单的音频播放
  5. Android(安卓)Activity 各函数解析
  6. Android默认闹钟-铃声-通知提示音修改
  7. android sdk 编译--如何将源代码加入android.jar,以及make原理 2
  8. Android中解析读取复杂word,excel,ppt等的方法
  9. Android真机时Failed to install xxx.apk on device '...'(null)

随机推荐

  1. Android处理屏幕旋转时的解决方案
  2. Android格式化外部设备如USB等
  3. 【知识点】android中的adapter
  4. Android 开发入门
  5. android横屏竖屏处理
  6. Android图案密码,手势锁源码解析
  7. Android(安卓)文本阅读以及TextView指定
  8. Android UI开发第九篇——SlidingDrawer
  9. Android: 你必须掌握的Android命令
  10. android Spinner和数值选择器使用demo