Skip to content

Observing bluetooth state

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

The state of the local Bluetooth adapter can be one of: BluetoothAdapter.STATE_OFF, BluetoothAdapter.STATE_TURNING_ON, BluetoothAdapter.STATE_ON, BluetoothAdapter.STATE_TURNING_OFF.

You can observe single state:

rxBluetooth.observeBluetoothState()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .filter(BtPredicate.in(BluetoothAdapter.STATE_ON))
    .subscribe(new Consumer<Integer>() {
      @Override public void accept(Integer integer) throws Exception {
        //
      }
    });

or multiple states:

rxBluetooth.observeBluetoothState()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeOn(Schedulers.computation())
    .subscribe(new Consumer<Integer>() {
      @Override public void accept(Integer integer) throws Exception {
        //switch (state) {
            //case BluetoothAdapter.STATE_OFF:
              //
              //break;
           //...
         //}
      }
    });