赞
踩
一、同样的程序我在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类的代码
- package com.anwsdk.service;
-
- import android.os.Parcel;
- import android.os.Parcelable;
-
- import java.util.ArrayList;
- import java.util.List;
-
- public class AnWBT_BLE_Service implements Parcelable {
- private int service_type;
- private long mostSiguuid;
- private long leastSiguuid;
- private int handle;
- private int end_group_handle;
- private ArrayList<AnWBT_BLE_Characteristic> characteristic;
-
- public AnWBT_BLE_Service() {
- }
-
- 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;
- }
-
- protected AnWBT_BLE_Service(Parcel in) {
- service_type = in.readInt();
- mostSiguuid = in.readLong();
- leastSiguuid = in.readLong();
- handle = in.readInt();
- end_group_handle = in.readInt();
- ArrayList<AnWBT_BLE_Characteristic> characteristic_new = new ArrayList<AnWBT_BLE_Characteristic>();
- in.readTypedList(characteristic_new,AnWBT_BLE_Characteristic.CREATOR);
- characteristic = characteristic_new;
- }
-
- public int getService_type() {
- return service_type;
- }
-
- public long getMostSiguuid() {
- return mostSiguuid;
- }
-
- public long getLeastSiguuid() {
- return leastSiguuid;
- }
-
- public int getHandle() {
- return handle;
- }
-
- public int getEnd_group_handle() {
- return end_group_handle;
- }
-
- public ArrayList<AnWBT_BLE_Characteristic> getCharacteristic() {
- return characteristic;
- }
-
- public void setService_type(int service_type) {
- this.service_type = service_type;
- }
-
- public void setMostSiguuid(long mostSiguuid) {
- this.mostSiguuid = mostSiguuid;
- }
-
- public void setLeastSiguuid(long leastSiguuid) {
- this.leastSiguuid = leastSiguuid;
- }
-
- public void setHandle(int handle) {
- this.handle = handle;
- }
-
- public void setEnd_group_handle(int end_group_handle) {
- this.end_group_handle = end_group_handle;
- }
-
- public void setCharacteristic(ArrayList<AnWBT_BLE_Characteristic> characteristic) {
- this.characteristic = characteristic;
- }
-
- @Override
- public int describeContents() {
- return 0;
- }
-
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeInt(service_type);
- dest.writeLong(mostSiguuid);
- dest.writeLong(leastSiguuid);
- dest.writeInt(handle);
- dest.writeInt(end_group_handle);
- dest.writeTypedList(characteristic);
- }
-
- public static final Creator<AnWBT_BLE_Service> CREATOR = new Creator<AnWBT_BLE_Service>() {
- @Override
- public AnWBT_BLE_Service createFromParcel(Parcel in) {
- return new AnWBT_BLE_Service(in);
- }
-
- @Override
- public AnWBT_BLE_Service[] newArray(int size) {
- return new AnWBT_BLE_Service[size];
- }
- };
- }
四、JNI层修改前代码
- /*
- * Class : com_anwsdk_service
- * Method :GATT_GetService()
- * Signature : (J)Ljava/util/List;
- */
- JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
- {
- ANW_LOGI("+++ GATT_GetService---");
- int i = 0;
- GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;
- jclass arraylist_cls = env->FindClass("java/util/ArrayList");
- jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");
- jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");
-
- jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- if(gattHandle == NULL){
- return service_arraylist;
- }
- AnW_BLE_Device device;
- GetBLEDeviceManagerByGATTHandle(&device,gattHandle);
- if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){
- return service_arraylist;
- }
- jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");
- jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","(IJJ[B)V");
-
-
- jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");
- jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","(IIIJJ[BLjava/util/ArrayList;)V");
-
- jclass service_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Service");
- jmethodID service_init = env->GetMethodID(service_cls,"<init>","(IJJIILjava/util/ArrayList;)V");
-
-
- for(i = 0;i < device.service_count;i++){
- jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- AnW_BLE_Service *service = &(device.ble_service[i]);
- int j = 0;
- for(j = 0;j < service->characteristic_count;j++){
- jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);
- jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);
- int k = 0;
- for(k = 0;k < characteristic->descriptor_count;k++){
- AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);
- jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);
- jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,descriptor->handle,*(long *)(descriptor->uuid + 8),*(long *)(descriptor->uuid),descriptor_value);
- env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);
- }
- 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);
- env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);
- }
- 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);
- env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);
- }
-
- ANW_LOGI("--- GATT_GetService ---");
- return service_arraylist;
- }
JNI层修改后代码(这里面其实还修改了一个long类型)
- jlong uuid_lsb(u_int8* uuid) {
- jlong lsb = 0;
-
- for (int i = 7; i >= 0; i--) {
- lsb <<= 8;
- lsb |= uuid[i];
- }
-
- return lsb;
- }
-
- jlong uuid_msb(u_int8* uuid) {
- jlong msb = 0;
-
- for (int i = 15; i >= 8; i--) {
- msb <<= 8;
- msb |= uuid[i];
- }
-
- return msb;
- }
- /*
- * Class : com_anwsdk_service
- * Method :GATT_GetService()
- * Signature : (J)Ljava/util/List;
- */
- JNIEXPORT jobject JNICALL Java_com_anwsdk_service_anwbtservice_GATT_GetService(JNIEnv *env, jobject,jlong gatt)
-
- {
- ANW_LOGI("+++ GATT_GetService---(0x%llx)",gatt);
- int i = 0;
- GATT_HANDLE gattHandle = (GATT_HANDLE)gatt;
- jclass arraylist_cls = env->FindClass("java/util/ArrayList");
- jmethodID arraylist_init = env->GetMethodID(arraylist_cls,"<init>","()V");
- jmethodID arraylist_add = env->GetMethodID(arraylist_cls, "add", "(Ljava/lang/Object;)Z");
-
- jobject service_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- if(gattHandle == NULL){
- return service_arraylist;
- }
- AnW_BLE_Device device;
- GetBLEDeviceManagerByGATTHandle(&device,gattHandle);
- if(device.gatt_handle == NULL || device.download_status != BLE_DOWNLOAD_STATUS_COMPLETE){
- return service_arraylist;
- }
- jclass descriptor_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Descriptor");
- jmethodID descriptor_init = env->GetMethodID(descriptor_cls,"<init>","()V");
- jmethodID descriptor_setHandle = env->GetMethodID(descriptor_cls,"setHandle","(I)V");
- jmethodID descriptor_setMostSiguuid = env->GetMethodID(descriptor_cls,"setMostSiguuid","(J)V");
- jmethodID descriptor_setLeastSiguuid = env->GetMethodID(descriptor_cls,"setLeastSiguuid","(J)V");
- jmethodID descriptor_setValue = env->GetMethodID(descriptor_cls,"setValue","([B)V");
-
-
- jclass characteristic_cls = env->FindClass("com/anwsdk/service/AnWBT_BLE_Characteristic");
- jmethodID characteristic_init = env->GetMethodID(characteristic_cls,"<init>","()V");
- jmethodID characteristic_setHandle = env->GetMethodID(characteristic_cls,"setHandle","(I)V");
- jmethodID characteristic_setProperties = env->GetMethodID(characteristic_cls,"setProperties","(I)V");
- jmethodID characteristic_setValue_handle = env->GetMethodID(characteristic_cls,"setValue_handle","(I)V");
- jmethodID characteristic_setMostSiguuid = env->GetMethodID(characteristic_cls,"setMostSiguuid","(J)V");
- jmethodID characteristic_setLeastSiguuid = env->GetMethodID(characteristic_cls,"setLeastSiguuid","(J)V");
- jmethodID characteristic_setValue = env->GetMethodID(characteristic_cls,"setValue","([B)V");
- jmethodID characteristic_setDescriptor = env->GetMethodID(characteristic_cls,"setDescriptor","(Ljava/util/ArrayList;)V");
-
- 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");
- for(i = 0;i < device.service_count;i++){
- jobject characteristic_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- AnW_BLE_Service *service = device.ble_service + i;
- int j = 0;
- for(j = 0;j < service->characteristic_count;j++){
- jobject descriptor_arraylist = env->NewObject(arraylist_cls,arraylist_init,"");
- AnW_BLE_Service_Info_Characteristic *characteristic = &(service->characteristic[j]);
- jbyteArray characteristic_value = charToByteArray(env, (const char*)characteristic->value,characteristic->value_len);
- int k = 0;
- for(k = 0;k < characteristic->descriptor_count;k++){
- AnW_BLE_Service_Info_Characteristic_Descriptor *descriptor = &(characteristic->descriptor[k]);
- jbyteArray descriptor_value = charToByteArray(env, (const char*)descriptor->value,descriptor->value_len);
- jobject descriptor_obj = env->NewObject(descriptor_cls,descriptor_init,"");
- env->CallVoidMethod(descriptor_obj,descriptor_setHandle,descriptor->handle);
- env->CallVoidMethod(descriptor_obj,descriptor_setMostSiguuid,uuid_msb(descriptor->uuid));
- env->CallVoidMethod(descriptor_obj,descriptor_setLeastSiguuid,uuid_lsb(descriptor->uuid));
- env->CallVoidMethod(descriptor_obj,descriptor_setValue,NULL);
- env->CallBooleanMethod(descriptor_arraylist, arraylist_add, descriptor_obj);
- }
- jobject characteristic_obj = env->NewObject(characteristic_cls,characteristic_init,"");
- env->CallVoidMethod(characteristic_obj,characteristic_setHandle,characteristic->handle);
- env->CallVoidMethod(characteristic_obj,characteristic_setProperties,characteristic->properties);
- env->CallVoidMethod(characteristic_obj,characteristic_setValue_handle,characteristic->value_handle);
- env->CallVoidMethod(characteristic_obj,characteristic_setMostSiguuid,uuid_msb(characteristic->uuid));
- env->CallVoidMethod(characteristic_obj,characteristic_setLeastSiguuid,uuid_lsb(characteristic->uuid));
- env->CallVoidMethod(characteristic_obj,characteristic_setValue,NULL);
- env->CallVoidMethod(characteristic_obj,characteristic_setDescriptor,descriptor_arraylist);
- env->CallBooleanMethod(characteristic_arraylist, arraylist_add, characteristic_obj);
- }
- jobject service_obj = env->NewObject(service_cls,service_init,"");
- env->CallVoidMethod(service_obj,service_setService_type,service->service_type);
- env->CallVoidMethod(service_obj,service_setMostSiguuid,uuid_msb(service->uuid));
- env->CallVoidMethod(service_obj,service_setLeastSiguuid,uuid_lsb(service->uuid));
- env->CallVoidMethod(service_obj,service_setHandle,service->handle);
- env->CallVoidMethod(service_obj,service_setEnd_group_handle,service->end_group_handle);
- env->CallVoidMethod(service_obj,service_setCharacteristic,characteristic_arraylist);
- env->CallBooleanMethod(service_arraylist, arraylist_add, service_obj);
- }
-
- ANW_LOGI("--- GATT_GetService ---");
- return service_arraylist;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。