Skip to content

Instantly share code, notes, and snippets.

@JogahCR
Created September 20, 2017 19:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JogahCR/5671b11bda86eb4f53479c7c42eaeea9 to your computer and use it in GitHub Desktop.
Extensible scan sample
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