当前位置:   article > 正文

android O 32位系统报错:JNI DETECTED ERROR IN APPLICATION: use of deleted local reference

jni detected error in application: use of deleted local reference

一、同样的程序我在android O 64位系统上跑没有任何问题,但是在32位系统上跑就报这个错误
问题原因是:
自己写的类中有这个构造函数:
public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {
    this.service_type = service_type;
    this.mostSiguuid = mostSiguuid;
    this.leastSiguuid = leastSiguuid;
    this.handle = handle;
    this.end_group_handle = end_group_handle;
    this.characteristic = characteristic;
}
然后再JNI调用这个构造函数:
jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");
这种调用方式就会有问题。
二、修改方法:使用没有参数的构造函数,然后使用set方法来设置类里面的成员变量
jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");
jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");
jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");
jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");
jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");
jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");
jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");

三、AnWBT_BLE_Service类的代码

  1. package com.anwsdk.service;
  2. import android.os.Parcel;
  3. import android.os.Parcelable;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class AnWBT_BLE_Service implements Parcelable {
  7. private int service_type;
  8. private long mostSiguuid;
  9. private long leastSiguuid;
  10. private int handle;
  11. private int end_group_handle;
  12. private ArrayList<AnWBT_BLE_Characteristic> characteristic;
  13. public AnWBT_BLE_Service() {
  14. }
  15. public AnWBT_BLE_Service(int service_type, long mostSiguuid, long leastSiguuid, int handle, int end_group_handle, ArrayList<AnWBT_BLE_Characteristic> characteristic) {
  16. this.service_type = service_type;
  17. this.mostSiguuid = mostSiguuid;
  18. this.leastSiguuid = leastSiguuid;
  19. this.handle = handle;
  20. this.end_group_handle = end_group_handle;
  21. this.characteristic = characteristic;
  22. }
  23. protected AnWBT_BLE_Service(Parcel in) {
  24. service_type = in.readInt();
  25. mostSiguuid = in.readLong();
  26. leastSiguuid = in.readLong();
  27. handle = in.readInt();
  28. end_group_handle = in.readInt();
  29. ArrayList<AnWBT_BLE_Characteristic> characteristic_new = new ArrayList<AnWBT_BLE_Characteristic>();
  30. in.readTypedList(characteristic_new,AnWBT_BLE_Characteristic.CREATOR);
  31. characteristic = characteristic_new;
  32. }
  33. public int getService_type() {
  34. return service_type;
  35. }
  36. public long getMostSiguuid() {
  37. return mostSiguuid;
  38. }
  39. public long getLeastSiguuid() {
  40. return leastSiguuid;
  41. }
  42. public int getHandle() {
  43. return handle;
  44. }
  45. public int getEnd_group_handle() {
  46. return end_group_handle;
  47. }
  48. public ArrayList<AnWBT_BLE_Characteristic> getCharacteristic() {
  49. return characteristic;
  50. }
  51. public void setService_type(int service_type) {
  52. this.service_type = service_type;
  53. }
  54. public void setMostSiguuid(long mostSiguuid) {
  55. this.mostSiguuid = mostSiguuid;
  56. }
  57. public void setLeastSiguuid(long leastSiguuid) {
  58. this.leastSiguuid = leastSiguuid;
  59. }
  60. public void setHandle(int handle) {
  61. this.handle = handle;
  62. }
  63. public void setEnd_group_handle(int end_group_handle) {
  64. this.end_group_handle = end_group_handle;
  65. }
  66. public void setCharacteristic(ArrayList<AnWBT_BLE_Characteristic> characteristic) {
  67. this.characteristic = characteristic;
  68. }
  69. @Override
  70. public int describeContents() {
  71. return 0;
  72. }
  73. @Override
  74. public void writeToParcel(Parcel dest, int flags) {
  75. dest.writeInt(service_type);
  76. dest.writeLong(mostSiguuid);
  77. dest.writeLong(leastSiguuid);
  78. dest.writeInt(handle);
  79. dest.writeInt(end_group_handle);
  80. dest.writeTypedList(characteristic);
  81. }
  82. public static final Creator<AnWBT_BLE_Service> CREATOR = new Creator<AnWBT_BLE_Service>() {
  83. @Override
  84. public AnWBT_BLE_Service createFromParcel(Parcel in) {
  85. return new AnWBT_BLE_Service(in);
  86. }
  87. @Override
  88. public AnWBT_BLE_Service[] newArray(int size) {
  89. return new AnWBT_BLE_Service[size];
  90. }
  91. };
  92. }

