ble/jni_android.c

133 lines
4.7 KiB
C

#include <stdlib.h>
#include <jni.h>
#include <android/log.h>
#include "_cgo_export.h"
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", "(Landroid/bluetooth/BluetoothDevice;)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", "(Landroid/bluetooth/BluetoothGatt;)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", "(Landroid/bluetooth/BluetoothGatt;)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", "(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattService;)V");
(*env)->CallVoidMethod(env, b, mid, g, s);
}
void
setCharacteristicNotification(JNIEnv *env, jobject b, jobject g, jobject c) {
jclass cls = (*env)->GetObjectClass(env, b);
jmethodID mid = (*env)->GetMethodID(env, cls, "setCharacteristicNotification", "(Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V");
(*env)->CallVoidMethod(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_BleConnect_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_BleConnect_onConnect(JNIEnv *env, jclass class, jobject gatt, jstring jid) {
const char* id = (*env)->GetStringUTFChars(env, jid, NULL);
jobject ggatt = (*env)->NewGlobalRef(env, gatt);
goOnConnect(ggatt, id);
(*env)->ReleaseStringUTFChars(env, jid, id);
}
void
Java_st_wow_git_ble_BleConnect_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_BleConnect_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_BleConnect_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);
}