Skip to content
This repository was archived by the owner on Jan 4, 2023. It is now read-only.

Commit 2c3706f

Browse files
juiicepetreeftime
authored andcommitted
Fix JNI FindClass "tinyb/BluetoothNotification" not found
- Issue #52 : JNI call to findClass BluetoothNotification failed - This issue happen with javafx application, but not with "simple" java application. - This issue happen only with "tinyb/BluetoothNotification" interface. - Using the callback reference to retrieve the class instead of using FindClass works fine. Signed-off-by: Julien Decaen <[email protected]>
1 parent d9c777b commit 2c3706f

File tree

6 files changed

+23
-11
lines changed

6 files changed

+23
-11
lines changed

java/jni/BluetoothAdapter.cxx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void Java_tinyb_BluetoothAdapter_enablePoweredNotifications(JNIEnv *env, jobject
286286
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
287287
obj_adapter->enable_powered_notifications([ callback_ptr ] (bool v)
288288
{
289-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
289+
jclass notification = search_class(*jni_env, **callback_ptr);
290290
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
291291
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
292292
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -376,7 +376,7 @@ void Java_tinyb_BluetoothAdapter_enableDiscoverableNotifications(JNIEnv *env, jo
376376
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
377377
obj_adapter->enable_discoverable_notifications([ callback_ptr ] (bool v)
378378
{
379-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
379+
jclass notification = search_class(*jni_env, **callback_ptr);
380380
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
381381
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
382382
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -489,7 +489,7 @@ void Java_tinyb_BluetoothAdapter_enablePairableNotifications(JNIEnv *env, jobjec
489489
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
490490
obj_adapter->enable_pairable_notifications([ callback_ptr ] (bool v)
491491
{
492-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
492+
jclass notification = search_class(*jni_env, **callback_ptr);
493493
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
494494
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
495495
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -622,7 +622,7 @@ void Java_tinyb_BluetoothAdapter_enableDiscoveringNotifications(JNIEnv *env, job
622622
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
623623
obj_adapter->enable_discovering_notifications([ callback_ptr ] (bool v)
624624
{
625-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
625+
jclass notification = search_class(*jni_env, **callback_ptr);
626626
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
627627
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
628628
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

java/jni/BluetoothDevice.cxx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ void Java_tinyb_BluetoothDevice_enablePairedNotifications(JNIEnv *env, jobject o
394394
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
395395
obj_device->enable_paired_notifications([ callback_ptr ] (bool v)
396396
{
397-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
397+
jclass notification = search_class(*jni_env, **callback_ptr);
398398
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
399399
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
400400
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -484,7 +484,7 @@ void Java_tinyb_BluetoothDevice_enableTrustedNotifications(JNIEnv *env, jobject
484484
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
485485
obj_device->enable_trusted_notifications([ callback_ptr ] (bool v)
486486
{
487-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
487+
jclass notification = search_class(*jni_env, **callback_ptr);
488488
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
489489
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
490490
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -574,7 +574,7 @@ void Java_tinyb_BluetoothDevice_enableBlockedNotifications(JNIEnv *env, jobject
574574
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
575575
obj_device->enable_blocked_notifications([ callback_ptr ] (bool v)
576576
{
577-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
577+
jclass notification = search_class(*jni_env, **callback_ptr);
578578
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
579579
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
580580
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);
@@ -664,7 +664,7 @@ void Java_tinyb_BluetoothDevice_enableRSSINotifications(JNIEnv *env, jobject obj
664664
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
665665
obj_device->enable_rssi_notifications([ callback_ptr ] (int16_t v)
666666
{
667-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
667+
jclass notification = search_class(*jni_env, **callback_ptr);
668668
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
669669
jclass short_cls = search_class(*jni_env, "java/lang/Short");
670670
jmethodID constructor = search_method(*jni_env, short_cls, "<init>", "(S)V", false);
@@ -734,7 +734,7 @@ void Java_tinyb_BluetoothDevice_enableConnectedNotifications(JNIEnv *env, jobjec
734734
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
735735
obj_device->enable_connected_notifications([ callback_ptr ] (bool v)
736736
{
737-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
737+
jclass notification = search_class(*jni_env, **callback_ptr);
738738
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
739739
jclass boolean_cls = search_class(*jni_env, "java/lang/Boolean");
740740
jmethodID constructor = search_method(*jni_env, boolean_cls, "<init>", "(Z)V", false);

java/jni/BluetoothGattCharacteristic.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void Java_tinyb_BluetoothGattCharacteristic_enableValueNotifications(JNIEnv *env
139139
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
140140
obj_gatt_char->enable_value_notifications([ callback_ptr ] (std::vector<unsigned char> &v)
141141
{
142-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
142+
jclass notification = search_class(*jni_env, **callback_ptr);
143143
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
144144
unsigned int size = v.size();
145145

java/jni/BluetoothGattDescriptor.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void Java_tinyb_BluetoothGattDescriptor_enableValueNotifications(JNIEnv *env, jo
212212
std::shared_ptr<JNIGlobalRef> callback_ptr(new JNIGlobalRef(callback));
213213
obj_gatt_desc->enable_value_notifications([ callback_ptr ] (std::vector<unsigned char> &v)
214214
{
215-
jclass notification = search_class(*jni_env, JAVA_PACKAGE "/BluetoothNotification");
215+
jclass notification = search_class(*jni_env, **callback_ptr);
216216
jmethodID method = search_method(*jni_env, notification, "run", "(Ljava/lang/Object;)V", false);
217217
unsigned int size = v.size();
218218

java/jni/helper.cxx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ jclass search_class(JNIEnv *env, const char *clazz_name)
5252
return clazz;
5353
}
5454

55+
jclass search_class(JNIEnv *env, jobject obj)
56+
{
57+
jclass clazz = env->GetObjectClass(obj);
58+
if (!clazz)
59+
{
60+
std::string error = "no class found: ";
61+
throw std::runtime_error(error);
62+
}
63+
return clazz;
64+
}
65+
5566
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name,
5667
const char *prototype, bool is_static)
5768
{

java/jni/helper.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jfieldID getInstanceField(JNIEnv *env, jobject obj);
3232

3333
jclass search_class(JNIEnv *env, const char *clazz_name);
3434
jclass search_class(JNIEnv *env, tinyb::BluetoothObject &object);
35+
jclass search_class(JNIEnv *env, jobject obj);
3536
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name,
3637
const char *prototype, bool is_static);
3738
jfieldID search_field(JNIEnv *env, jclass clazz, const char *field_name,

0 commit comments

Comments
 (0)