Skip to content

Commit

Permalink
Add the option to let users define scan filters and scan settings
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosTze committed Nov 2, 2022
1 parent 7f8108a commit d294125
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
Expand Down Expand Up @@ -334,6 +336,49 @@ public void run() {
});
}

/**
* This method is used to scan BLE devices.
*
* @param filters The scan filters that will be used
* @param bleScannerListener BleScanListener for scanning callbacks.
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION})
public void searchBleEspDevices(List<ScanFilter> filters, BleScanListener bleScannerListener) {

Log.e(TAG, "Search for BLE devices");
bleScanner = new BleScanner(context, bleScannerListener);
bleScanner.startScan(filters);
}

/**
* This method is used to scan BLE devices.
*
* @param scanSettings The scan settings that will be used
* @param bleScannerListener BleScanListener for scanning callbacks.
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION})
public void searchBleEspDevices(ScanSettings scanSettings, BleScanListener bleScannerListener) {

Log.e(TAG, "Search for BLE devices");
bleScanner = new BleScanner(context, bleScannerListener);
bleScanner.startScan(scanSettings);
}

/**
* This method is used to scan BLE devices.
*
* @param filters The scan filters that will be used
* @param scanSettings The scan settings that will be used
* @param bleScannerListener BleScanListener for scanning callbacks.
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH_ADMIN, Manifest.permission.BLUETOOTH, Manifest.permission.ACCESS_FINE_LOCATION})
public void searchBleEspDevices(List<ScanFilter> filters, ScanSettings scanSettings, BleScanListener bleScannerListener) {

Log.e(TAG, "Search for BLE devices");
bleScanner = new BleScanner(context, bleScannerListener);
bleScanner.startScan(filters, scanSettings);
}

/**
* This method is used to scan BLE devices.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,45 @@ public BleScanner(Context context, String prefix, BleScanListener bleScannerList
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN})
public void startScan() {
List<ScanFilter> filters = new ArrayList<>();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.build();
startScan(filters, settings);
}

/**
* This method is used to start BLE scan.
*
* @param filters The scan filters that will be used
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN})
public void startScan(List<ScanFilter> filters) {
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.build();
startScan(filters, settings);
}

/**
* This method is used to start BLE scan.
*
* @param scanSettings The scan settings that will be used
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN})
public void startScan(ScanSettings scanSettings) {
List<ScanFilter> filters = new ArrayList<>();
startScan(filters, scanSettings);
}

/**
* This method is used to start BLE scan.
*
* @param filters The scan filters that will be used
* @param scanSettings The scan settings that will be used
*/
@RequiresPermission(allOf = {Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN})
public void startScan(List<ScanFilter> filters, ScanSettings scanSettings) {

if (!bluetoothAdapter.isEnabled()) {
bleScanListener.scanStartFailed();
Expand All @@ -79,13 +118,8 @@ public void startScan() {
Log.d(TAG, "Starting BLE device scanning...");

bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
List<ScanFilter> filters = new ArrayList<>();
ScanSettings settings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_BALANCED)
.build();

isScanning = true;
bluetoothLeScanner.startScan(filters, settings, scanCallback);
bluetoothLeScanner.startScan(filters, scanSettings, scanCallback);
handler.postDelayed(stopScanTask, SCAN_TIME_OUT);
}

Expand Down

0 comments on commit d294125

Please sign in to comment.