安卓hid哪裡打開
❶ 查看手機是否支持hid
查看手機是否支持hid步驟如下:
1、首先Hid是HumanInterfaceDevice的縮寫,由其名稱可以了解HID設備是直接與人交互的設備,例如鍵盤、滑鼠與游戲手柄等。
2、我們知道在手機設置--藍牙功能界面可以手動搜索藍牙HID設備並進行連接,這篇博客就是介紹如何在android代碼中實現HID設備的連接。最後會給出完整的代碼工程。一個前提條件是android4.0以上才支持HID設備。
3、找遍所有公開的api中是沒用方法可以直接連接HID設備的,既然手機設置-藍牙界面可以連接HID設備,說明系統是可以做到的,那是不是把這個方法隱藏了。
❷ android bluetooth hid協議的開發求助
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介面。
主要實現以下兩個方法函數:
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文件。
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中先彈出一個隨機數,然後在鍵盤中敲入相同的隨機數即配對成功,具體實現如下:
// 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);
}
}
……
}
轉載