ble/jni_android.c

166 lines
6.4 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <jni.h>
#include <android/log.h>
#include "_cgo_export.h"
void
registerFragment(JNIEnv *env, jobject view) {
2020-09-05 14:12:29 -04:00
jclass cls = (*env)->GetObjectClass(env, view);
jmethodID mid = (*env)->GetMethodID(env, cls, "getContext", "()Landroid/content/Context;");
jobject ctx = (*env)->CallObjectMethod(env, view, mid);
cls = (*env)->GetObjectClass(env, ctx);
mid = (*env)->GetMethodID(env, cls, "getClassLoader", "()Ljava/lang/ClassLoader;");
jobject loader = (*env)->CallObjectMethod(env, ctx, mid);
cls = (*env)->GetObjectClass(env, loader);
mid = (*env)->GetMethodID(env, cls, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring str = (*env)->NewStringUTF(env, "st/wow/git/ble/BlessedConnect");
cls = (*env)->CallObjectMethod(env, loader, mid, str);
mid = (*env)->GetMethodID(env, cls, "<init>", "()V");
jobject inst = (*env)->NewObject(env, cls, mid);
mid = (*env)->GetMethodID(env, cls, "register", "(Landroid/view/View;)V");
(*env)->CallVoidMethod(env, inst, mid, view);
}
jboolean
enabled(JNIEnv *env, jobject b) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "enabled", "()Z");
return (*env)->CallBooleanMethod(env, b, mid);
}
void
scan(JNIEnv *env, jobject b) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "scan", "()V");
(*env)->CallVoidMethod(env, b, mid);
}
void
stopScan(JNIEnv *env, jobject b) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "stopScan", "()V");
(*env)->CallVoidMethod(env, b, mid);
}
void
connect(JNIEnv *env, jobject b, jobject d) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "connect", "(Lcom/welie/blessed/BluetoothPeripheral;)V");
2019-11-27 10:44:37 -05:00
(*env)->CallObjectMethod(env, b, mid, d);
}
void
2019-11-27 10:44:37 -05:00
disconnect(JNIEnv *env, jobject b, jobject g) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "disconnect", "(Lcom/welie/blessed/BluetoothPeripheral;)V");
2019-11-27 10:44:37 -05:00
(*env)->CallVoidMethod(env, b, mid, g);
}
void
discoverServices(JNIEnv *env, jobject b, jobject p) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "discoverServices", "(Lcom/welie/blessed/BluetoothPeripheral;)V");
(*env)->CallVoidMethod(env, b, mid, p);
}
void
2019-11-27 10:44:37 -05:00
discoverCharacteristics(JNIEnv *env, jobject b, jobject g, jobject s) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "discoverCharacteristics", "(Lcom/welie/blessed/BluetoothPeripheral;Landroid/bluetooth/BluetoothGattService;)V");
2019-11-27 10:44:37 -05:00
(*env)->CallVoidMethod(env, b, mid, g, s);
}
jboolean
2019-11-27 10:44:37 -05:00
setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "setCharacteristicNotification", "(Lcom/welie/blessed/BluetoothPeripheral;Landroid/bluetooth/BluetoothGattCharacteristic;)Z");
return (*env)->CallBooleanMethod(env, b, mid, g, c);
}
void
readCharacteristic(JNIEnv *env, jobject b, jobject g, jobject c) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "readCharacteristic", "(Lcom/welie/blessed/BluetoothPeripheral;Landroid/bluetooth/BluetoothGattCharacteristic;)V");
(*env)->CallBooleanMethod(env, b, mid, g, c);
}
jint
GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
return (*vm)->GetEnv(vm, (void **)env, version);
}
jint
AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args) {
return (*vm)->AttachCurrentThread(vm, p_env, thr_args);
}
jint
DetachCurrentThread(JavaVM *vm) {
return (*vm)->DetachCurrentThread(vm);
}
jobject
NewGlobalRef(JNIEnv *env, jobject o) {
return (*env)->NewGlobalRef(env, o);
}
void
Java_st_wow_git_ble_BlessedConnect_onScan(JNIEnv *env, jclass class, jstring jname, jstring jid, jint jrssi, jobject dev) {
2019-11-27 10:44:37 -05:00
const char* name = (*env)->GetStringUTFChars(env, jname, NULL);
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject gdev = (*env)->NewGlobalRef(env, dev);
goOnScan(name, id, (int)jrssi, gdev);
(*env)->ReleaseStringUTFChars(env, jname, name);
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BlessedConnect_onConnect(JNIEnv *env, jclass class, jobject peripheral, jstring jid) {
2019-11-27 10:44:37 -05:00
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject gperipheral = (*env)->NewGlobalRef(env, peripheral);
goOnConnect(gperipheral, id);
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BlessedConnect_onDisconnect(JNIEnv *env, jclass class, jobject peripheral, jstring jid) {
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject gperipheral = (*env)->NewGlobalRef(env, peripheral);
goOnDisconnect(gperipheral, id);
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BlessedConnect_onDiscoverService(JNIEnv *env, jclass class, jstring jid, jstring juuid, jobject serv) {
2019-11-27 10:44:37 -05:00
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
const char* uuid = (*env)->GetStringUTFChars(env, juuid, NULL);
jobject gserv = (*env)->NewGlobalRef(env, serv);
goOnDiscoverService(id, uuid, gserv);
(*env)->ReleaseStringUTFChars(env, jid, id);
(*env)->ReleaseStringUTFChars(env, juuid, uuid);
}
void
Java_st_wow_git_ble_BlessedConnect_onDiscoverCharacteristic(JNIEnv *env, jclass class, jstring jid, jstring jsuuid, jobject serv, jstring jcuuid, jobject chr) {
2019-11-27 10:44:37 -05:00
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
const char* suuid = (*env)->GetStringUTFChars(env, jsuuid, NULL);
const char* cuuid = (*env)->GetStringUTFChars(env, jcuuid, NULL);
jobject gchr = (*env)->NewGlobalRef(env, chr);
goOnDiscoverCharacteristic(id, suuid, serv, cuuid, gchr);
(*env)->ReleaseStringUTFChars(env, jid, id);
(*env)->ReleaseStringUTFChars(env, jsuuid, suuid);
2019-11-27 10:44:37 -05:00
(*env)->ReleaseStringUTFChars(env, jcuuid, cuuid);
}
2019-11-27 10:44:37 -05:00
void
Java_st_wow_git_ble_BlessedConnect_characteristicChanged(JNIEnv *env, jclass class, jstring jid, jstring jcuuid, jobject chr, jbyteArray jvalue, jint len) {
2019-11-27 10:44:37 -05:00
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
const char* cuuid = (*env)->GetStringUTFChars(env, jcuuid, NULL);
jbyte* value = (*env)->GetByteArrayElements(env, jvalue, NULL);
goOnCharacteristicChanged(id, cuuid, chr, value, len);
(*env)->ReleaseStringUTFChars(env, jid, id);
(*env)->ReleaseStringUTFChars(env, jcuuid, cuuid);
(*env)->ReleaseByteArrayElements(env, jvalue, value, len);
}