Extensible scan sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void onStartScanClicked(@NonNull View view) { | |
final Func1<ScanResult, Pair<ScanResult, HBData>> mapToData = scanResult -> | |
new Pair<>(scanResult, | |
new HBData(scanResult.getScanRecord())); | |
final Func1<Pair<ScanResult, HBData>, Boolean> filter = result -> { | |
switch (mFiler) { | |
case GROW: | |
return result.second.isScale(); | |
case REST: | |
return result.second.isNightlight(); | |
case RECOVERY: | |
return result.second.isRecoveryMode(); | |
default: | |
return result.second.isHBBleDevice() || result.second.isRecoveryMode(); | |
} | |
}; | |
final Func1<Pair<ScanResult, HBData>, HBBleDevice> mapToDevice = result -> { | |
if (result.second.isScale()) { | |
return new Scale(result.first.getBleDevice()); | |
} else if (result.second.isNightlight()) { | |
return new Nightlight(result.first.getBleDevice()); | |
} else { | |
return null; | |
} | |
}; | |
HBBleManager.getInstance() | |
.startScanBy(mapToData, filter, mapToDevice) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.compose(bindUntilEvent(FragmentEvent.PAUSE)) | |
.takeUntil( | |
Observable.interval(10, TimeUnit.SECONDS) | |
.observeOn(AndroidSchedulers.mainThread())) | |
.subscribe( | |
bleDevice -> | |
mScanAdapter.add(bleDevice), | |
throwable -> | |
Timber.e(throwable, "Error scanning"), | |
() -> { | |
Timber.d("Scan finished!"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment