Skip to content

Observing discovery state

Ivan Baranov edited this page Jun 21, 2017 · 5 revisions

To start discovering devices, call rxBluetooth.startDiscovery(). The process is asynchronous and the method will immediately return with a boolean indicating whether discovery has successfully started.

To observe discovery 'started' state:

rxBluetooth.observeDiscovery()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .filter(BtPredicate.in(BluetoothAdapter.ACTION_DISCOVERY_STARTED))
    .subscribe(new Consumer<String>() {
      @Override public void accept(String action) throws Exception {
        //
      }
    });

Discovery state can be BluetoothAdapter.ACTION_DISCOVERY_STARTED or BluetoothAdapter.ACTION_DISCOVERY_FINISHED.

To observe both discovery states:

rxBluetooth.observeDiscovery()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .filter(BtPredicate.in(BluetoothAdapter.ACTION_DISCOVERY_STARTED, BluetoothAdapter.ACTION_DISCOVERY_FINISHED))
    .subscribe(new Consumer<String>() {
      @Override public void accept(String action) throws Exception {
        //
      }
    });