Skip to content

Commit

Permalink
fix scan on older android
Browse files Browse the repository at this point in the history
  • Loading branch information
alerighi committed Jan 10, 2025
1 parent 27990ef commit 2f079d8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,43 @@ public void cancel(String transactionId, Promise promise) {
public void scanStart(ReadableArray filterUuid, Promise promise) {
Log.d(NAME, String.format("scanStart(%s)", filterUuid));


if (adapter == null) {
promise.reject(BleError.ERROR_NOT_INITIALIZED.name(), "module is not initialized");
} else {
Log.i(NAME, "starting scan");
boolean isFilteringSupported = adapter.isOffloadedFilteringSupported();
boolean isBatchingSupported = adapter.isOffloadedScanBatchingSupported();

Log.i(NAME, String.format("starting scan filter supported %b batching supported %b", isFilteringSupported, isBatchingSupported));

List<ScanFilter> filters = null;
ScanSettings.Builder settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE);

if (filterUuid != null && filterUuid.size() > 0) {
List<ParcelUuid> uuidFilter = null;
if (isFilteringSupported && filterUuid != null && filterUuid.size() > 0) {
settings.setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH | ScanSettings.CALLBACK_TYPE_MATCH_LOST);
filters = new ArrayList<>();
uuidFilter = new ArrayList<>();
for (int i = 0; i < filterUuid.size(); i++) {
String serviceUuid = filterUuid.getString(i);
Log.d(NAME, "adding filter UUID: " + serviceUuid);
ParcelUuid uuid = ParcelUuid.fromString(serviceUuid);
uuidFilter.add(uuid);
filters.add(new ScanFilter.Builder().setServiceUuid(uuid).build());
}
} else {
// avoid flooding JS with events
settings.setReportDelay(SCAN_REPORT_DELAY_MS);
if (isBatchingSupported) {
settings.setReportDelay(SCAN_REPORT_DELAY_MS);
}
settings.setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES);
}

if (scanner == null) {
scanner = adapter.getBluetoothLeScanner();
scanCallback = new BleScanCallback(emitter);
scanCallback = new BleScanCallback(emitter, uuidFilter);
} else {
// stopping existing scan to restart it
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package it.iotinga.blelibrary;

import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanRecord;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.os.ParcelUuid;
import android.util.Log;

import androidx.annotation.RequiresPermission;
Expand All @@ -12,24 +14,49 @@
public class BleScanCallback extends ScanCallback {
private static final String TAG = "BleScanCallback";
private final EventEmitter eventEmitter;
private final List<ParcelUuid> filter;

BleScanCallback(EventEmitter eventEmitter) {
BleScanCallback(EventEmitter eventEmitter, List<ParcelUuid> filter) {
super();
this.eventEmitter = eventEmitter;
this.filter = filter;
}

private boolean resultPassesUuidFilter(ScanResult result) {
if (filter == null || filter.isEmpty()) {
return true;
}

ScanRecord record = result.getScanRecord();
if (record == null) {
return false;
}

for (ParcelUuid uuid : record.getServiceUuids()) {
for (ParcelUuid allowedUuid : filter) {
if (uuid.equals(allowedUuid)) {
return true;
}
}
}

return false;
}

@Override
@RequiresPermission(value = "android.permission.BLUETOOTH_CONNECT")
public void onScanResult(int callbackType, ScanResult result) {
Log.i(TAG, String.format("got scan result: %s (callback type: %d)", result, callbackType));

boolean available = callbackType == ScanSettings.CALLBACK_TYPE_FIRST_MATCH
|| callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES;
if (resultPassesUuidFilter(result)) {
boolean available = callbackType == ScanSettings.CALLBACK_TYPE_FIRST_MATCH
|| callbackType == ScanSettings.CALLBACK_TYPE_ALL_MATCHES;

RNEventScanResult event = new RNEventScanResult();
event.add(result, available);
RNEventScanResult event = new RNEventScanResult();
event.add(result, available);

eventEmitter.emit(event);
eventEmitter.emit(event);
}
}

@Override
Expand All @@ -46,7 +73,9 @@ public void onBatchScanResults(List<ScanResult> results) {

RNEventScanResult event = new RNEventScanResult();
for (ScanResult result : results) {
event.add(result, true);
if (resultPassesUuidFilter(result)) {
event.add(result, true);
}
}

eventEmitter.emit(event);
Expand Down

0 comments on commit 2f079d8

Please sign in to comment.