Skip to content

Commit

Permalink
Allow discovery RSSI threshold to be configured manually
Browse files Browse the repository at this point in the history
  • Loading branch information
weliem committed Dec 30, 2021
1 parent 8912942 commit 486d4b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public class BluetoothCentralManager {
private final Set<String> scanOptions;

private static final int ADDRESS_LENGTH = 17;
static final short DISCOVERY_RSSI_THRESHOLD = -80;
private short discoveryRssiThreshold = -80;

// Scan in intervals. Make sure it is less than 10seconds to avoid issues with Bluez internal scanning
private static final long SCAN_WINDOW = TimeUnit.SECONDS.toMillis(6);
Expand Down Expand Up @@ -373,6 +373,17 @@ public void stopScan() {
stopScanning();
}

public void setRssiThreshold(final short threshold) {
if (threshold > 20 || threshold < -200) {
throw new IllegalArgumentException("RSSI threshold value outside range (-200 to +20");
}
this.discoveryRssiThreshold = threshold;
}

public short getRssiThreshold() {
return this.discoveryRssiThreshold;
}

private void resetScanFilters() {
scanPeripheralNames = new HashSet<>();
scanPeripheralAddresses = new HashSet<>();
Expand All @@ -383,7 +394,7 @@ private void resetScanFilters() {

private void setBasicFilters() {
scanFilters.put(DiscoveryFilter.Transport, DiscoveryTransport.LE);
scanFilters.put(DiscoveryFilter.RSSI, DISCOVERY_RSSI_THRESHOLD);
scanFilters.put(DiscoveryFilter.RSSI, discoveryRssiThreshold);
scanFilters.put(DiscoveryFilter.DuplicateData, true);
}

Expand Down Expand Up @@ -497,7 +508,7 @@ void handleInterfaceAddedForDevice(@NotNull final String path, @NotNull final Ma
if ((value.get(PROPERTY_RSSI) != null) && (value.get(PROPERTY_RSSI).getValue() instanceof Short)) {
rssi = (Short) value.get(PROPERTY_RSSI).getValue();
} else {
rssi = DISCOVERY_RSSI_THRESHOLD;
rssi = discoveryRssiThreshold;
}

// Convert the service UUIDs
Expand Down Expand Up @@ -610,7 +621,7 @@ private ScanResult getScanResultFromDevice(@NotNull final BluezDevice bluezDevic
final String deviceAddress = bluezDevice.getAddress();
final List<@NotNull UUID> uuids = bluezDevice.getUuids();
final Short rssi = bluezDevice.getRssi();
final int rssiInt = rssi == null ? DISCOVERY_RSSI_THRESHOLD : rssi;
final int rssiInt = rssi == null ? discoveryRssiThreshold : rssi;
final Map<@NotNull Integer, byte[]> manufacturerData = bluezDevice.getManufacturerData();
final Map<@NotNull String, byte[]> serviceData = bluezDevice.getServiceData();
return new ScanResult(deviceName, deviceAddress, uuids, rssiInt, manufacturerData, serviceData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void checkFilters(Map<String, Variant<?>> filterMap) {
assertTrue(duplicateData);

Short rssi = (Short) filterMap.get("RSSI").getValue();
assertEquals(DISCOVERY_RSSI_THRESHOLD, (int) rssi);
assertNotNull(rssi);
}

@Test
Expand Down

0 comments on commit 486d4b9

Please sign in to comment.