Android BLE scan and show of result
The way I did it was to implement scanCallback() object and overriding the onScanResult() or onBatchScanResults() as needed.
private ScanCallback callback= new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
// handles scan result
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
super.onBatchScanResults(results);
// handles batch scan results
for (ScanResult result : results) {
// you can iterate through each result like so
}
}
@Override
public void onScanFailed(int errorCode) {
super.onScanFailed(errorCode);
// handles error
}
};
You then pass callback
inside of stopScan()
and startScan()
bluetoothLeScanner.startScan(callback);
bluetoothLeScanner.stopScan(callback);
Also consider using result.getDevice()
so you can retrieve the specific data of the device you need instead of a large chunk of information. For example, result.getDevice().getAddress()
if you want the address only.