四、JNI层修改前代码

  1. /*
  2. * Class : com_anwsdk_service
  3. * Method :GATT_GetService()
  4. * Signature : (J)Ljava/util/List;
  5. */
  6. JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
  7. {
  8. ANW_LOGI("+++ GATT_GetService---");
  9. int i = 0;
  10. GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;
  11. jclass arraylist_cls = env->FindClass("java/util/ArrayList");
  12. jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");
  13. jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");
  14. jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  15. if(gattHandle == NULL){
  16. return service_arraylist;
  17. }
  18. AnW_BLE_Device device;
  19. GetBLEDeviceManagerByGATTHandle(&device,gattHandle);
  20. if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){
  21. return service_arraylist;
  22. }
  23. jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");
  24. jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","(IJJ[B)V");
  25. jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");
  26. jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","(IIIJJ[BLjava/util/ArrayList;)V");
  27. jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
  28. jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");
  29. for(i = 0;i < device.service_count;i++){
  30. jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  31. AnW_BLE_Service *service = &(device.ble_service[i]);
  32. int j = 0;
  33. for(j = 0;j < service->characteristic_count;j++){
  34. jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  35. AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);
  36. jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);
  37. int k = 0;
  38. for(k = 0;k < characteristic->descriptor_count;k++){
  39. AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);
  40. jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);
  41. jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,descriptor->handle,*(long *)(descriptor->uuid + 8),*(long *)(descriptor->uuid),descriptor_value);
  42. env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);
  43. }
  44. jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,characteristic->handle,characteristic->properties,characteristic->value_handle,*(long *)(characteristic->uuid + 8),*(long *)(characteristic->uuid),characteristic_value,descriptor_arraylist);
  45. env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);
  46. }
  47. jobject service_obj = env->NewObject(service_cls,service_init,service->service_type,*(long *)(service->uuid + 8),*(long *)(service->uuid),service->handle,service->end_group_handle,characteristic_arraylist);
  48. env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);
  49. }
  50. ANW_LOGI("--- GATT_GetService ---");
  51. return service_arraylist;
  52. }

