Skip to content

Observing bluetooth state

Ivan Baranov edited this page Dec 15, 2015 · 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(Action.isEqualTo(BluetoothAdapter.STATE_ON))
      .subscribe(new Action1<Integer>() {
        @Override public void call(Integer state) {
          //
        }
      });

or multiple states:

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