-
-
Save elloza/7bc3ffb9b37b9158aaece510d7c9ea39 to your computer and use it in GitHub Desktop.
Rx Wrapper around Bluetooth Low Energy scan process
This file contains hidden or 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
import android.bluetooth.BluetoothAdapter; | |
import android.bluetooth.BluetoothDevice; | |
import android.text.TextUtils; | |
import rx.Observable; | |
import rx.Subscriber; | |
import rx.android.schedulers.AndroidSchedulers; | |
import rx.functions.Action0; | |
import rx.schedulers.Schedulers; | |
/** | |
Rx Wrapper around Bluetooth Low Energy scan process | |
*/ | |
public class RxScanner { | |
final BluetoothAdapter adapter; | |
private LeCallback callback; | |
public RxScanner(BluetoothAdapter adapter) { | |
this.adapter = adapter; | |
} | |
/** | |
Scan observable, subscribe to start scan, unsubscribe to finish | |
*/ | |
public Observable<DeviceEntry> observe() { | |
return Observable.create(new Observable.OnSubscribe<DeviceEntry>() { | |
@Override | |
public void call(final Subscriber<? super DeviceEntry> subscriber) { | |
callback = new LeCallback(subscriber); | |
adapter.startLeScan(callback); | |
} | |
}) | |
.doOnUnsubscribe(new Action0() { | |
@Override | |
public void call() { | |
adapter.stopLeScan(callback); | |
callback.onComplete(); | |
} | |
}) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()); | |
} | |
private static class LeCallback implements BluetoothAdapter.LeScanCallback { | |
final Subscriber<? super DeviceEntry> subscriber; | |
private LeCallback(Subscriber<? super DeviceEntry> subscriber) { | |
this.subscriber = subscriber; | |
} | |
@Override | |
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) { | |
if (isCompleted) { | |
return; | |
} | |
subscriber.onNext(new DeviceEntry(device, scanRecord)); | |
} | |
private volatile boolean isCompleted = false; | |
public void onComplete() { | |
isCompleted = true; | |
subscriber.onCompleted(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment