diff options
author | Andrei Vasiliu <[email protected]> | 2015-12-18 20:09:25 +0200 |
---|---|---|
committer | Andrei Vasiliu <[email protected]> | 2016-01-12 00:03:19 +0200 |
commit | a97afed4dfbdac2e364f6bd50ad6a86dfe5417c7 (patch) | |
tree | 74e53dbd0dd625739ddcf27305a85ac541ee3714 | |
parent | e6006161b8a4a2c1221f9fe1fa08569f087a4221 (diff) |
Java: Added Java API
Signed-off-by: Andrei Vasiliu <[email protected]>
36 files changed, 2526 insertions, 17 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 66dcdb67..e8291200 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.1) +cmake_minimum_required (VERSION 3.1.0 FATAL_ERROR) project (tinyb) FIND_PACKAGE (Threads REQUIRED) @@ -56,9 +56,9 @@ IF(WARNINGS) IF(UNIX) IF(CMAKE_COMPILER_IS_GNUCC) ADD_DEFINITIONS(-Wall -Wextra) - ENDIF(CMAKE_COMPILER_IS_GNUCC) - ENDIF(UNIX) - ENDIF(WARNINGS) + ENDIF(CMAKE_COMPILER_IS_GNUCC) + ENDIF(UNIX) +ENDIF(WARNINGS) IF(DEBUG) IF(UNIX) @@ -71,8 +71,7 @@ ENDIF(DEBUG) find_path (SYSTEM_USR_DIR "stdlib.h") include_directories (${SYSTEM_USR_DIR}) -option (BUILDSWIG "Build swig modules." ON) -option (BUILDSWIGJAVA "Build Java API." OFF) +option (BUILDJAVA "Build Java API." OFF) add_subdirectory (src) add_subdirectory (examples) diff --git a/java/BluetoothAdapter.java b/java/BluetoothAdapter.java new file mode 100644 index 00000000..db2045fd --- /dev/null +++ b/java/BluetoothAdapter.java @@ -0,0 +1,43 @@ +import java.util.*; + +public class BluetoothAdapter extends BluetoothObject +{ + private long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + public native boolean startDiscovery(); + public native boolean stopDiscovery(); + public native List<BluetoothDevice> getDevices(); + public native String getAddress(); + public native String getName(); + public native String getAlias(); + public native void setAlias(String value); + public native long getBluetoothClass(); + public native boolean getPowered(); + public native void setPowered(boolean value); + public native boolean getDiscoverable(); + public native void setDiscoverable(boolean value); + public native long getDiscoverableTimeout(); + public native void setDiscoverableTimout(long value); + public native boolean getPairable(); + public native void setPairable(boolean value); + public native long getPairableTimeout(); + public native void setPairableTimeout(long value); + public native boolean getDiscovering(); + public native String[] getUuids(); + public native String getModalias(); + + private native void delete(); + + private BluetoothAdapter(long instance) + { + super(instance); + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothCallback.java b/java/BluetoothCallback.java new file mode 100644 index 00000000..a4da9c74 --- /dev/null +++ b/java/BluetoothCallback.java @@ -0,0 +1,9 @@ +public abstract class BluetoothCallback implements Runnable +{ + protected BluetoothObject bObj; + + /* + * public void run() is missing because it will be implemented + * in children classes + */ +} diff --git a/java/BluetoothDevice.java b/java/BluetoothDevice.java new file mode 100644 index 00000000..06f49222 --- /dev/null +++ b/java/BluetoothDevice.java @@ -0,0 +1,50 @@ +import java.util.*; + +public class BluetoothDevice extends BluetoothObject +{ + private long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothDevice clone(); + public native boolean disconnect(); + public native boolean connect(); + public native boolean connectProfile(String arg_UUID); + public native boolean disconnectProfile(String arg_UUID); + public native boolean pair(); + public native boolean cancelPairing(); + public native List<BluetoothGattService> getServices(); + public native String getAddress(); + public native String getName(); + public native String getAlias(); + public native void setAlias(String value); + public native int getBluetoothClass(); + public native short getAppearance(); + public native String getIcon(); + public native boolean getPaired(); + public native boolean getTrusted(); + public native void setTrusted(boolean value); + public native boolean getBlocked(); + public native void setBlocked(boolean value); + public native boolean getLegacyPairing(); + public native short getRssi(); + public native boolean getConnected(); + public native String[] getUuids(); + public native String getModalias(); + public native BluetoothAdapter getAdapter(); + + private native void delete(); + /*public BluetoothDevice() + { + System.out.println("mda"); + }*/ + private BluetoothDevice(long instance) + { + super(instance); + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothEvent.java b/java/BluetoothEvent.java new file mode 100644 index 00000000..611a51dd --- /dev/null +++ b/java/BluetoothEvent.java @@ -0,0 +1,27 @@ +import java.util.*; + +public class BluetoothEvent +{ + private long nativeInstance; + + public native BluetoothType getType(); + public native String getName(); + public native String getIdentifier(); + public native boolean executeCallback(); + public native boolean hasCallback(); + + private native void init(BluetoothType type, String name, String identifier, + BluetoothObject parent, BluetoothCallback cb, Object data); + private native void delete(); + + public BluetoothEvent(BluetoothType type, String name, String identifier, + BluetoothObject parent, BluetoothCallback cb, Object data) + { + init(type, name, identifier, parent, cb, data); + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothGattCharacteristic.java b/java/BluetoothGattCharacteristic.java new file mode 100644 index 00000000..41f814b9 --- /dev/null +++ b/java/BluetoothGattCharacteristic.java @@ -0,0 +1,33 @@ +import java.util.*; + +public class BluetoothGattCharacteristic extends BluetoothObject +{ + private long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothGattCharacteristic clone(); + public native List<Byte> readValue(); + public native boolean writeValue(List<Byte> argValue); + public native boolean startNotify(); + public native boolean stopNotify(); + public native String getUuid(); + public native BluetoothGattService getService(); + public native List<Byte> getValue(); + public native boolean getNotifying(); + public native String[] getFlags(); + public native List<BluetoothGattDescriptor> getDescriptors(); + + private native void init(BluetoothGattCharacteristic obj); + private native void delete(); + + private BluetoothGattCharacteristic(long instance) + { + super(instance); + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothGattDescriptor.java b/java/BluetoothGattDescriptor.java new file mode 100644 index 00000000..c378c2fa --- /dev/null +++ b/java/BluetoothGattDescriptor.java @@ -0,0 +1,28 @@ +import java.util.*; + +public class BluetoothGattDescriptor extends BluetoothObject +{ + public long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + + public native List<Byte> readValue(); + public native boolean writeValue(List<Byte> argValue); + public native String getUuid(); + public native BluetoothGattCharacteristic getCharacteristic(); + public native List<Byte> getValue(); + + private native void delete(); + + private BluetoothGattDescriptor(long instance) + { + super(instance); + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothGattService.java b/java/BluetoothGattService.java new file mode 100644 index 00000000..4e51f2fd --- /dev/null +++ b/java/BluetoothGattService.java @@ -0,0 +1,26 @@ +import java.util.*; + +public class BluetoothGattService extends BluetoothObject +{ + private long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothAdapter clone(); + public native String getUuid(); + public native BluetoothDevice getDevice(); + public native boolean getPrimary(); + public native List<BluetoothGattCharacteristic> getCharacteristics(); + + private native void delete(); + + private BluetoothGattService(long instance) + { + super(instance); + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothManager.java b/java/BluetoothManager.java new file mode 100644 index 00000000..00d64541 --- /dev/null +++ b/java/BluetoothManager.java @@ -0,0 +1,40 @@ +import java.util.*; + +public class BluetoothManager +{ + private long nativeInstance; + private static BluetoothManager inst; + + public native BluetoothType getBluetoothType(); + public native BluetoothObject getObject(BluetoothType type, String name, + String identifier, BluetoothObject parent); + public native List<BluetoothObject> getObjects(BluetoothType type, String name, + String identifier, BluetoothObject parent); + public native List<BluetoothAdapter> getAdapters(); + public native List<BluetoothDevice> getDevices(); + public native List<BluetoothGattService> getServices(); + public native boolean setDefaultAdapter(BluetoothAdapter adapter); + public native boolean startDiscovery(); + public native boolean stopDiscovery(); + + private native void init(); + private native void delete(); + private BluetoothManager() + { + init(); + } + public static synchronized BluetoothManager getBluetoothManager() + { + if (inst == null) + { + inst = new BluetoothManager(); + inst.init(); + } + return inst; + } + + protected void finalize() + { + delete(); + } +} diff --git a/java/BluetoothObject.java b/java/BluetoothObject.java new file mode 100644 index 00000000..2965b5a1 --- /dev/null +++ b/java/BluetoothObject.java @@ -0,0 +1,31 @@ +import java.util.*; + +public class BluetoothObject +{ + long nativeInstance; + + public native BluetoothType getBluetoothType(); + public native BluetoothObject clone(); + + private native void delete(); + private native boolean operatorEqual(BluetoothObject obj); + + protected BluetoothObject(long instance) + { + nativeInstance = instance; + } + + protected void finalize() + { + delete(); + } + + public boolean equals(BluetoothObject obj) + { + if (obj == null) + { + return false; + } + return operatorEqual(obj); + } +} diff --git a/java/BluetoothType.java b/java/BluetoothType.java new file mode 100644 index 00000000..d533a837 --- /dev/null +++ b/java/BluetoothType.java @@ -0,0 +1,8 @@ +public enum BluetoothType +{ + NONE, ADAPTER, DEVICE, + GATT_SERVICE, GATT_CHARACTERISTIC, + GATT_DESCRIPTOR; + + private long nativeInstance; +} diff --git a/java/ObjectArgCallback.java b/java/ObjectArgCallback.java new file mode 100644 index 00000000..e8468144 --- /dev/null +++ b/java/ObjectArgCallback.java @@ -0,0 +1,14 @@ +public class ObjectArgCallback extends BluetoothCallback +{ + private Object callbackArg; + + public ObjectArgCallback(BluetoothObject bObj, Object callbackArg) + { + this.bObj = bObj; + this.callbackArg = callbackArg; + } + + public void run() + { + } +} diff --git a/java/ObjectArrayArgCallback.java b/java/ObjectArrayArgCallback.java new file mode 100644 index 00000000..9a5c5e64 --- /dev/null +++ b/java/ObjectArrayArgCallback.java @@ -0,0 +1,14 @@ +public class ObjectArrayArgCallback extends BluetoothCallback +{ + private Object[] callbackArg; + + public ObjectArrayArgCallback(BluetoothObject bObj, Object[] callbackArg) + { + this.bObj = bObj; + this.callbackArg = callbackArg; + } + + public void run() + { + } +} diff --git a/java/jni/BluetoothAdapter.cxx b/java/jni/BluetoothAdapter.cxx new file mode 100644 index 00000000..56b568e2 --- /dev/null +++ b/java/jni/BluetoothAdapter.cxx @@ -0,0 +1,212 @@ +#include "tinyb/BluetoothAdapter.hpp" +#include "tinyb/BluetoothDevice.hpp" +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothAdapter.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothAdapter_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "ADAPTER"); +} + +jobject Java_BluetoothAdapter_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothAdapter>(env, obj, "BluetoothAdapter"); +} + +jboolean Java_BluetoothAdapter_startDiscovery(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->start_discovery() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothAdapter_stopDiscovery(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->stop_discovery() ? JNI_TRUE : JNI_FALSE; +} + +jobject Java_BluetoothAdapter_getDevices(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::vector<std::unique_ptr<BluetoothDevice>> array = obj_adapter->get_devices(); + jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, + "BluetoothDevice", + "(J)V"); + + return result; +} + +jstring Java_BluetoothAdapter_getAddress(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string address = obj_adapter->get_address(); + + return env->NewStringUTF((const char *)address.c_str()); +} + +jstring Java_BluetoothAdapter_getName(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string name = obj_adapter->get_name(); + + return env->NewStringUTF((const char *)name.c_str()); +} + +jstring Java_BluetoothAdapter_getAlias(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string alias = obj_adapter->get_alias(); + + return env->NewStringUTF((const char *)alias.c_str()); +} + +void Java_BluetoothAdapter_setAlias(JNIEnv *env, jobject obj, jstring str) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + jboolean is_copy = JNI_TRUE; + const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); + const std::string string_to_write = std::string(str_chars); + + env->ReleaseStringUTFChars(str, str_chars); + + obj_adapter->set_alias(string_to_write); +} + +jlong Java_BluetoothAdapter_getBluetoothClass(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_class(); +} + +jboolean Java_BluetoothAdapter_getPowered(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_powered() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothAdapter_setPowered(JNIEnv *env, jobject obj, jboolean val) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_powered(val_to_write); +} + +jboolean Java_BluetoothAdapter_getDiscoverable(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_discoverable() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothAdapter_setDiscoverable(JNIEnv *env, jobject obj, jboolean val) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_discoverable(val_to_write); +} + +jlong Java_BluetoothAdapter_getDiscoverableTimeout(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_discoverable_timeout(); +} + +void Java_BluetoothAdapter_setDiscoverableTimout(JNIEnv *env, jobject obj, jlong timeout) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + if (timeout < 0) + { + throw std::invalid_argument("timeout argument is negative\n"); + } + obj_adapter->set_discoverable_timeout((unsigned int)timeout); +} + +jboolean Java_BluetoothAdapter_getPairable(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_pairable() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothAdapter_setPairable(JNIEnv *env, jobject obj, jboolean val) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_adapter->set_pairable(val_to_write); +} + +jlong Java_BluetoothAdapter_getPairableTimeout(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return (jlong)obj_adapter->get_pairable_timeout(); +} + +void Java_BluetoothAdapter_setPairableTimeout(JNIEnv *env, jobject obj, jlong timeout) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + if (timeout < 0) + { + throw std::invalid_argument("timeout argument is negative\n"); + } + obj_adapter->set_pairable_timeout((unsigned int)timeout); +} + +jboolean Java_BluetoothAdapter_getDiscovering(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + + return obj_adapter->get_discovering() ? JNI_TRUE : JNI_FALSE; +} + +jobjectArray Java_BluetoothAdapter_getUuids(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::vector<std::string> uuids = obj_adapter->get_uuids(); + unsigned int uuids_size = uuids.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); + + for (unsigned int i = 0; i < uuids_size; ++i) + { + std::string str_elem = uuids.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; +} + +jstring Java_BluetoothAdapter_getModalias(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *obj_adapter = getInstance<BluetoothAdapter>(env, obj); + std::string modalias = obj_adapter->get_modalias(); + + return env->NewStringUTF((const char *)modalias.c_str()); +} + +void Java_BluetoothAdapter_delete(JNIEnv *env, jobject obj) +{ + BluetoothAdapter *adapter = getInstance<BluetoothAdapter>(env, obj); + delete adapter; +} + diff --git a/java/jni/BluetoothAdapter.h b/java/jni/BluetoothAdapter.h new file mode 100644 index 00000000..688979aa --- /dev/null +++ b/java/jni/BluetoothAdapter.h @@ -0,0 +1,205 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothAdapter */ + +#ifndef _Included_BluetoothAdapter +#define _Included_BluetoothAdapter +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothAdapter + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothAdapter_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: clone + * Signature: ()LBluetoothAdapter; + */ +JNIEXPORT jobject JNICALL Java_BluetoothAdapter_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: startDiscovery + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_startDiscovery + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: stopDiscovery + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_stopDiscovery + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getDevices + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothAdapter_getDevices + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getAddress + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothAdapter_getAddress + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getName + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothAdapter_getName + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getAlias + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothAdapter_getAlias + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setAlias + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setAlias + (JNIEnv *, jobject, jstring); + +/* + * Class: BluetoothAdapter + * Method: getBluetoothClass + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_BluetoothAdapter_getBluetoothClass + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getPowered + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_getPowered + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setPowered + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setPowered + (JNIEnv *, jobject, jboolean); + +/* + * Class: BluetoothAdapter + * Method: getDiscoverable + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_getDiscoverable + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setDiscoverable + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setDiscoverable + (JNIEnv *, jobject, jboolean); + +/* + * Class: BluetoothAdapter + * Method: getDiscoverableTimeout + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_BluetoothAdapter_getDiscoverableTimeout + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setDiscoverableTimout + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setDiscoverableTimout + (JNIEnv *, jobject, jlong); + +/* + * Class: BluetoothAdapter + * Method: getPairable + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_getPairable + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setPairable + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setPairable + (JNIEnv *, jobject, jboolean); + +/* + * Class: BluetoothAdapter + * Method: getPairableTimeout + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_BluetoothAdapter_getPairableTimeout + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: setPairableTimeout + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_setPairableTimeout + (JNIEnv *, jobject, jlong); + +/* + * Class: BluetoothAdapter + * Method: getDiscovering + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothAdapter_getDiscovering + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getUuids + * Signature: ()[Ljava/lang/String; + */ +JNIEXPORT jobjectArray JNICALL Java_BluetoothAdapter_getUuids + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: getModalias + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothAdapter_getModalias + (JNIEnv *, jobject); + +/* + * Class: BluetoothAdapter + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothAdapter_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothCallback.h b/java/jni/BluetoothCallback.h new file mode 100644 index 00000000..fe28bd5d --- /dev/null +++ b/java/jni/BluetoothCallback.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothCallback */ + +#ifndef _Included_BluetoothCallback +#define _Included_BluetoothCallback +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothDevice.cxx b/java/jni/BluetoothDevice.cxx new file mode 100644 index 00000000..0dcf9327 --- /dev/null +++ b/java/jni/BluetoothDevice.cxx @@ -0,0 +1,255 @@ +#include "tinyb/BluetoothAdapter.hpp" +#include "tinyb/BluetoothDevice.hpp" +#include "tinyb/BluetoothGattService.hpp" +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothDevice.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothAdapter_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "DEVICE"); +} + +jobject Java_BluetoothDevice_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothDevice>(env, obj, "BluetoothDevice"); +} + +jboolean Java_BluetoothDevice_disconnect(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->disconnect() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothDevice_connect(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->connect() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothDevice_connectProfile(JNIEnv *env, jobject obj, jstring str) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + jboolean is_copy = JNI_TRUE; + const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); + const std::string string_to_write = std::string(str_chars); + + env->ReleaseStringUTFChars(str, str_chars); + + return obj_device->connect_profile(string_to_write); +} + +jboolean Java_BluetoothDevice_disconnectProfile(JNIEnv *env, jobject obj, jstring str) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + jboolean is_copy = JNI_TRUE; + const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); + const std::string string_to_write = std::string(str_chars); + + env->ReleaseStringUTFChars(str, str_chars); + + return obj_device->disconnect_profile(string_to_write); +} + +jboolean Java_BluetoothDevice_pair(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->pair() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothDevice_cancelPairing(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->cancel_pairing() ? JNI_TRUE : JNI_FALSE; +} + +jobject Java_BluetoothDevice_getServices(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::vector<std::unique_ptr<BluetoothGattService>> array = obj_device->get_services(); + jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, + "BluetoothGattService", + "(J)V"); + + return result; +} + +jstring Java_BluetoothDevice_getAddress(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string address = obj_device->get_address(); + + return env->NewStringUTF((const char *)address.c_str()); +} + +jstring Java_BluetoothDevice_getName(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string name = obj_device->get_name(); + + return env->NewStringUTF((const char *)name.c_str()); +} + +jstring Java_BluetoothDevice_getAlias(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string alias = obj_device->get_alias(); + + return env->NewStringUTF((const char *)alias.c_str()); +} + +void Java_BluetoothDevice_setAlias(JNIEnv *env, jobject obj, jstring str) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + jboolean is_copy = JNI_TRUE; + const char *str_chars = (char *)env->GetStringUTFChars(str, &is_copy); + const std::string string_to_write = std::string(str_chars); + + env->ReleaseStringUTFChars(str, str_chars); + + obj_device->set_alias(string_to_write); +} + +jint Java_BluetoothDevice_getBluetoothClass(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jlong)obj_device->get_class(); +} + +jshort Java_BluetoothDevice_getAppearance(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jshort)obj_device->get_appearance(); +} + +jstring Java_BluetoothDevice_getIcon(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string icon = obj_device->get_icon(); + + return env->NewStringUTF((const char *)icon.c_str()); +} + +jboolean Java_BluetoothDevice_getPaired(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_paired() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothDevice_getTrusted(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_trusted() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothDevice_setTrusted(JNIEnv *env, jobject obj, jboolean val) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_device->set_trusted(val_to_write); +} + +jboolean Java_BluetoothDevice_getBlocked(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_blocked() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothDevice_setBlocked(JNIEnv *env, jobject obj, jboolean val) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + bool val_to_write = from_jboolean_to_bool(val); + obj_device->set_blocked(val_to_write); +} + +jboolean Java_BluetoothDevice_getLegacyPairing(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_legacy_pairing() ? JNI_TRUE : JNI_FALSE; +} + +jshort Java_BluetoothDevice_getRssi(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return (jshort)obj_device->get_rssi(); +} + +jboolean Java_BluetoothDevice_getConnected(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + + return obj_device->get_connected() ? JNI_TRUE : JNI_FALSE; +} + +jobjectArray Java_BluetoothDevice_getUuids(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::vector<std::string> uuids = obj_device->get_uuids(); + unsigned int uuids_size = uuids.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(uuids_size, string_class, 0); + + for (unsigned int i = 0; i < uuids_size; ++i) + { + std::string str_elem = uuids.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; +} + +jstring Java_BluetoothDevice_getModalias(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + std::string modalias = obj_device->get_modalias(); + + return env->NewStringUTF((const char *)modalias.c_str()); +} + +jobject Java_BluetoothDevice_getAdapter(JNIEnv *env, jobject obj) +{ + BluetoothDevice *obj_device = getInstance<BluetoothDevice>(env, obj); + BluetoothAdapter *obj_adapter = obj_device->get_adapter().clone(); + + jclass b_adapter_class = search_class(env, "BluetoothAdapter"); + jmethodID b_adapter_ctor = search_method(env, b_adapter_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_adapter_class, b_adapter_ctor, (jlong)obj_adapter); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; +} + +void Java_BluetoothDevice_delete(JNIEnv *env, jobject obj) +{ + BluetoothDevice *b_device = getInstance<BluetoothDevice>(env, obj); + delete b_device; +} + diff --git a/java/jni/BluetoothDevice.h b/java/jni/BluetoothDevice.h new file mode 100644 index 00000000..3c03fcae --- /dev/null +++ b/java/jni/BluetoothDevice.h @@ -0,0 +1,237 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothDevice */ + +#ifndef _Included_BluetoothDevice +#define _Included_BluetoothDevice +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothDevice + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothDevice_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: clone + * Signature: ()LBluetoothDevice; + */ +JNIEXPORT jobject JNICALL Java_BluetoothDevice_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: disconnect + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_disconnect + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: connect + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_connect + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: connectProfile + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_connectProfile + (JNIEnv *, jobject, jstring); + +/* + * Class: BluetoothDevice + * Method: disconnectProfile + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_disconnectProfile + (JNIEnv *, jobject, jstring); + +/* + * Class: BluetoothDevice + * Method: pair + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_pair + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: cancelPairing + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_cancelPairing + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getServices + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothDevice_getServices + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getAddress + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothDevice_getAddress + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getName + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothDevice_getName + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getAlias + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothDevice_getAlias + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: setAlias + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_BluetoothDevice_setAlias + (JNIEnv *, jobject, jstring); + +/* + * Class: BluetoothDevice + * Method: getBluetoothClass + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_BluetoothDevice_getBluetoothClass + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getAppearance + * Signature: ()S + */ +JNIEXPORT jshort JNICALL Java_BluetoothDevice_getAppearance + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getIcon + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothDevice_getIcon + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getPaired + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_getPaired + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getTrusted + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_getTrusted + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: setTrusted + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_BluetoothDevice_setTrusted + (JNIEnv *, jobject, jboolean); + +/* + * Class: BluetoothDevice + * Method: getBlocked + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_getBlocked + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: setBlocked + * Signature: (Z)V + */ +JNIEXPORT void JNICALL Java_BluetoothDevice_setBlocked + (JNIEnv *, jobject, jboolean); + +/* + * Class: BluetoothDevice + * Method: getLegacyPairing + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_getLegacyPairing + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getRssi + * Signature: ()S + */ +JNIEXPORT jshort JNICALL Java_BluetoothDevice_getRssi + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getConnected + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothDevice_getConnected + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getUuids + * Signature: ()[Ljava/lang/String; + */ +JNIEXPORT jobjectArray JNICALL Java_BluetoothDevice_getUuids + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getModalias + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothDevice_getModalias + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: getAdapter + * Signature: ()LBluetoothAdapter; + */ +JNIEXPORT jobject JNICALL Java_BluetoothDevice_getAdapter + (JNIEnv *, jobject); + +/* + * Class: BluetoothDevice + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothDevice_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothEvent.cxx b/java/jni/BluetoothEvent.cxx new file mode 100644 index 00000000..9f6b9720 --- /dev/null +++ b/java/jni/BluetoothEvent.cxx @@ -0,0 +1,32 @@ +#include "BluetoothEvent.h" + +jobject Java_BluetoothEvent_getType(JNIEnv *env, jobject obj) +{ +} + +jstring Java_BluetoothEvent_getName(JNIEnv *env, jobject obj) +{ +} + +jstring Java_BluetoothEvent_getIdentifier(JNIEnv *env, jobject obj) +{ +} + +jboolean Java_BluetoothEvent_executeCallback(JNIEnv *env, jobject obj) +{ +} + +jboolean Java_BluetoothEvent_hasCallback(JNIEnv *env, jobject obj) +{ +} + +void Java_BluetoothEvent_init(JNIEnv *env, jobject obj, jobject type, jstring name, + jstring identifier, jobject parent, jobject callback, + jobject arg_data) +{ +} + +void Java_BluetoothEvent_delete(JNIEnv *env, jobject obj) +{ +} + diff --git a/java/jni/BluetoothEvent.h b/java/jni/BluetoothEvent.h new file mode 100644 index 00000000..6fab5f65 --- /dev/null +++ b/java/jni/BluetoothEvent.h @@ -0,0 +1,69 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothEvent */ + +#ifndef _Included_BluetoothEvent +#define _Included_BluetoothEvent +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothEvent + * Method: getType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothEvent_getType + (JNIEnv *, jobject); + +/* + * Class: BluetoothEvent + * Method: getName + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothEvent_getName + (JNIEnv *, jobject); + +/* + * Class: BluetoothEvent + * Method: getIdentifier + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothEvent_getIdentifier + (JNIEnv *, jobject); + +/* + * Class: BluetoothEvent + * Method: executeCallback + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothEvent_executeCallback + (JNIEnv *, jobject); + +/* + * Class: BluetoothEvent + * Method: hasCallback + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothEvent_hasCallback + (JNIEnv *, jobject); + +/* + * Class: BluetoothEvent + * Method: init + * Signature: (LBluetoothType;Ljava/lang/String;Ljava/lang/String;LBluetoothObject;LBluetoothCallback;Ljava/lang/Object;)V + */ +JNIEXPORT void JNICALL Java_BluetoothEvent_init + (JNIEnv *, jobject, jobject, jstring, jstring, jobject, jobject, jobject); + +/* + * Class: BluetoothEvent + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothEvent_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothGattCharacteristic.cxx b/java/jni/BluetoothGattCharacteristic.cxx new file mode 100644 index 00000000..2b2cfbe4 --- /dev/null +++ b/java/jni/BluetoothGattCharacteristic.cxx @@ -0,0 +1,185 @@ +#include "tinyb/BluetoothGattCharacteristic.hpp" +#include "tinyb/BluetoothGattDescriptor.hpp" +#include "tinyb/BluetoothGattService.hpp" +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothGattCharacteristic.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothGattCharacteristic_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "GATT_CHARACTERISTIC"); +} + +jobject Java_BluetoothGattCharacteristic_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothGattCharacteristic>(env, obj, "BluetoothGattCharacteristic"); +} + +jobject Java_BluetoothGattCharacteristic_readValue(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<unsigned char> array = obj_gatt_char->read_value(); + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + jclass byte_class = search_class(env, "Ljava/lang/Byte"); + jmethodID byte_ctor = search_method(env, byte_class, "<init>", "(B)V", false); + + for (unsigned int i = 0; i < array_size; ++i) + { + unsigned char elem = array.at(i); + jobject byte_obj = env->NewObject(byte_class, byte_ctor, elem); + if (byte_obj == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + env->CallBooleanMethod(result, arraylist_add, byte_obj); + } + + return result; +} + +jboolean Java_BluetoothGattCharacteristic_writeValue(JNIEnv *env, jobject obj, jobject argValue) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + jclass arraylist_class = search_class(env, "Ljava/util/ArrayList;"); + jmethodID arraylist_get = search_method(env, arraylist_class, "get", + "(I)Ljava/lang/Object;", false); + jmethodID arraylist_size = search_method(env, arraylist_class, "size", + "()I", false); + unsigned int size = env->CallIntMethod(argValue, arraylist_size); + + std::vector<unsigned char> array(size); + for (unsigned int i = 0; i < size; ++i) + { + unsigned char elem = env->CallByteMethod(argValue, arraylist_get, i); + array.push_back(elem); + } + + return obj_gatt_char->write_value(array); +} + +jboolean Java_BluetoothGattCharacteristic_startNotify(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->start_notify() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothGattCharacteristic_stopNotify(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->stop_notify() ? JNI_TRUE : JNI_FALSE; +} + +jstring Java_BluetoothGattCharacteristic_getUuid(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::string uuid = obj_gatt_char->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); +} + +jobject Java_BluetoothGattCharacteristic_getService(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + BluetoothGattService *obj_gatt_serv = obj_gatt_char->get_service().clone(); + + jclass b_gatt_serv_class = search_class(env, "BluetoothGattService"); + jmethodID b_gatt_serv_ctor = search_method(env, b_gatt_serv_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_gatt_serv_class, b_gatt_serv_ctor, (jlong)obj_gatt_serv); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; +} + +jobject Java_BluetoothGattCharacteristic_getValue(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<unsigned char> array = obj_gatt_char->get_value(); + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + jclass byte_class = search_class(env, "Ljava/lang/Byte"); + jmethodID byte_ctor = search_method(env, byte_class, "<init>", "(B)V", false); + + for (unsigned int i = 0; i < array_size; ++i) + { + unsigned char elem = array.at(i); + jobject byte_obj = env->NewObject(byte_class, byte_ctor, elem); + if (byte_obj == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + env->CallBooleanMethod(result, arraylist_add, byte_obj); + } + + return result; +} + +jboolean Java_BluetoothGattCharacteristic_getNotifying(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + return obj_gatt_char->get_notifying() ? JNI_TRUE : JNI_FALSE; +} + +jobjectArray Java_BluetoothGattCharacteristic_getFlags(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<std::string> flags = obj_gatt_char->get_flags(); + unsigned int flags_size = flags.size(); + + jclass string_class = search_class(env, "Ljava/lang/String;"); + jobjectArray result = env->NewObjectArray(flags_size, string_class, 0); + + for (unsigned int i = 0; i < flags_size; ++i) + { + std::string str_elem = flags.at(i); + jobject elem = env->NewStringUTF((const char *)str_elem.c_str()); + env->SetObjectArrayElement(result, i, elem); + } + + return result; +} + +jobject Java_BluetoothGattCharacteristic_getDescriptors(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + std::vector<std::unique_ptr<BluetoothGattDescriptor>> array = obj_gatt_char->get_descriptors(); + + jobject result = convert_vector_to_jobject<BluetoothGattDescriptor>(env, array, + "BluetoothGattDescriptor", + "(J)V"); + return result; +} + +void Java_BluetoothGattCharacteristic_delete(JNIEnv *env, jobject obj) +{ + BluetoothGattCharacteristic *obj_gatt_char = + getInstance<BluetoothGattCharacteristic>(env, obj); + delete obj_gatt_char; +} + diff --git a/java/jni/BluetoothGattCharacteristic.h b/java/jni/BluetoothGattCharacteristic.h new file mode 100644 index 00000000..efb44419 --- /dev/null +++ b/java/jni/BluetoothGattCharacteristic.h @@ -0,0 +1,125 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothGattCharacteristic */ + +#ifndef _Included_BluetoothGattCharacteristic +#define _Included_BluetoothGattCharacteristic +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothGattCharacteristic + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: clone + * Signature: ()LBluetoothGattCharacteristic; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: readValue + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_readValue + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: writeValue + * Signature: (Ljava/util/List;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattCharacteristic_writeValue + (JNIEnv *, jobject, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: startNotify + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattCharacteristic_startNotify + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: stopNotify + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattCharacteristic_stopNotify + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getUuid + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothGattCharacteristic_getUuid + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getService + * Signature: ()LBluetoothGattService; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_getService + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getValue + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_getValue + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getNotifying + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattCharacteristic_getNotifying + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getFlags + * Signature: ()[Ljava/lang/String; + */ +JNIEXPORT jobjectArray JNICALL Java_BluetoothGattCharacteristic_getFlags + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: getDescriptors + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattCharacteristic_getDescriptors + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: init + * Signature: (LBluetoothGattCharacteristic;)V + */ +JNIEXPORT void JNICALL Java_BluetoothGattCharacteristic_init + (JNIEnv *, jobject, jobject); + +/* + * Class: BluetoothGattCharacteristic + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothGattCharacteristic_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothGattDescriptor.cxx b/java/jni/BluetoothGattDescriptor.cxx new file mode 100644 index 00000000..f218fe02 --- /dev/null +++ b/java/jni/BluetoothGattDescriptor.cxx @@ -0,0 +1,127 @@ +#include "tinyb/BluetoothGattCharacteristic.hpp" +#include "tinyb/BluetoothGattDescriptor.hpp" +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothGattDescriptor.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothGattDescriptor_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "GATT_DESCRIPTOR"); +} + +jobject Java_BluetoothGattDescriptor_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothGattDescriptor>(env, obj, "BluetoothGattDescriptor"); +} + +jobject Java_BluetoothGattDescriptor_readValue(JNIEnv *env, jobject obj) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::vector<unsigned char> array = obj_gatt_desc->read_value(); + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + jclass byte_class = search_class(env, "Ljava/lang/Byte"); + jmethodID byte_ctor = search_method(env, byte_class, "<init>", "(B)V", false); + + for (unsigned int i = 0; i < array_size; ++i) + { + unsigned char elem = array.at(i); + jobject byte_obj = env->NewObject(byte_class, byte_ctor, elem); + if (byte_obj == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + env->CallBooleanMethod(result, arraylist_add, byte_obj); + } + + return result; +} + +jboolean Java_BluetoothGattDescriptor_writeValue(JNIEnv *env, jobject obj, jobject argValue) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + + jclass arraylist_class = search_class(env, "Ljava/util/ArrayList;"); + jmethodID arraylist_get = search_method(env, arraylist_class, "get", + "(I)Ljava/lang/Object;", false); + jmethodID arraylist_size = search_method(env, arraylist_class, "size", + "()I", false); + unsigned int size = env->CallIntMethod(argValue, arraylist_size); + + std::vector<unsigned char> array(size); + for (unsigned int i = 0; i < size; ++i) + { + unsigned char elem = env->CallByteMethod(argValue, arraylist_get, i); + array.push_back(elem); + } + + return obj_gatt_desc->write_value(array); +} + +jstring Java_BluetoothGattDescriptor_getUuid(JNIEnv *env, jobject obj) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::string uuid = obj_gatt_desc->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); +} + +jobject Java_BluetoothGattDescriptor_getCharacteristic(JNIEnv *env, jobject obj) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + BluetoothGattCharacteristic *obj_gatt_char = obj_gatt_desc->get_characteristic().clone(); + + jclass b_gatt_char_class = search_class(env, "BluetoothGattCharacteristic"); + jmethodID b_gatt_char_ctor = search_method(env, b_gatt_char_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_gatt_char_class, b_gatt_char_ctor, (jlong)obj_gatt_char); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; +} + +jobject Java_BluetoothGattDescriptor_getValue(JNIEnv *env, jobject obj) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + std::vector<unsigned char> array = obj_gatt_desc->get_value(); + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + jclass byte_class = search_class(env, "Ljava/lang/Byte"); + jmethodID byte_ctor = search_method(env, byte_class, "<init>", "(B)V", false); + + for (unsigned int i = 0; i < array_size; ++i) + { + unsigned char elem = array.at(i); + jobject byte_obj = env->NewObject(byte_class, byte_ctor, elem); + if (byte_obj == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + env->CallBooleanMethod(result, arraylist_add, byte_obj); + } + + return result; + +} + +void Java_BluetoothGattDescriptor_delete(JNIEnv *env, jobject obj) +{ + BluetoothGattDescriptor *obj_gatt_desc = getInstance<BluetoothGattDescriptor>(env, obj); + delete obj_gatt_desc; +} + diff --git a/java/jni/BluetoothGattDescriptor.h b/java/jni/BluetoothGattDescriptor.h new file mode 100644 index 00000000..42fd959c --- /dev/null +++ b/java/jni/BluetoothGattDescriptor.h @@ -0,0 +1,77 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothGattDescriptor */ + +#ifndef _Included_BluetoothGattDescriptor +#define _Included_BluetoothGattDescriptor +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothGattDescriptor + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattDescriptor_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: clone + * Signature: ()LBluetoothAdapter; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattDescriptor_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: readValue + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattDescriptor_readValue + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: writeValue + * Signature: (Ljava/util/List;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattDescriptor_writeValue + (JNIEnv *, jobject, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: getUuid + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothGattDescriptor_getUuid + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: getCharacteristic + * Signature: ()LBluetoothGattCharacteristic; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattDescriptor_getCharacteristic + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: getValue + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattDescriptor_getValue + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattDescriptor + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothGattDescriptor_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothGattService.cxx b/java/jni/BluetoothGattService.cxx new file mode 100644 index 00000000..39efad0f --- /dev/null +++ b/java/jni/BluetoothGattService.cxx @@ -0,0 +1,72 @@ +#include "tinyb/BluetoothDevice.hpp" +#include "tinyb/BluetoothGattService.hpp" +#include "tinyb/BluetoothGattCharacteristic.hpp" +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothGattService.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothGattService_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "GATT_SERVICE"); +} + +jobject Java_BluetoothGattService_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothGattService>(env, obj, "BluetoothGattService"); +} + +jstring Java_BluetoothGattService_getUuid(JNIEnv *env, jobject obj) +{ + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + std::string uuid = obj_gatt_serv->get_uuid(); + + return env->NewStringUTF((const char *)uuid.c_str()); +} + +jobject Java_BluetoothGattService_getDevice(JNIEnv *env, jobject obj) +{ + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + BluetoothDevice *obj_device = obj_gatt_serv->get_device().clone(); + + jclass b_device_class = search_class(env, "BluetoothDevice"); + jmethodID b_device_ctor = search_method(env, b_device_class, "<init>", + "(J)V", false); + jobject result = env->NewObject(b_device_class, b_device_ctor, (jlong)obj_device); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; +} + +jboolean Java_BluetoothGattService_getPrimary(JNIEnv *env, jobject obj) +{ + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + + return obj_gatt_serv->get_primary() ? JNI_TRUE : JNI_FALSE; +} + +jobject Java_BluetoothGattService_getCharacteristics(JNIEnv *env, jobject obj) +{ + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + std::vector<std::unique_ptr<BluetoothGattCharacteristic>> array = + obj_gatt_serv->get_characteristics(); + jobject result = convert_vector_to_jobject<BluetoothGattCharacteristic>(env, array, + "BluetoothGattCharacteristic", + "(J)V"); + return result; +} + +void Java_BluetoothGattService_delete(JNIEnv *env, jobject obj) +{ + BluetoothGattService *obj_gatt_serv = getInstance<BluetoothGattService>(env, obj); + delete obj_gatt_serv; +} + diff --git a/java/jni/BluetoothGattService.h b/java/jni/BluetoothGattService.h new file mode 100644 index 00000000..81addb34 --- /dev/null +++ b/java/jni/BluetoothGattService.h @@ -0,0 +1,69 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothGattService */ + +#ifndef _Included_BluetoothGattService +#define _Included_BluetoothGattService +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothGattService + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattService_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: clone + * Signature: ()LBluetoothAdapter; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattService_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: getUuid + * Signature: ()Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_BluetoothGattService_getUuid + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: getDevice + * Signature: ()LBluetoothDevice; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattService_getDevice + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: getPrimary + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothGattService_getPrimary + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: getCharacteristics + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothGattService_getCharacteristics + (JNIEnv *, jobject); + +/* + * Class: BluetoothGattService + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothGattService_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothManager.cxx b/java/jni/BluetoothManager.cxx new file mode 100644 index 00000000..749eac72 --- /dev/null +++ b/java/jni/BluetoothManager.cxx @@ -0,0 +1,110 @@ +#include "tinyb/BluetoothAdapter.hpp" +#include "tinyb/BluetoothDevice.hpp" +#include "tinyb/BluetoothGattService.hpp" +#include "tinyb/BluetoothManager.hpp" + +#include "BluetoothManager.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothManager_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "NONE"); +} + +jobject Java_BluetoothManager_getObject(JNIEnv *env, jobject obj, jobject type, + jstring name, jstring identifier, jobject parent) +{ + (void)env; + (void)obj; + (void)type; + (void)name; + (void)identifier; + (void)parent; + + return nullptr; +} + +jobject Java_BluetoothManager_getObjects(JNIEnv *env, jobject obj, jobject type, + jstring name, jstring identifier, jobject parent) +{ + (void)env; + (void)obj; + (void)type; + (void)name; + (void)identifier; + (void)parent; + + return nullptr; +} + +jobject Java_BluetoothManager_getAdapters(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothAdapter>> array = manager->get_adapters(); + jobject result = convert_vector_to_jobject<BluetoothAdapter>(env, array, + "BluetoothAdapter", + "(J)V"); + return result; +} + +jobject Java_BluetoothManager_getDevices(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothDevice>> array = manager->get_devices(); + jobject result = convert_vector_to_jobject<BluetoothDevice>(env, array, + "BluetoothDevice", + "(J)V"); + return result; + +} + +jobject Java_BluetoothManager_getServices(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + + std::vector<std::unique_ptr<BluetoothGattService>> array = manager->get_services(); + jobject result = convert_vector_to_jobject<BluetoothGattService>(env, array, + "BluetoothGattService", + "(J)V"); + return result; +} + +jboolean Java_BluetoothManager_setDefaultAdapter(JNIEnv *env, jobject obj, jobject adapter) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + BluetoothAdapter *b_adapter = getInstance<BluetoothAdapter>(env, adapter); + + return manager->set_default_adapter(b_adapter); +} + +jboolean Java_BluetoothManager_startDiscovery(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; +} + +jboolean Java_BluetoothManager_stopDiscovery(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + return manager->start_discovery() ? JNI_TRUE : JNI_FALSE; +} + +void Java_BluetoothManager_init(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = BluetoothManager::get_bluetooth_manager(); + setInstance<BluetoothManager>(env, obj, manager); +} + +void Java_BluetoothManager_delete(JNIEnv *env, jobject obj) +{ + BluetoothManager *manager = getInstance<BluetoothManager>(env, obj); + delete manager; +} + diff --git a/java/jni/BluetoothManager.h b/java/jni/BluetoothManager.h new file mode 100644 index 00000000..65f2db47 --- /dev/null +++ b/java/jni/BluetoothManager.h @@ -0,0 +1,101 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothManager */ + +#ifndef _Included_BluetoothManager +#define _Included_BluetoothManager +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothManager + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: getObject + * Signature: (LBluetoothType;Ljava/lang/String;Ljava/lang/String;LBluetoothObject;)LBluetoothObject; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getObject + (JNIEnv *, jobject, jobject, jstring, jstring, jobject); + +/* + * Class: BluetoothManager + * Method: getObjects + * Signature: (LBluetoothType;Ljava/lang/String;Ljava/lang/String;LBluetoothObject;)Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getObjects + (JNIEnv *, jobject, jobject, jstring, jstring, jobject); + +/* + * Class: BluetoothManager + * Method: getAdapters + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getAdapters + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: getDevices + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getDevices + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: getServices + * Signature: ()Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_BluetoothManager_getServices + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: setDefaultAdapter + * Signature: (LBluetoothAdapter;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothManager_setDefaultAdapter + (JNIEnv *, jobject, jobject); + +/* + * Class: BluetoothManager + * Method: startDiscovery + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothManager_startDiscovery + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: stopDiscovery + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothManager_stopDiscovery + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: init + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothManager_init + (JNIEnv *, jobject); + +/* + * Class: BluetoothManager + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothManager_delete + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothObject.cxx b/java/jni/BluetoothObject.cxx new file mode 100644 index 00000000..cb1d5b51 --- /dev/null +++ b/java/jni/BluetoothObject.cxx @@ -0,0 +1,35 @@ +#include "tinyb/BluetoothObject.hpp" + +#include "BluetoothObject.h" + +#include "helper.h" + +using namespace tinyb; + +jobject Java_BluetoothObject_getBluetoothType(JNIEnv *env, jobject obj) +{ + (void)obj; + + return get_bluetooth_type(env, "NONE"); +} + +jobject Java_BluetoothObject_clone(JNIEnv *env, jobject obj) +{ + return generic_clone<BluetoothObject>(env, obj, "BluetoothObject"); +} + +void Java_BluetoothObject_delete(JNIEnv *env, jobject obj) +{ + BluetoothObject *obj_b = getInstance<BluetoothObject>(env, obj); + + delete obj_b; +} + +jboolean Java_BluetoothObject_operatorEqual(JNIEnv *env, jobject obj, jobject other) +{ + BluetoothObject *obj_b = getInstance<BluetoothObject>(env, obj); + BluetoothObject *obj_other = getInstance<BluetoothObject>(env, other); + + return (*obj_b) == (*obj_other); +} + diff --git a/java/jni/BluetoothObject.h b/java/jni/BluetoothObject.h new file mode 100644 index 00000000..193c9632 --- /dev/null +++ b/java/jni/BluetoothObject.h @@ -0,0 +1,45 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothObject */ + +#ifndef _Included_BluetoothObject +#define _Included_BluetoothObject +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: BluetoothObject + * Method: getBluetoothType + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothObject_getBluetoothType + (JNIEnv *, jobject); + +/* + * Class: BluetoothObject + * Method: clone + * Signature: ()LBluetoothType; + */ +JNIEXPORT jobject JNICALL Java_BluetoothObject_clone + (JNIEnv *, jobject); + +/* + * Class: BluetoothObject + * Method: delete + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_BluetoothObject_delete + (JNIEnv *, jobject); + +/* + * Class: BluetoothObject + * Method: operatorEqual + * Signature: (LBluetoothObject;)Z + */ +JNIEXPORT jboolean JNICALL Java_BluetoothObject_operatorEqual + (JNIEnv *, jobject, jobject); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/BluetoothType.h b/java/jni/BluetoothType.h new file mode 100644 index 00000000..5c80ea78 --- /dev/null +++ b/java/jni/BluetoothType.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class BluetoothType */ + +#ifndef _Included_BluetoothType +#define _Included_BluetoothType +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/ObjectArgCallback.h b/java/jni/ObjectArgCallback.h new file mode 100644 index 00000000..884d8432 --- /dev/null +++ b/java/jni/ObjectArgCallback.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class ObjectArgCallback */ + +#ifndef _Included_ObjectArgCallback +#define _Included_ObjectArgCallback +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/ObjectArrayArgCallback.h b/java/jni/ObjectArrayArgCallback.h new file mode 100644 index 00000000..081bbd5b --- /dev/null +++ b/java/jni/ObjectArrayArgCallback.h @@ -0,0 +1,13 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include <jni.h> +/* Header for class ObjectArrayArgCallback */ + +#ifndef _Included_ObjectArrayArgCallback +#define _Included_ObjectArrayArgCallback +#ifdef __cplusplus +extern "C" { +#endif +#ifdef __cplusplus +} +#endif +#endif diff --git a/java/jni/helper.cxx b/java/jni/helper.cxx new file mode 100644 index 00000000..9f41eea3 --- /dev/null +++ b/java/jni/helper.cxx @@ -0,0 +1,115 @@ +#include <jni.h> +#include <memory> +#include <stdexcept> +#include <vector> + +#include "helper.h" + +jfieldID getInstanceField(JNIEnv *env, jobject obj) +{ + jclass clazz = env->GetObjectClass(obj); + // J == long + return env->GetFieldID(clazz, "nativeInstance", "J"); +} + +jclass search_class(JNIEnv *env, const char *clazz_name) +{ + jclass clazz = env->FindClass(clazz_name); + if (clazz == NULL) + { + throw std::runtime_error("no class found\n"); + } + return clazz; +} + +jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, + const char *prototype, bool is_static) +{ + jmethodID method; + if(is_static) + { + method = env->GetStaticMethodID(clazz, method_name, prototype); + } + else + { + method = env->GetMethodID(clazz, method_name, prototype); + } + + if(method == NULL) + { + throw std::runtime_error("no method found\n"); + } + + return method; +} + +jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, + const char *type, bool is_static) +{ + jfieldID field; + if(is_static) + { + field = env->GetStaticFieldID(clazz, field_name, type); + } + else + { + field = env->GetFieldID(clazz, field_name, type); + } + + if(field == NULL) + { + throw std::runtime_error("no method found\n"); + } + + return field; +} + +bool from_jboolean_to_bool(jboolean val) +{ + bool result; + + if (val == JNI_TRUE) + { + result = true; + } + else + { + if (val == JNI_FALSE) + { + result = false; + } + else + { + throw std::invalid_argument("the jboolean value is not true/false\n"); + } + } + + return result; +} + +jobject get_bluetooth_type(JNIEnv *env, const char *field_name) +{ + jclass b_type_enum = search_class(env, "BluetoothType"); + + jfieldID b_type_field = search_field(env, b_type_enum, field_name, "l", true); + + jobject result = env->GetStaticObjectField(b_type_enum, b_type_field); + return result; +} + + +jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add) +{ + jclass arraylist_class = search_class(env, "Ljava/util/ArrayList;"); + jmethodID arraylist_ctor = search_method(env, arraylist_class, "<init>", "(I)V", false); + + jobject result = env->NewObject(arraylist_class, arraylist_ctor, size); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + *add = search_method(env, arraylist_class, "add", "(Ljava/lang/Object;)Z", false); + + return result; +} diff --git a/java/jni/helper.h b/java/jni/helper.h new file mode 100644 index 00000000..30617506 --- /dev/null +++ b/java/jni/helper.h @@ -0,0 +1,74 @@ +#ifndef _HELPER_H_ +#define _HELPER_H_ + +#include <vector> + +jfieldID getInstanceField(JNIEnv *env, jobject obj); + +jclass search_class(JNIEnv *env, const char *clazz_name); +jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, + const char *prototype, bool is_static); +jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name, + const char *type, bool is_static); +bool from_jboolean_to_bool(jboolean val); +jobject get_bluetooth_type(JNIEnv *env, const char *field_name); +jobject get_new_arraylist(JNIEnv *env, unsigned int size, jmethodID *add); + +template <typename T> +T *getInstance(JNIEnv *env, jobject obj) +{ + jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); + return reinterpret_cast<T *>(instance); +} + +template <typename T> +void setInstance(JNIEnv *env, jobject obj, T *t) +{ + jlong instance = reinterpret_cast<jlong>(t); + env->SetLongField(obj, getInstanceField(env, obj), instance); +} + +template <typename T> +jobject generic_clone(JNIEnv *env, jobject obj, const char *class_name) +{ + T *obj_generic = getInstance<T>(env, obj); + T *copy_generic = new T(*obj_generic); + + jclass generic_class = search_class(env, class_name); + jmethodID generic_ctor = search_method(env, generic_class, "<init>", "(J)V", false); + + jobject result = env->NewObject(generic_class, generic_ctor, (jlong)copy_generic); + if (result == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + + return result; +} + +template <typename T> +jobject convert_vector_to_jobject(JNIEnv *env, std::vector<std::unique_ptr<T>>& array, + const char *class_name, const char *ctor_prototype) +{ + unsigned int array_size = array.size(); + + jmethodID arraylist_add; + jobject result = get_new_arraylist(env, array_size, &arraylist_add); + + jclass clazz = search_class(env, class_name); + jmethodID clazz_ctor = search_method(env, clazz, "<init>", ctor_prototype, false); + + for (unsigned int i = 0; i < array_size; ++i) + { + T *elem = array.at(i).release(); + jobject object = env->NewObject(clazz, clazz_ctor, (long)elem); + if (object == NULL) + { + throw std::runtime_error("cannot create instance of class\n"); + } + env->CallBooleanMethod(result, arraylist_add, object); + } + return result; +} + +#endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3a016b30..262faf0e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,6 +27,7 @@ set (tinyb_LIB_SRCS ) set (tinyb_LIB_GLOB_HEADERS + ${PROJECT_SOURCE_DIR}/api/tinyb.h ${PROJECT_SOURCE_DIR}/api/tinyb.hpp ) @@ -56,14 +57,3 @@ tinyb_create_install_pkgconfig (tinyb.pc lib${LIB_SUFFIX}/pkgconfig) install(TARGETS tinyb LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}) -if (BUILDSWIG) - find_package (SWIG) - if (SWIG_FOUND) - include (${SWIG_USE_FILE}) - set_source_files_properties (tinyb.i PROPERTIES SWIG_FLAGS "-I${CMAKE_BINARY_DIR}/src") - - if (BUILDSWIGJAVA) - add_subdirectory (java) - endif () - endif () -endif () |