JNI层修改后代码(这里面其实还修改了一个long类型)

  1. jlong uuid_lsb(u_int8* uuid) {
  2. jlong lsb = 0;
  3. for (int i = 7; i >= 0; i--) {
  4. lsb <<= 8;
  5. lsb |= uuid[i];
  6. }
  7. return lsb;
  8. }
  9. jlong uuid_msb(u_int8* uuid) {
  10. jlong msb = 0;
  11. for (int i = 15; i >= 8; i--) {
  12. msb <<= 8;
  13. msb |= uuid[i];
  14. }
  15. return msb;
  16. }
  17. /*
  18. * Class : com_anwsdk_service
  19. * Method :GATT_GetService()
  20. * Signature : (J)Ljava/util/List;
  21. */
  22. JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
  23. {
  24. ANW_LOGI("+++ GATT_GetService---(0x%llx)",gatt);
  25. int i = 0;
  26. GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;
  27. jclass arraylist_cls = env->FindClass("java/util/ArrayList");
  28. jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");
  29. jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");
  30. jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  31. if(gattHandle == NULL){
  32. return service_arraylist;
  33. }
  34. AnW_BLE_Device device;
  35. GetBLEDeviceManagerByGATTHandle(&device,gattHandle);
  36. if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){
  37. return service_arraylist;
  38. }
  39. jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");
  40. jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","()V");
  41. jmethodID descriptor_setHandle = env->GetMethodID(descriptor_cls,"setHandle","(I)V");
  42. jmethodID descriptor_setMostSiguuid = env->GetMethodID(descriptor_cls,"setMostSiguuid","(J)V");
  43. jmethodID descriptor_setLeastSiguuid = env->GetMethodID(descriptor_cls,"setLeastSiguuid","(J)V");
  44. jmethodID descriptor_setValue = env->GetMethodID(descriptor_cls,"setValue","([B)V");
  45. jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");
  46. jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","()V");
  47. jmethodID characteristic_setHandle = env->GetMethodID(characteristic_cls,"setHandle","(I)V");
  48. jmethodID characteristic_setProperties = env->GetMethodID(characteristic_cls,"setProperties","(I)V");
  49. jmethodID characteristic_setValue_handle = env->GetMethodID(characteristic_cls,"setValue_handle","(I)V");
  50. jmethodID characteristic_setMostSiguuid = env->GetMethodID(characteristic_cls,"setMostSiguuid","(J)V");
  51. jmethodID characteristic_setLeastSiguuid = env->GetMethodID(characteristic_cls,"setLeastSiguuid","(J)V");
  52. jmethodID characteristic_setValue = env->GetMethodID(characteristic_cls,"setValue","([B)V");
  53. jmethodID characteristic_setDescriptor = env->GetMethodID(characteristic_cls,"setDescriptor","(Ljava/util/ArrayList;)V");
  54. jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
  55. jmethodID service_init = env->GetMethodID(service_cls,"<init>","()V");
  56. jmethodID service_setService_type = env->GetMethodID(service_cls,"setService_type","(I)V");
  57. jmethodID service_setMostSiguuid = env->GetMethodID(service_cls,"setMostSiguuid","(J)V");
  58. jmethodID service_setLeastSiguuid = env->GetMethodID(service_cls,"setLeastSiguuid","(J)V");
  59. jmethodID service_setHandle = env->GetMethodID(service_cls,"setHandle","(I)V");
  60. jmethodID service_setEnd_group_handle = env->GetMethodID(service_cls,"setEnd_group_handle","(I)V");
  61. jmethodID service_setCharacteristic = env->GetMethodID(service_cls,"setCharacteristic","(Ljava/util/ArrayList;)V");
  62. for(i = 0;i < device.service_count;i++){
  63. jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  64. AnW_BLE_Service *service = device.ble_service + i;
  65. int j = 0;
  66. for(j = 0;j < service->characteristic_count;j++){
  67. jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
  68. AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);
  69. jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);
  70. int k = 0;
  71. for(k = 0;k < characteristic->descriptor_count;k++){
  72. AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);
  73. jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);
  74. jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,"");
  75. env->CallVoidMethod(descriptor_obj,descriptor_setHandle,descriptor->handle);
  76. env->CallVoidMethod(descriptor_obj,descriptor_setMostSiguuid,uuid_msb(descriptor->uuid));
  77. env->CallVoidMethod(descriptor_obj,descriptor_setLeastSiguuid,uuid_lsb(descriptor->uuid));
  78. env->CallVoidMethod(descriptor_obj,descriptor_setValue,NULL);
  79. env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);
  80. }
  81. jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,"");
  82. env->CallVoidMethod(characteristic_obj,characteristic_setHandle,characteristic->handle);
  83. env->CallVoidMethod(characteristic_obj,characteristic_setProperties,characteristic->properties);
  84. env->CallVoidMethod(characteristic_obj,characteristic_setValue_handle,characteristic->value_handle);
  85. env->CallVoidMethod(characteristic_obj,characteristic_setMostSiguuid,uuid_msb(characteristic->uuid));
  86. env->CallVoidMethod(characteristic_obj,characteristic_setLeastSiguuid,uuid_lsb(characteristic->uuid));
  87. env->CallVoidMethod(characteristic_obj,characteristic_setValue,NULL);
  88. env->CallVoidMethod(characteristic_obj,characteristic_setDescriptor,descriptor_arraylist);
  89. env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);
  90. }
  91. jobject service_obj = env->NewObject(service_cls,service_init,"");
  92. env->CallVoidMethod(service_obj,service_setService_type,service->service_type);
  93. env->CallVoidMethod(service_obj,service_setMostSiguuid,uuid_msb(service->uuid));
  94. env->CallVoidMethod(service_obj,service_setLeastSiguuid,uuid_lsb(service->uuid));
  95. env->CallVoidMethod(service_obj,service_setHandle,service->handle);
  96. env->CallVoidMethod(service_obj,service_setEnd_group_handle,service->end_group_handle);
  97. env->CallVoidMethod(service_obj,service_setCharacteristic,characteristic_arraylist);
  98. env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);
  99. }
  100. ANW_LOGI("--- GATT_GetService ---");
  101. return service_arraylist;
  102. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/158489
推荐阅读
相关标签
  

闽ICP备14008679号