From 5c428803c5d21718b65a647e9fb8f34ae1b14f69 Mon Sep 17 00:00:00 2001 From: a1anwang <245121415@qq.com> Date: Tue, 5 Nov 2019 17:07:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E7=AC=A6=E5=90=88BL?= =?UTF-8?q?E=E5=8D=8F=E8=AE=AE=E7=9A=84=E5=B9=BF=E6=92=AD=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E5=A5=94=E6=BA=83=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复当扫描到不符合BLE协议的广播时会奔溃的问题,虽然这种情况极少极少,但是我还是遇到了 --- okble-lib/build.gradle | 4 +- .../okble/client/scan/BLEScanResult.java | 267 +++++++++--------- 2 files changed, 139 insertions(+), 132 deletions(-) diff --git a/okble-lib/build.gradle b/okble-lib/build.gradle index 13ac262..3fbdd79 100644 --- a/okble-lib/build.gradle +++ b/okble-lib/build.gradle @@ -8,8 +8,8 @@ android { defaultConfig { minSdkVersion 18 targetSdkVersion 26 - versionCode 6 - versionName "1.1.2" + versionCode 7 + versionName "1.1.3" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/okble-lib/src/main/java/com/a1anwang/okble/client/scan/BLEScanResult.java b/okble-lib/src/main/java/com/a1anwang/okble/client/scan/BLEScanResult.java index 9a431ed..d8b0a37 100644 --- a/okble-lib/src/main/java/com/a1anwang/okble/client/scan/BLEScanResult.java +++ b/okble-lib/src/main/java/com/a1anwang/okble/client/scan/BLEScanResult.java @@ -3,11 +3,13 @@ import android.bluetooth.BluetoothDevice; import android.os.Parcel; import android.os.Parcelable; +import android.util.Log; import android.util.SparseArray; import com.a1anwang.okble.common.OKBLEDataUtils; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -141,156 +143,161 @@ public Map getServiceData() { synchronized private void analyzeAdvertisingData() { if (advertisingData != null && advertisingData.length > 0) { - int index = 0; - while (index < advertisingData.length - 1) { - int length = advertisingData[index]; - if (length == 0) { - break; - } - int type = advertisingData[index + 1]; - //LogUtils.e(TAG, " type:" + OKBLEDataUtils.Bytes2HexString(new byte[]{(byte) type})); - if (type == (byte) DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL) { - if(serviceUuids==null){ - serviceUuids = new ArrayList<>(); + try { + int index = 0; + while (index < advertisingData.length - 1) { + int length = advertisingData[index]; + if (length == 0) { + break; } + int type = advertisingData[index + 1]; + //LogUtils.e(TAG, " type:" + OKBLEDataUtils.Bytes2HexString(new byte[]{(byte) type})); + if (type == (byte) DATA_TYPE_SERVICE_UUIDS_16_BIT_PARTIAL) { + if(serviceUuids==null){ + serviceUuids = new ArrayList<>(); + } - byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); + byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); - if (serveruuids.length % 2 == 0) { + if (serveruuids.length % 2 == 0) { - int count = serveruuids.length / 2; - for (int i = 0; i < count; i++) { - byte[] serveruuid = new byte[]{serveruuids[2 * i + 1], serveruuids[2 * i]}; - //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); - String uuid=OKBLEDataUtils.BytesToHexString(serveruuid); - serviceUuids.add(uuid); - // LogUtils.e(TAG, " serveruuid:" + uuid); + int count = serveruuids.length / 2; + for (int i = 0; i < count; i++) { + byte[] serveruuid = new byte[]{serveruuids[2 * i + 1], serveruuids[2 * i]}; + //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); + String uuid=OKBLEDataUtils.BytesToHexString(serveruuid); + serviceUuids.add(uuid); + // LogUtils.e(TAG, " serveruuid:" + uuid); + } } - } - }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE){ - if(serviceUuids==null){ - serviceUuids = new ArrayList<>(); - } - byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); - - if (serveruuids.length % 2 == 0) { - - int count = serveruuids.length / 2; - for (int i = 0; i < count; i++) { - byte[] serveruuid = new byte[]{serveruuids[2 * i + 1], serveruuids[2 * i]}; - //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); - String uuid=OKBLEDataUtils.BytesToHexString(serveruuid); - serviceUuids.add(uuid); - // LogUtils.e(TAG, " serveruuid:" + uuid); + }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_16_BIT_COMPLETE){ + if(serviceUuids==null){ + serviceUuids = new ArrayList<>(); } + byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); + + if (serveruuids.length % 2 == 0) { + + int count = serveruuids.length / 2; + for (int i = 0; i < count; i++) { + byte[] serveruuid = new byte[]{serveruuids[2 * i + 1], serveruuids[2 * i]}; + //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); + String uuid=OKBLEDataUtils.BytesToHexString(serveruuid); + serviceUuids.add(uuid); + // LogUtils.e(TAG, " serveruuid:" + uuid); + } - } - }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL){ - if(serviceUuids==null){ - serviceUuids = new ArrayList<>(); - } - byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); - - if (serveruuids.length % 16 == 0) { + } + }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_128_BIT_PARTIAL){ + if(serviceUuids==null){ + serviceUuids = new ArrayList<>(); + } + byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); + + if (serveruuids.length % 16 == 0) { + + int count = serveruuids.length / 16; + for (int i = 0; i < count; i++) { + + byte[] serveruuid = new byte[16]; + for (int j=0;j<16;j++){ + serveruuid[j]=serveruuids[16 * i + (15-j)]; + } + String hexStr=OKBLEDataUtils.BytesToHexString(serveruuid); + String uuid=hexStr.substring(0, 8); + uuid+="-"; + uuid+=hexStr.substring(8, 12); + uuid+="-"; + uuid+=hexStr.substring(12, 16); + uuid+="-"; + uuid+=hexStr.substring(16, 20); + uuid+="-"; + uuid+=hexStr.substring(20, 32); + //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); + + serviceUuids.add(uuid); + // LogUtils.e(TAG, " serveruuid:" + uuid); + } - int count = serveruuids.length / 16; - for (int i = 0; i < count; i++) { - byte[] serveruuid = new byte[16]; - for (int j=0;j<16;j++){ - serveruuid[j]=serveruuids[16 * i + (15-j)]; - } - String hexStr=OKBLEDataUtils.BytesToHexString(serveruuid); - String uuid=hexStr.substring(0, 8); - uuid+="-"; - uuid+=hexStr.substring(8, 12); - uuid+="-"; - uuid+=hexStr.substring(12, 16); - uuid+="-"; - uuid+=hexStr.substring(16, 20); - uuid+="-"; - uuid+=hexStr.substring(20, 32); - //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); - - serviceUuids.add(uuid); - // LogUtils.e(TAG, " serveruuid:" + uuid); } + }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE){ + if(serviceUuids==null){ + serviceUuids = new ArrayList<>(); + } + byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); + + if (serveruuids.length % 16 == 0) { + + int count = serveruuids.length / 16; + for (int i = 0; i < count; i++) { + + byte[] serveruuid = new byte[16]; + for (int j=0;j<16;j++){ + serveruuid[j]=serveruuids[16 * i + (15-j)]; + } + String hexStr=OKBLEDataUtils.BytesToHexString(serveruuid); + String uuid=hexStr.substring(0, 8); + uuid+="-"; + uuid+=hexStr.substring(8, 12); + uuid+="-"; + uuid+=hexStr.substring(12, 16); + uuid+="-"; + uuid+=hexStr.substring(16, 20); + uuid+="-"; + uuid+=hexStr.substring(20, 32); + //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); + + serviceUuids.add(uuid); + // LogUtils.e(TAG, " serveruuid:" + uuid); + } + } + }else if (type == (byte) DATA_TYPE_SERVICE_DATA) { + serviceData = new HashMap<>(); + byte[] serverData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + byte[] uuid = new byte[]{serverData[1], serverData[0]}; + byte[] data = OKBLEDataUtils.subByteArray(serverData, 2, serverData.length - 2); + // String uuidStr = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(uuid).toLowerCase()); + String uuidStr=OKBLEDataUtils.BytesToHexString(uuid); + serviceData.put(uuidStr, data); + + // LogUtils.e(TAG, " server data :" + OKBLEDataUtils.Bytes2HexString(data) + " uuid:" + uuidStr); + + } else if (type == (byte) DATA_TYPE_MANUFACTURER_SPECIFIC_DATA) { + manufacturerSpecificData = new SparseArray<>(); + byte[] manufacturerData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + byte[] manufacturerId = new byte[]{manufacturerData[1], manufacturerData[0]}; + // LogUtils.e(TAG, " manufacturerId:" + OKBLEDataUtils.Bytes2HexString(manufacturerId)); + byte[] manufacturerValue = OKBLEDataUtils.subByteArray(manufacturerData, 2, manufacturerData.length - 2); + manufacturerSpecificData.append(OKBLEDataUtils.buildUint16(manufacturerId[0], manufacturerId[1]), manufacturerValue); + // LogUtils.e(TAG, " manufacturerValue:" + OKBLEDataUtils.Bytes2HexString(manufacturerValue)); + + + } else if (type == (byte) DATA_TYPE_LOCAL_NAME_COMPLETE) { + + byte[] nameData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); + completeLocalName = new String(nameData); + // LogUtils.e(TAG, " completeLocalName:" + completeLocalName); + }else if (type == (byte) DATA_TYPE_TX_POWER_LEVEL) { + + txPowerLevel = advertisingData[index + 2]; } - }else if(type == (byte) DATA_TYPE_SERVICE_UUIDS_128_BIT_COMPLETE){ - if(serviceUuids==null){ - serviceUuids = new ArrayList<>(); - } - byte[] serveruuids = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - // LogUtils.e(TAG, " serveruuids:" + OKBLEDataUtils.Bytes2HexString(serveruuids)); - - if (serveruuids.length % 16 == 0) { - int count = serveruuids.length / 16; - for (int i = 0; i < count; i++) { - - byte[] serveruuid = new byte[16]; - for (int j=0;j<16;j++){ - serveruuid[j]=serveruuids[16 * i + (15-j)]; - } - String hexStr=OKBLEDataUtils.BytesToHexString(serveruuid); - String uuid=hexStr.substring(0, 8); - uuid+="-"; - uuid+=hexStr.substring(8, 12); - uuid+="-"; - uuid+=hexStr.substring(12, 16); - uuid+="-"; - uuid+=hexStr.substring(16, 20); - uuid+="-"; - uuid+=hexStr.substring(20, 32); - //String uuid = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(serveruuid).toLowerCase()); - - serviceUuids.add(uuid); - // LogUtils.e(TAG, " serveruuid:" + uuid); - } + index += length + 1;//1是length自身占一个字节 - } - }else if (type == (byte) DATA_TYPE_SERVICE_DATA) { - serviceData = new HashMap<>(); - byte[] serverData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - byte[] uuid = new byte[]{serverData[1], serverData[0]}; - byte[] data = OKBLEDataUtils.subByteArray(serverData, 2, serverData.length - 2); - // String uuidStr = CommonUUIDUtils.CommonUUIDStr_x.replace("xxxx", OKBLEDataUtils.BytesToHexString(uuid).toLowerCase()); - String uuidStr=OKBLEDataUtils.BytesToHexString(uuid); - serviceData.put(uuidStr, data); - - // LogUtils.e(TAG, " server data :" + OKBLEDataUtils.Bytes2HexString(data) + " uuid:" + uuidStr); - - } else if (type == (byte) DATA_TYPE_MANUFACTURER_SPECIFIC_DATA) { - manufacturerSpecificData = new SparseArray<>(); - byte[] manufacturerData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - byte[] manufacturerId = new byte[]{manufacturerData[1], manufacturerData[0]}; - // LogUtils.e(TAG, " manufacturerId:" + OKBLEDataUtils.Bytes2HexString(manufacturerId)); - byte[] manufacturerValue = OKBLEDataUtils.subByteArray(manufacturerData, 2, manufacturerData.length - 2); - manufacturerSpecificData.append(OKBLEDataUtils.buildUint16(manufacturerId[0], manufacturerId[1]), manufacturerValue); - // LogUtils.e(TAG, " manufacturerValue:" + OKBLEDataUtils.Bytes2HexString(manufacturerValue)); - - - } else if (type == (byte) DATA_TYPE_LOCAL_NAME_COMPLETE) { - - byte[] nameData = OKBLEDataUtils.subByteArray(advertisingData, index + 2, length - 1); - completeLocalName = new String(nameData); - // LogUtils.e(TAG, " completeLocalName:" + completeLocalName); - }else if (type == (byte) DATA_TYPE_TX_POWER_LEVEL) { - - txPowerLevel = advertisingData[index + 2]; } - - index += length + 1;//1是length自身占一个字节 - + }catch (Exception e){ + Log.e(TAG, "unable to parse scan record: " + Arrays.toString(advertisingData)); }