From 4d040ceaf03499711cafb339a19e221f61504f91 Mon Sep 17 00:00:00 2001 From: Bosko Popovic Date: Mon, 31 Jan 2022 03:30:58 +0100 Subject: [PATCH] Possible crash fix caused by wrong raw data --- CHANGELOG.md | 1 + .../boskokg/flutter_blue_plus/ProtoMaker.java | 20 +++++++++++-------- ios/Classes/FlutterBluePlusPlugin.m | 14 ++++++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17017b63..78d1e537 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 1.1.0 +* Possible crash fix caused by wrong raw data (fixed by narrit) * Ios : try reconnect on unexpected disconnection (fixed by EB-Plum) * Android: Add missing break in switch, which causes exceptions (fixed by russelltg) * Android: Enforcing maxSdkVersion on the ACCESS_FINE_LOCATION permission will create issues for Android 12 devices that use location for purposes other than Bluetooth (such as using packages that actually need location). (fixed by rickcasson) diff --git a/android/src/main/java/com/boskokg/flutter_blue_plus/ProtoMaker.java b/android/src/main/java/com/boskokg/flutter_blue_plus/ProtoMaker.java index 66e37cd1..84a53e30 100644 --- a/android/src/main/java/com/boskokg/flutter_blue_plus/ProtoMaker.java +++ b/android/src/main/java/com/boskokg/flutter_blue_plus/ProtoMaker.java @@ -68,17 +68,21 @@ static Protos.ScanResult from(BluetoothDevice device, ScanResult scanResult) { } // Manufacturer Specific Data SparseArray msd = scanRecord.getManufacturerSpecificData(); - for (int i = 0; i < msd.size(); i++) { - int key = msd.keyAt(i); - byte[] value = msd.valueAt(i); - a.putManufacturerData(key, ByteString.copyFrom(value)); + if(msd != null) { + for (int i = 0; i < msd.size(); i++) { + int key = msd.keyAt(i); + byte[] value = msd.valueAt(i); + a.putManufacturerData(key, ByteString.copyFrom(value)); + } } // Service Data Map serviceData = scanRecord.getServiceData(); - for (Map.Entry entry : serviceData.entrySet()) { - ParcelUuid key = entry.getKey(); - byte[] value = entry.getValue(); - a.putServiceData(key.getUuid().toString(), ByteString.copyFrom(value)); + if(serviceData != null) { + for (Map.Entry entry : serviceData.entrySet()) { + ParcelUuid key = entry.getKey(); + byte[] value = entry.getValue(); + a.putServiceData(key.getUuid().toString(), ByteString.copyFrom(value)); + } } // Service UUIDs List serviceUuids = scanRecord.getServiceUuids(); diff --git a/ios/Classes/FlutterBluePlusPlugin.m b/ios/Classes/FlutterBluePlusPlugin.m index ca00f1d3..532254f4 100644 --- a/ios/Classes/FlutterBluePlusPlugin.m +++ b/ios/Classes/FlutterBluePlusPlugin.m @@ -604,20 +604,24 @@ - (ProtosScanResult*)toScanResultProto:(CBPeripheral *)peripheral advertisementD } // Manufacturer Specific Data NSData *manufData = advertisementData[CBAdvertisementDataManufacturerDataKey]; - if(manufData.length > 2) { + if(manufData != nil && manufData.length > 2) { unsigned short manufacturerId; [manufData getBytes:&manufacturerId length:2]; [[ads manufacturerData] setObject:[manufData subdataWithRange:NSMakeRange(2, manufData.length - 2)] forKey:manufacturerId]; } // Service Data NSDictionary *serviceData = advertisementData[CBAdvertisementDataServiceDataKey]; - for (CBUUID *uuid in serviceData) { - [[ads serviceData] setObject:serviceData[uuid] forKey:uuid.UUIDString]; + if(serviceData != nil) { + for (CBUUID *uuid in serviceData) { + [[ads serviceData] setObject:serviceData[uuid] forKey:uuid.UUIDString]; + } } // Service Uuids NSArray *serviceUuids = advertisementData[CBAdvertisementDataServiceUUIDsKey]; - for (CBUUID *uuid in serviceUuids) { - [[ads serviceUuidsArray] addObject:uuid.UUIDString]; + if(serviceUuids != nil) { + for (CBUUID *uuid in serviceUuids) { + [[ads serviceUuidsArray] addObject:uuid.UUIDString]; + } } [result setAdvertisementData:ads]; return result;