@Test public void testScanResultNotHavingScanRecordReturnNullReferenceOnParseAttempt() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { final ScanResult scanResult = new ScanResult( getBluetoothDevice("00:11:22:33:FF:EE"), null, -95, 769642079079204L ); final Beacon beacon = parser.parse(scanResult); assertNull(beacon); }
@Override @Nullable public Beacon parse(@NonNull ScanResult scanResult) { if (scanResult.getScanRecord() == null) { return null; } final SparseArray<byte[]> manufacturerData = scanResult .getScanRecord() .getManufacturerSpecificData(); for (int i = 0; i < manufacturerData.size(); i++) { final int key = manufacturerData.keyAt(i); final byte[] rawBytes = manufacturerData.get(key); final List<Byte> bytesList = Conversions.asList(rawBytes); try { final List<Object> identifiers = new ArrayList<>(); for (int j = 0; j < fieldConverters.size(); j++) { final IFieldConverter<?> converter = fieldConverters.get(j); final Object parsedField = converter.consume(bytesList); identifiers.add(parsedField); if (j == beaconTypeIndex && !beaconType.equals(parsedField)) { BeaconLogger.e( format( Locale.US, "Expected beacon type is %s, while the actual beacon type is %s;" + " the data %s will be skipped", beaconType, parsedField, Conversions.byteArrayToHexString(rawBytes) ) ); return null; } } final Byte txPower = txPowerIndex != -1 ? ((byte) identifiers.get(txPowerIndex)) : null; return new Beacon( scanResult.getDevice().getAddress(), identifiers, txPower ); } catch (Exception e) { BeaconLogger.e( "An attempt to parse the following manufacturerData: " + byteArrayToHexString(rawBytes) + " have resulted in the following " + "exception: " + e.toString() ); } } return null; }
BeaconParser implements IBeaconParser { @Override @Nullable public Beacon parse(@NonNull ScanResult scanResult) { if (scanResult.getScanRecord() == null) { return null; } final SparseArray<byte[]> manufacturerData = scanResult .getScanRecord() .getManufacturerSpecificData(); for (int i = 0; i < manufacturerData.size(); i++) { final int key = manufacturerData.keyAt(i); final byte[] rawBytes = manufacturerData.get(key); final List<Byte> bytesList = Conversions.asList(rawBytes); try { final List<Object> identifiers = new ArrayList<>(); for (int j = 0; j < fieldConverters.size(); j++) { final IFieldConverter<?> converter = fieldConverters.get(j); final Object parsedField = converter.consume(bytesList); identifiers.add(parsedField); if (j == beaconTypeIndex && !beaconType.equals(parsedField)) { BeaconLogger.e( format( Locale.US, "Expected beacon type is %s, while the actual beacon type is %s;" + " the data %s will be skipped", beaconType, parsedField, Conversions.byteArrayToHexString(rawBytes) ) ); return null; } } final Byte txPower = txPowerIndex != -1 ? ((byte) identifiers.get(txPowerIndex)) : null; return new Beacon( scanResult.getDevice().getAddress(), identifiers, txPower ); } catch (Exception e) { BeaconLogger.e( "An attempt to parse the following manufacturerData: " + byteArrayToHexString(rawBytes) + " have resulted in the following " + "exception: " + e.toString() ); } } return null; } }
BeaconParser implements IBeaconParser { @Override @Nullable public Beacon parse(@NonNull ScanResult scanResult) { if (scanResult.getScanRecord() == null) { return null; } final SparseArray<byte[]> manufacturerData = scanResult .getScanRecord() .getManufacturerSpecificData(); for (int i = 0; i < manufacturerData.size(); i++) { final int key = manufacturerData.keyAt(i); final byte[] rawBytes = manufacturerData.get(key); final List<Byte> bytesList = Conversions.asList(rawBytes); try { final List<Object> identifiers = new ArrayList<>(); for (int j = 0; j < fieldConverters.size(); j++) { final IFieldConverter<?> converter = fieldConverters.get(j); final Object parsedField = converter.consume(bytesList); identifiers.add(parsedField); if (j == beaconTypeIndex && !beaconType.equals(parsedField)) { BeaconLogger.e( format( Locale.US, "Expected beacon type is %s, while the actual beacon type is %s;" + " the data %s will be skipped", beaconType, parsedField, Conversions.byteArrayToHexString(rawBytes) ) ); return null; } } final Byte txPower = txPowerIndex != -1 ? ((byte) identifiers.get(txPowerIndex)) : null; return new Beacon( scanResult.getDevice().getAddress(), identifiers, txPower ); } catch (Exception e) { BeaconLogger.e( "An attempt to parse the following manufacturerData: " + byteArrayToHexString(rawBytes) + " have resulted in the following " + "exception: " + e.toString() ); } } return null; } BeaconParser(@NonNull List<IFieldConverter<?>> fieldConverters,
                 int beaconTypeIndex,
                 int manufacturerId,
                 Object beaconType,
                 int txPowerIndex); }
