Bluetooth Low Energy for Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
6.4 KiB

#include <stdlib.h>
#include <jni.h>
#include <android/log.h>
#include "_cgo_export.h"
void
registerFragment(JNIEnv *env, jobject view) {
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");
(*env)->CallObjectMethod(env, b, mid, d);
}
void
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");
(*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
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");
(*env)->CallVoidMethod(env, b, mid, g, s);
}
jboolean
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) {
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) {
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) {
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) {
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);
(*env)->ReleaseStringUTFChars(env, jcuuid, cuuid);
}
void
Java_st_wow_git_ble_BlessedConnect_characteristicChanged(JNIEnv *env, jclass class, jstring jid, jstring jcuuid, jobject chr, jbyteArray jvalue, jint len) {
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);
}