diff options
author | Sven Gothel <[email protected]> | 2020-07-24 12:51:26 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-24 12:51:26 +0200 |
commit | 537b779c122605865f541a89a45b8d205dad5fb8 (patch) | |
tree | b8c13a2c490953e87a386ba78069983d416d59ce | |
parent | ad58716e9514619c5b7279f7882fc762afbcb582 (diff) |
DBTGattCharacteristic::configNotificationIndicationImpl(..): Tolerate deleted native instance @ disablev2.1.14
-rw-r--r-- | java/direct_bt/tinyb/DBTGattCharacteristic.java | 2 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattCharacteristic.cxx | 10 | ||||
-rw-r--r-- | java/jni/helper_base.hpp | 7 |
3 files changed, 17 insertions, 2 deletions
diff --git a/java/direct_bt/tinyb/DBTGattCharacteristic.java b/java/direct_bt/tinyb/DBTGattCharacteristic.java index d2086e5f..22c341bd 100644 --- a/java/direct_bt/tinyb/DBTGattCharacteristic.java +++ b/java/direct_bt/tinyb/DBTGattCharacteristic.java @@ -235,7 +235,7 @@ public class DBTGattCharacteristic extends DBTObject implements BluetoothGattCha enabledState[0] = false; enabledState[1] = false; if( DEBUG ) { - System.err.println("GATTCharacteristicListener.configNotificationIndication: FALSE*"); + System.err.println("GATTCharacteristicListener.configNotificationIndication: FALSE*: hasNotify "+hasNotify+", hasIndicate "+hasIndicate); } return false; } diff --git a/java/jni/direct_bt/DBTGattCharacteristic.cxx b/java/jni/direct_bt/DBTGattCharacteristic.cxx index f376618e..84483d6a 100644 --- a/java/jni/direct_bt/DBTGattCharacteristic.cxx +++ b/java/jni/direct_bt/DBTGattCharacteristic.cxx @@ -165,7 +165,15 @@ jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_writeValueImpl(JNIEnv *env, jboolean Java_direct_1bt_tinyb_DBTGattCharacteristic_configNotificationIndicationImpl(JNIEnv *env, jobject obj, jboolean enableNotification, jboolean enableIndication, jbooleanArray jEnabledState) { try { - GATTCharacteristic *characteristic = getInstance<GATTCharacteristic>(env, obj); + GATTCharacteristic *characteristic = getInstanceUnchecked<GATTCharacteristic>(env, obj); + if( nullptr == characteristic ) { + if( !enableNotification && !enableIndication ) { + // OK to have native characteristic being shutdown @ disable + DBG_PRINT("Characteristic's native instance has been deleted"); + return false; + } + throw IllegalStateException("Characteristic's native instance deleted", E_FILE_LINE); + } JavaGlobalObj::check(characteristic->getJavaObject(), E_FILE_LINE); if( nullptr == jEnabledState ) { diff --git a/java/jni/helper_base.hpp b/java/jni/helper_base.hpp index a1f8bbdc..18ae8679 100644 --- a/java/jni/helper_base.hpp +++ b/java/jni/helper_base.hpp @@ -116,6 +116,13 @@ T *getInstance(JNIEnv *env, jobject obj) } template <typename T> +T *getInstanceUnchecked(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) { if (t == nullptr) { |