From 6e0f29b08a040d14576d7053c1206a8439936570 Mon Sep 17 00:00:00 2001
From: yy1717 <fctom1215@outlook.com>
Date: 星期二, 24 十一月 2020 14:18:03 +0800
Subject: [PATCH] 坐标

---
 lib/src/main/cpp/native-lib.cpp |   88 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/lib/src/main/cpp/native-lib.cpp b/lib/src/main/cpp/native-lib.cpp
index 2eb0fc9..0c9d356 100644
--- a/lib/src/main/cpp/native-lib.cpp
+++ b/lib/src/main/cpp/native-lib.cpp
@@ -286,6 +286,72 @@
         }
     }
 }
+
+void SendToBluetooth(const uint8_t *data, int length)
+{
+    JNIEnv *env;
+    bool ready_in_java_env = false;
+
+    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
+        // Attach涓荤嚎绋�
+        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
+            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
+            return;
+        }
+    } else {
+        ready_in_java_env = true;
+    }
+
+    jclass cls = env->GetObjectClass(sg_obj);
+    jmethodID fun = env->GetMethodID(cls, "WriteBluetooth", "([B)V");
+
+    jbyteArray array = env->NewByteArray(length);
+
+    env->SetByteArrayRegion(array, 0, length, (jbyte *) data);
+
+    env->CallVoidMethod(sg_obj, fun, array);
+
+    env->DeleteLocalRef(array);
+    env->DeleteLocalRef(cls);
+
+    if (!ready_in_java_env) {
+        //Detach涓荤嚎绋�
+        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
+            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
+        }
+    }
+}
+
+void ConnectToBluetooth(const char *addr, const char *pin)
+{
+    JNIEnv *env;
+    bool ready_in_java_env = false;
+
+    if (sg_jvm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
+        // Attach涓荤嚎绋�
+        if (sg_jvm->AttachCurrentThread(&env, NULL) != JNI_OK) {
+            LOGE("%s: AttachCurrentThread() failed", __FUNCTION__);
+            return;
+        }
+    } else {
+        ready_in_java_env = true;
+    }
+
+    jclass cls = env->GetObjectClass(sg_obj);
+    jmethodID fun = env->GetMethodID(cls, "ConnectBluetooth", "(Ljava/lang/String;Ljava/lang/String;)V");
+
+    env->CallVoidMethod(sg_obj, fun, env->NewStringUTF(addr), env->NewStringUTF(pin));
+
+    env->DeleteLocalRef(cls);
+
+    if (!ready_in_java_env) {
+        //Detach涓荤嚎绋�
+        if (sg_jvm->DetachCurrentThread() != JNI_OK) {
+            LOGE("%s: DetachCurrentThread() failed", __FUNCTION__);
+        }
+    }
+}
+
 extern "C"
 JNIEXPORT void JNICALL
 Java_com_anyun_exam_lib_RemoteService_startNative(JNIEnv *env, jobject thiz) {
@@ -405,3 +471,25 @@
         env->ReleaseByteArrayElements(rom, c_dat, NULL);
     }
 }
+
+extern "C"
+JNIEXPORT void JNICALL
+Java_com_anyun_exam_lib_RemoteService_BluetoothDataComeIn(JNIEnv *env, jobject thiz,
+                                                          jbyteArray data, jint length) {
+    // TODO: implement BluetoothDataComeIn()
+    jbyte *c_dat = env->GetByteArrayElements(data, NULL);
+
+    PlatformStatusChanged(BLUETOOTH_DATA_EVT, (uint8_t *)c_dat, length);
+
+    env->ReleaseByteArrayElements(data, c_dat, NULL);
+}
+
+extern "C"
+JNIEXPORT void JNICALL
+Java_com_anyun_exam_lib_RemoteService_BluetoothStatusChange(JNIEnv *env, jobject thiz,
+                                                            jint status) {
+    // TODO: implement BluetoothStatusChange()
+    uint8_t  sta = status;
+
+    PlatformStatusChanged(BLUETOOTH_STATUS_EVT, &sta, 1);
+}

--
Gitblit v1.8.0