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

Android Bluetooth HID完成详解,androidhid

来源: 开发者 投稿于  被查看 41909 次 评论:195

Android Bluetooth HID完成详解,androidhid


[代码] [Java]代码

代码来源:http://www.oschina.net/code/snippet_103631_5776

view source print ?
01 一. 当地层
02
03 路径:framework/base/core/jni/
04
05 比照android_server_BluetoothA2dpService.cpp修建 android_server_bluetoothHidServer.cpp。该类中首要是穿过dbus对bluez协定栈的走访,dbus 的通用方式都在android_bluetooth_common.cpp中完成,我们做的仅仅是经过dbus_func_args_async调用到 bluez供给的input接口,AS3技术与提醒23-28
06
07 重要得逞以下两个方式函数:
08 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;}
09 此地要留神将该文件增加到AndroidRuntime.cpp和Android.mk中,不然不会编译到动态库中。
10
11 此局部编译后最终身成libandroid_runtime.so并轮换到system/libs下
12 二.Framework的java部分
13
14 路径framework/base/java/android/server/中增加BluetoothHidService.java文件
15
16 路径framework/base/java/android/bluetooth/中增加BluetoothHid.java和IBluetoothHid.aidl文件。
17 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);}
18 BluetoothHid.java中重要的两个措施connect和disconnect间接地穿过aidl探访BluetoothHidService。此地主要是得逞跨过程并为上层供给可直接走访的计策。
19
20 由此framework的重要局部打包生成framework.Jar并最后策划到system/framework里。
21 三.使用(Settings.apk)
22
23 最终必要修正利用局部,使用部分的更正点对照疏散,GTK+的编译还真烦琐,不想框架层那样整块模拟A2DP的样子那么适宜,但也不是说jni局部有多么轻易。反而对于我这种对C语言不熟知的人来说,批改jni是最头疼得事了。好在蓝牙HID 这局部框架层的改动都是整块举行的,领会上还算比价简单。
24
25 总的来说在Settings.apk中要修正的文件首要是这样几个:
26
27 LocalBluetoothProfileManager.java 此地首要供给一个HID的profile以便利用层走访。建一个HIDProfile的class调用framework中的BluetoothHID。切实上即使穿过bender机制调用了BluetoothHidService。
28
29 CashedBluetoothDevice中添加显现蓝牙键盘的图标,雅漾,BluetoothPairingDialog中则必要添加一段蓝牙配对印证解决的代码,我是比照i9000中先弹出一个随机数,然后在键盘中敲入相像的随机数即配对胜利,详细落实如下:
30 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); } }……}
31 以上为android中完成蓝牙键盘的详细环节。
32
33 参看博客:Android 上得逞蓝牙的一些Profile blog.163.com/hancker_31/blog/static/3558736120113271253306/即使一个大广度的问题,这时,作为一种组织方式,“面向对象”不能帮我们些什么吗?

1.[Java]代码

[代码] [Java]代码
view sourceprint?01 一. 当地层  

02    

03 路径:framework/base/core/jni/  

04    

05 比照android_server_BluetoothA2dpService.cpp修建 android_server_bluetoothHidServer.cpp。该类中首要是穿过dbus对bluez协定栈的走访,dbus 的通用方式都在android_bluetooth_common.cpp中完成,我们做的仅仅是经过dbus_func_args_async调用到 bluez供给的input接口,AS3技术与提醒23-28。  

06    

07 重要得逞以下两个方式函数:  

08 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;}  

09 此地要留神将该文件增加到AndroidRuntime.cpp和Android.mk中,不然不会编译到动态库中。  

10    

11 此局部编译后最终身成libandroid_runtime.so并轮换到system/libs下  

12 二.Framework的java部分  

13    

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

15    

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

17 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);}  

18 BluetoothHid.java中重要的两个措施connect和disconnect间接地穿过aidl探访BluetoothHidService。此地主要是得逞跨过程并为上层供给可直接走访的计策。  

19    

20 由此framework的重要局部打包生成framework.Jar并最后策划到system/framework里。  

21 三.使用(Settings.apk)  

22    

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

24    

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

26    

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

28    

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

30 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);                                     }                            }……}  

31 以上为android中完成蓝牙键盘的详细环节。  

32    

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

用户评论