欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

Android Bluetooth HID完成详解,androidhid

来源: 开发者 投稿于  被查看 43977 次 评论:40

Android Bluetooth HID完成详解,androidhid


Android Bluetooth HID落实详解

Android 关于蓝牙的局部运用的是BlueZ协定栈。然而直到眼前2.3.3都不曾伸展HID的profile,只是完成了最大致的Handset和d2dp的profile

1.[Java]代码

一. 当地层

路径: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接口,AS3技术与提醒23-28。

重要得逞以下两个方式函数:
tatic 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,<a href="http://www.bo-carter.info/" style="color: black; text-decoration: none; font-size: 8pt;">羊毛被</a>, 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文件。
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)

最终必要修正利用局部,使用部分的更正点对照疏散,GTK+的编译还真烦琐,不想框架层那样整块模拟A2DP的样子那么适宜,但也不是说jni局部有多么轻易。反而对于我这种对C语言不熟知的人来说,批改jni是最头疼得事了。好在蓝牙HID 这局部框架层的改动都是整块举行的,领会上还算比价简单。

总的来说在Settings.apk中要修正的文件首要是这样几个:

LocalBluetoothProfileManager.java 此地首要供给一个HID的profile以便利用层走访。建一个HIDProfile的class调用framework中的BluetoothHID。切实上即使穿过bender机制调用了BluetoothHidService。

CashedBluetoothDevice中添加显现蓝牙键盘的图标,雅漾,BluetoothPairingDialog中则必要添加一段蓝牙配对印证解决的代码,我是比照i9000中先弹出一个随机数,然后在键盘中敲入相像的随机数即配对胜利,详细落实如下:
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中完成蓝牙键盘的详细环节。

参看博客:Android 上得逞蓝牙的一些Profile blog.163.com/hancker_31/blog/static/3558736120113271253306/即使一个大广度的问题,这时,作为一种组织方式,“面向对象”不能帮我们些什么吗? 

用户评论