BeaconParser implements IBeaconParser { @Override @Nullable public Beacon parse(@NonNull ScanResult scanResult) { if (scanResult.getScanRecord() == null) { return null; } final SparseArray<byte[]> manufacturerData = scanResult .getScanRecord() .getManufacturerSpecificData(); for (int i = 0; i < manufacturerData.size(); i++) { final int key = manufacturerData.keyAt(i); final byte[] rawBytes = manufacturerData.get(key); final List<Byte> bytesList = Conversions.asList(rawBytes); try { final List<Object> identifiers = new ArrayList<>(); for (int j = 0; j < fieldConverters.size(); j++) { final IFieldConverter<?> converter = fieldConverters.get(j); final Object parsedField = converter.consume(bytesList); identifiers.add(parsedField); if (j == beaconTypeIndex && !beaconType.equals(parsedField)) { BeaconLogger.e( format( Locale.US, "Expected beacon type is %s, while the actual beacon type is %s;" + " the data %s will be skipped", beaconType, parsedField, Conversions.byteArrayToHexString(rawBytes) ) ); return null; } } final Byte txPower = txPowerIndex != -1 ? ((byte) identifiers.get(txPowerIndex)) : null; return new Beacon( scanResult.getDevice().getAddress(), identifiers, txPower ); } catch (Exception e) { BeaconLogger.e( "An attempt to parse the following manufacturerData: " + byteArrayToHexString(rawBytes) + " have resulted in the following " + "exception: " + e.toString() ); } } return null; } BeaconParser(@NonNull List<IFieldConverter<?>> fieldConverters,
                 int beaconTypeIndex,
                 int manufacturerId,
                 Object beaconType,
                 int txPowerIndex); @Override @Nullable Beacon parse(@NonNull ScanResult scanResult); @NonNull @Override List<ScanFilter> asScanFilters(@NonNull List<Region> regions); }
BeaconParser implements IBeaconParser { @Override @Nullable public Beacon parse(@NonNull ScanResult scanResult) { if (scanResult.getScanRecord() == null) { return null; } final SparseArray<byte[]> manufacturerData = scanResult .getScanRecord() .getManufacturerSpecificData(); for (int i = 0; i < manufacturerData.size(); i++) { final int key = manufacturerData.keyAt(i); final byte[] rawBytes = manufacturerData.get(key); final List<Byte> bytesList = Conversions.asList(rawBytes); try { final List<Object> identifiers = new ArrayList<>(); for (int j = 0; j < fieldConverters.size(); j++) { final IFieldConverter<?> converter = fieldConverters.get(j); final Object parsedField = converter.consume(bytesList); identifiers.add(parsedField); if (j == beaconTypeIndex && !beaconType.equals(parsedField)) { BeaconLogger.e( format( Locale.US, "Expected beacon type is %s, while the actual beacon type is %s;" + " the data %s will be skipped", beaconType, parsedField, Conversions.byteArrayToHexString(rawBytes) ) ); return null; } } final Byte txPower = txPowerIndex != -1 ? ((byte) identifiers.get(txPowerIndex)) : null; return new Beacon( scanResult.getDevice().getAddress(), identifiers, txPower ); } catch (Exception e) { BeaconLogger.e( "An attempt to parse the following manufacturerData: " + byteArrayToHexString(rawBytes) + " have resulted in the following " + "exception: " + e.toString() ); } } return null; } BeaconParser(@NonNull List<IFieldConverter<?>> fieldConverters,
                 int beaconTypeIndex,
                 int manufacturerId,
                 Object beaconType,
                 int txPowerIndex); @Override @Nullable Beacon parse(@NonNull ScanResult scanResult); @NonNull @Override List<ScanFilter> asScanFilters(@NonNull List<Region> regions);  }