Loading android/app/jni/com_android_bluetooth_hid.cpp +26 −6 Original line number Original line Diff line number Diff line Loading @@ -16,7 +16,7 @@ #define LOG_TAG "BluetoothHidServiceJni" #define LOG_TAG "BluetoothHidServiceJni" #define LOG_NDEBUG 0 #define LOG_NDEBUG 1 #define CHECK_CALLBACK_ENV \ #define CHECK_CALLBACK_ENV \ if (!checkCallbackThread()) { \ if (!checkCallbackThread()) { \ Loading @@ -36,6 +36,7 @@ namespace android { static jmethodID method_onConnectStateChanged; static jmethodID method_onConnectStateChanged; static jmethodID method_onGetProtocolMode; static jmethodID method_onGetProtocolMode; static jmethodID method_onGetReport; static jmethodID method_onGetReport; static jmethodID method_onHandshake; static jmethodID method_onVirtualUnplug; static jmethodID method_onVirtualUnplug; static const bthh_interface_t *sBluetoothHidInterface = NULL; static const bthh_interface_t *sBluetoothHidInterface = NULL; Loading Loading @@ -131,7 +132,7 @@ static void get_report_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status, u } } static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { ALOGD("call to virtual_unplug_callback"); ALOGV("call to virtual_unplug_callback"); jbyteArray addr; jbyteArray addr; CHECK_CALLBACK_ENV CHECK_CALLBACK_ENV Loading Loading @@ -163,6 +164,23 @@ static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_statu sCallbackEnv->DeleteLocalRef(addr);*/ sCallbackEnv->DeleteLocalRef(addr);*/ } } static void handshake_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { jbyteArray addr; CHECK_CALLBACK_ENV addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t)); if (!addr) { ALOGE("Fail to new jbyteArray bd addr for handshake callback"); checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); return; } sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte *) bd_addr); sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onHandshake, addr, (jint) hh_status); checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); sCallbackEnv->DeleteLocalRef(addr); } static bthh_callbacks_t sBluetoothHidCallbacks = { static bthh_callbacks_t sBluetoothHidCallbacks = { sizeof(sBluetoothHidCallbacks), sizeof(sBluetoothHidCallbacks), Loading @@ -171,7 +189,8 @@ static bthh_callbacks_t sBluetoothHidCallbacks = { get_protocol_mode_callback, get_protocol_mode_callback, NULL, NULL, get_report_callback, get_report_callback, virtual_unplug_callback virtual_unplug_callback, handshake_callback }; }; // Define native functions // Define native functions Loading @@ -184,6 +203,7 @@ static void classInitNative(JNIEnv* env, jclass clazz) { method_onConnectStateChanged = env->GetMethodID(clazz, "onConnectStateChanged", "([BI)V"); method_onConnectStateChanged = env->GetMethodID(clazz, "onConnectStateChanged", "([BI)V"); method_onGetProtocolMode = env->GetMethodID(clazz, "onGetProtocolMode", "([BI)V"); method_onGetProtocolMode = env->GetMethodID(clazz, "onGetProtocolMode", "([BI)V"); method_onGetReport = env->GetMethodID(clazz, "onGetReport", "([B[BI)V"); method_onGetReport = env->GetMethodID(clazz, "onGetReport", "([B[BI)V"); method_onHandshake = env->GetMethodID(clazz, "onHandshake", "([BI)V"); method_onVirtualUnplug = env->GetMethodID(clazz, "onVirtualUnplug", "([BI)V"); method_onVirtualUnplug = env->GetMethodID(clazz, "onVirtualUnplug", "([BI)V"); /* /* Loading Loading @@ -399,7 +419,7 @@ static jboolean setProtocolModeNative(JNIEnv *env, jobject object, jbyteArray ad } } static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jbyte reportId, jint bufferSize) { static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jbyte reportId, jint bufferSize) { ALOGD("%s: reportType = %d, reportId = %d, bufferSize = %d", __FUNCTION__, reportType, reportId, bufferSize); ALOGV("%s: reportType = %d, reportId = %d, bufferSize = %d", __FUNCTION__, reportType, reportId, bufferSize); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; Loading Loading @@ -427,7 +447,7 @@ static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jstring report) { static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jstring report) { ALOGD("%s: reportType = %d", __FUNCTION__, reportType); ALOGV("%s: reportType = %d", __FUNCTION__, reportType); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; jboolean ret = JNI_TRUE; jboolean ret = JNI_TRUE; Loading @@ -453,7 +473,7 @@ static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, } } static jboolean sendDataNative(JNIEnv *env, jobject object, jbyteArray address, jstring report) { static jboolean sendDataNative(JNIEnv *env, jobject object, jbyteArray address, jstring report) { ALOGD("%s", __FUNCTION__); ALOGV("%s", __FUNCTION__); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; jboolean ret = JNI_TRUE; jboolean ret = JNI_TRUE; Loading android/app/src/com/android/bluetooth/hid/HidService.java +23 −0 Original line number Original line Diff line number Diff line Loading @@ -67,6 +67,7 @@ public class HidService extends ProfileService { private static final int MESSAGE_SET_REPORT = 10; private static final int MESSAGE_SET_REPORT = 10; private static final int MESSAGE_SEND_DATA = 11; private static final int MESSAGE_SEND_DATA = 11; private static final int MESSAGE_ON_VIRTUAL_UNPLUG = 12; private static final int MESSAGE_ON_VIRTUAL_UNPLUG = 12; private static final int MESSAGE_ON_HANDSHAKE = 13; static { static { classInitNative(); classInitNative(); Loading Loading @@ -249,6 +250,13 @@ public class HidService extends ProfileService { broadcastReport(device, report, bufferSize); broadcastReport(device, report, bufferSize); } } break; break; case MESSAGE_ON_HANDSHAKE: { BluetoothDevice device = getDevice((byte[])msg.obj); int status = msg.arg1; broadcastHandshake(device, status); } break; case MESSAGE_SET_REPORT: case MESSAGE_SET_REPORT: { { BluetoothDevice device = (BluetoothDevice) msg.obj; BluetoothDevice device = (BluetoothDevice) msg.obj; Loading Loading @@ -564,6 +572,13 @@ public class HidService extends ProfileService { mHandler.sendMessage(msg); mHandler.sendMessage(msg); } } private void onHandshake(byte[] address, int status) { Message msg = mHandler.obtainMessage(MESSAGE_ON_HANDSHAKE); msg.obj = address; msg.arg1 = status; mHandler.sendMessage(msg); } private void onVirtualUnplug(byte[] address, int status) { private void onVirtualUnplug(byte[] address, int status) { Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG); Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG); msg.obj = address; msg.obj = address; Loading Loading @@ -603,6 +618,14 @@ public class HidService extends ProfileService { sendBroadcast(intent, BLUETOOTH_PERM); sendBroadcast(intent, BLUETOOTH_PERM); } } private void broadcastHandshake(BluetoothDevice device, int status) { Intent intent = new Intent(BluetoothInputDevice.ACTION_HANDSHAKE); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.putExtra(BluetoothInputDevice.EXTRA_STATUS, status); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); sendBroadcast(intent, BLUETOOTH_PERM); } private void broadcastProtocolMode(BluetoothDevice device, int protocolMode) { private void broadcastProtocolMode(BluetoothDevice device, int protocolMode) { Intent intent = new Intent(BluetoothInputDevice.ACTION_PROTOCOL_MODE_CHANGED); Intent intent = new Intent(BluetoothInputDevice.ACTION_PROTOCOL_MODE_CHANGED); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); Loading Loading
android/app/jni/com_android_bluetooth_hid.cpp +26 −6 Original line number Original line Diff line number Diff line Loading @@ -16,7 +16,7 @@ #define LOG_TAG "BluetoothHidServiceJni" #define LOG_TAG "BluetoothHidServiceJni" #define LOG_NDEBUG 0 #define LOG_NDEBUG 1 #define CHECK_CALLBACK_ENV \ #define CHECK_CALLBACK_ENV \ if (!checkCallbackThread()) { \ if (!checkCallbackThread()) { \ Loading @@ -36,6 +36,7 @@ namespace android { static jmethodID method_onConnectStateChanged; static jmethodID method_onConnectStateChanged; static jmethodID method_onGetProtocolMode; static jmethodID method_onGetProtocolMode; static jmethodID method_onGetReport; static jmethodID method_onGetReport; static jmethodID method_onHandshake; static jmethodID method_onVirtualUnplug; static jmethodID method_onVirtualUnplug; static const bthh_interface_t *sBluetoothHidInterface = NULL; static const bthh_interface_t *sBluetoothHidInterface = NULL; Loading Loading @@ -131,7 +132,7 @@ static void get_report_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status, u } } static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { ALOGD("call to virtual_unplug_callback"); ALOGV("call to virtual_unplug_callback"); jbyteArray addr; jbyteArray addr; CHECK_CALLBACK_ENV CHECK_CALLBACK_ENV Loading Loading @@ -163,6 +164,23 @@ static void virtual_unplug_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_statu sCallbackEnv->DeleteLocalRef(addr);*/ sCallbackEnv->DeleteLocalRef(addr);*/ } } static void handshake_callback(bt_bdaddr_t *bd_addr, bthh_status_t hh_status) { jbyteArray addr; CHECK_CALLBACK_ENV addr = sCallbackEnv->NewByteArray(sizeof(bt_bdaddr_t)); if (!addr) { ALOGE("Fail to new jbyteArray bd addr for handshake callback"); checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); return; } sCallbackEnv->SetByteArrayRegion(addr, 0, sizeof(bt_bdaddr_t), (jbyte *) bd_addr); sCallbackEnv->CallVoidMethod(mCallbacksObj, method_onHandshake, addr, (jint) hh_status); checkAndClearExceptionFromCallback(sCallbackEnv, __FUNCTION__); sCallbackEnv->DeleteLocalRef(addr); } static bthh_callbacks_t sBluetoothHidCallbacks = { static bthh_callbacks_t sBluetoothHidCallbacks = { sizeof(sBluetoothHidCallbacks), sizeof(sBluetoothHidCallbacks), Loading @@ -171,7 +189,8 @@ static bthh_callbacks_t sBluetoothHidCallbacks = { get_protocol_mode_callback, get_protocol_mode_callback, NULL, NULL, get_report_callback, get_report_callback, virtual_unplug_callback virtual_unplug_callback, handshake_callback }; }; // Define native functions // Define native functions Loading @@ -184,6 +203,7 @@ static void classInitNative(JNIEnv* env, jclass clazz) { method_onConnectStateChanged = env->GetMethodID(clazz, "onConnectStateChanged", "([BI)V"); method_onConnectStateChanged = env->GetMethodID(clazz, "onConnectStateChanged", "([BI)V"); method_onGetProtocolMode = env->GetMethodID(clazz, "onGetProtocolMode", "([BI)V"); method_onGetProtocolMode = env->GetMethodID(clazz, "onGetProtocolMode", "([BI)V"); method_onGetReport = env->GetMethodID(clazz, "onGetReport", "([B[BI)V"); method_onGetReport = env->GetMethodID(clazz, "onGetReport", "([B[BI)V"); method_onHandshake = env->GetMethodID(clazz, "onHandshake", "([BI)V"); method_onVirtualUnplug = env->GetMethodID(clazz, "onVirtualUnplug", "([BI)V"); method_onVirtualUnplug = env->GetMethodID(clazz, "onVirtualUnplug", "([BI)V"); /* /* Loading Loading @@ -399,7 +419,7 @@ static jboolean setProtocolModeNative(JNIEnv *env, jobject object, jbyteArray ad } } static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jbyte reportId, jint bufferSize) { static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jbyte reportId, jint bufferSize) { ALOGD("%s: reportType = %d, reportId = %d, bufferSize = %d", __FUNCTION__, reportType, reportId, bufferSize); ALOGV("%s: reportType = %d, reportId = %d, bufferSize = %d", __FUNCTION__, reportType, reportId, bufferSize); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; Loading Loading @@ -427,7 +447,7 @@ static jboolean getReportNative(JNIEnv *env, jobject object, jbyteArray address, static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jstring report) { static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, jbyte reportType, jstring report) { ALOGD("%s: reportType = %d", __FUNCTION__, reportType); ALOGV("%s: reportType = %d", __FUNCTION__, reportType); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; jboolean ret = JNI_TRUE; jboolean ret = JNI_TRUE; Loading @@ -453,7 +473,7 @@ static jboolean setReportNative(JNIEnv *env, jobject object, jbyteArray address, } } static jboolean sendDataNative(JNIEnv *env, jobject object, jbyteArray address, jstring report) { static jboolean sendDataNative(JNIEnv *env, jobject object, jbyteArray address, jstring report) { ALOGD("%s", __FUNCTION__); ALOGV("%s", __FUNCTION__); bt_status_t status; bt_status_t status; jbyte *addr; jbyte *addr; jboolean ret = JNI_TRUE; jboolean ret = JNI_TRUE; Loading
android/app/src/com/android/bluetooth/hid/HidService.java +23 −0 Original line number Original line Diff line number Diff line Loading @@ -67,6 +67,7 @@ public class HidService extends ProfileService { private static final int MESSAGE_SET_REPORT = 10; private static final int MESSAGE_SET_REPORT = 10; private static final int MESSAGE_SEND_DATA = 11; private static final int MESSAGE_SEND_DATA = 11; private static final int MESSAGE_ON_VIRTUAL_UNPLUG = 12; private static final int MESSAGE_ON_VIRTUAL_UNPLUG = 12; private static final int MESSAGE_ON_HANDSHAKE = 13; static { static { classInitNative(); classInitNative(); Loading Loading @@ -249,6 +250,13 @@ public class HidService extends ProfileService { broadcastReport(device, report, bufferSize); broadcastReport(device, report, bufferSize); } } break; break; case MESSAGE_ON_HANDSHAKE: { BluetoothDevice device = getDevice((byte[])msg.obj); int status = msg.arg1; broadcastHandshake(device, status); } break; case MESSAGE_SET_REPORT: case MESSAGE_SET_REPORT: { { BluetoothDevice device = (BluetoothDevice) msg.obj; BluetoothDevice device = (BluetoothDevice) msg.obj; Loading Loading @@ -564,6 +572,13 @@ public class HidService extends ProfileService { mHandler.sendMessage(msg); mHandler.sendMessage(msg); } } private void onHandshake(byte[] address, int status) { Message msg = mHandler.obtainMessage(MESSAGE_ON_HANDSHAKE); msg.obj = address; msg.arg1 = status; mHandler.sendMessage(msg); } private void onVirtualUnplug(byte[] address, int status) { private void onVirtualUnplug(byte[] address, int status) { Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG); Message msg = mHandler.obtainMessage(MESSAGE_ON_VIRTUAL_UNPLUG); msg.obj = address; msg.obj = address; Loading Loading @@ -603,6 +618,14 @@ public class HidService extends ProfileService { sendBroadcast(intent, BLUETOOTH_PERM); sendBroadcast(intent, BLUETOOTH_PERM); } } private void broadcastHandshake(BluetoothDevice device, int status) { Intent intent = new Intent(BluetoothInputDevice.ACTION_HANDSHAKE); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.putExtra(BluetoothInputDevice.EXTRA_STATUS, status); intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); sendBroadcast(intent, BLUETOOTH_PERM); } private void broadcastProtocolMode(BluetoothDevice device, int protocolMode) { private void broadcastProtocolMode(BluetoothDevice device, int protocolMode) { Intent intent = new Intent(BluetoothInputDevice.ACTION_PROTOCOL_MODE_CHANGED); Intent intent = new Intent(BluetoothInputDevice.ACTION_PROTOCOL_MODE_CHANGED); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device); Loading