diff options
author | Sven Gothel <[email protected]> | 2020-07-26 05:35:58 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-26 05:35:58 +0200 |
commit | 247a93e27eac0c8f7809802238b7d02b8ee2cf4b (patch) | |
tree | e777fa97a0f942dac89a35521db7bab1f6c8658f /java | |
parent | d3f8dd6310158fc0fc83bc57da7eececbb679fcb (diff) |
Have GATT[Service,Characteristic,Descriptor] derived from DBTObject for valid check; Use new JNI getInstance() -> getDBTObject() w/ valid check
Diffstat (limited to 'java')
-rw-r--r-- | java/jni/direct_bt/helper_dbt.hpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/java/jni/direct_bt/helper_dbt.hpp b/java/jni/direct_bt/helper_dbt.hpp index abd78b5..f644d58 100644 --- a/java/jni/direct_bt/helper_dbt.hpp +++ b/java/jni/direct_bt/helper_dbt.hpp @@ -149,6 +149,37 @@ namespace direct_bt { BDAddressType fromJavaAdressTypeToBDAddressType(JNIEnv *env, jstring jAddressType); jstring fromBDAddressTypeToJavaAddressType(JNIEnv *env, BDAddressType bdAddressType); + template <typename T> + T *getDBTObject(JNIEnv *env, jobject obj) + { + jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); + T *t = reinterpret_cast<T *>(instance); + if (t == nullptr) { + throw std::runtime_error("Trying to acquire null DBTObject"); + } + t->checkValid(); + return t; + } + + template <typename T> + T *getDBTObjectUnchecked(JNIEnv *env, jobject obj) + { + jlong instance = env->GetLongField(obj, getInstanceField(env, obj)); + return reinterpret_cast<T *>(instance); + } + + template <typename T> + void setDBTObject(JNIEnv *env, jobject obj, T *t) + { + if (t == nullptr) { + throw std::runtime_error("Trying to create null DBTObject"); + } + jlong instance = reinterpret_cast<jlong>(t); + env->SetLongField(obj, getInstanceField(env, obj), instance); + } + + + } // namespace direct_bt #endif /* HELPER_DBT_HPP_ */ |