diff --git a/README.md b/README.md index 17e026b..025d557 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,38 @@ BluetoothAdapter.STATE_CONNECTED BluetoothAdapter.STATE_DISCONNECTING ``` +##### Observing device bond state + +To observe the bond state of devices, you can receive the `BondStateEvent` which provides the state, previous state, and `BluetoothDevice`. + +```java +rxBluetooth.observeBondState() + .observeOn(AndroidSchedulers.mainThread()) + .subscribeOn(Schedulers.computation()) + .subscribe(new Action1() { + @Override public void call(BondStateEvent event) { + switch (event.getState()) { + case BluetoothDevice.BOND_NONE: + // device unbonded + break; + case BluetoothDevice.BOND_BONDING: + // device bonding + break; + case BluetoothDevice.BOND_BONDED: + // device bonded + break; + } + } + }); +``` + +Possible states are: +```java +BluetoothDevice.BOND_NONE +BluetoothDevice.BOND_BONDING +BluetoothDevice.BOND_BONDED +``` + #### Read and Write with BluetoothSocket After creating a connection to the device, you can use `BluetoothConnection` class to read and write with its socket. diff --git a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/BondStateEvent.java b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/BondStateEvent.java new file mode 100644 index 0000000..b22f9a4 --- /dev/null +++ b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/BondStateEvent.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2015 Ivan Baranov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.github.ivbaranov.rxbluetooth; + +import android.bluetooth.BluetoothDevice; + +/** + * Event container class. Contains bond state (whether the device is unbonded, bonding, or bonded), + * previous bond state, and {@link BluetoothDevice}. + * + * Possible state values are: + * {@link BluetoothDevice#BOND_NONE}, + * {@link BluetoothDevice#BOND_BONDING}, + * {@link BluetoothDevice#BOND_BONDED} + */ +public class BondStateEvent { + + private int mState; + private int mPreviousState; + private BluetoothDevice mBluetoothDevice; + + public BondStateEvent(int state, int previousState, BluetoothDevice bluetoothDevice) { + mState = state; + mPreviousState = previousState; + mBluetoothDevice = bluetoothDevice; + } + + public int getState() { + return mState; + } + + public int getPreviousState() { + return mState; + } + + public BluetoothDevice getBluetoothDevice() { + return mBluetoothDevice; + } + + @Override public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + BondStateEvent that = (BondStateEvent) o; + + if (mState != that.mState) return false; + if (mPreviousState != that.mPreviousState) return false; + return !(mBluetoothDevice != null ? !mBluetoothDevice.equals(that.mBluetoothDevice) + : that.mBluetoothDevice != null); + } + + @Override public int hashCode() { + int result = mState; + result = 31 * result + mPreviousState; + result = 31 * result + (mBluetoothDevice != null ? mBluetoothDevice.hashCode() : 0); + return result; + } + + @Override public String toString() { + return "BondStateEvent{" + + "mState=" + mState + + ", mPreviousState=" + mPreviousState + + ", mBluetoothDevice=" + mBluetoothDevice + + '}'; + } +} diff --git a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java index 98f88b8..7193331 100644 --- a/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java +++ b/rxbluetooth/src/main/java/com/github/ivbaranov/rxbluetooth/RxBluetooth.java @@ -390,6 +390,44 @@ public Observable observeConnectionState() { }); } + /** + * Observes bond state of devices. + * + * @return RxJava Observable with {@link BondStateEvent} + */ + public Observable observeBondState() { + final IntentFilter filter = new IntentFilter(); + filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); + + return Observable.defer(new Func0>() { + @Override public Observable call() { + + return Observable.create(new Observable.OnSubscribe() { + + @Override public void call(final Subscriber subscriber) { + final BroadcastReceiver receiver = new BroadcastReceiver() { + @Override public void onReceive(Context context, Intent intent) { + int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); + int previousState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.BOND_NONE); + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + + subscriber.onNext(new BondStateEvent(state, previousState, device)); + } + }; + + context.registerReceiver(receiver, filter); + + subscriber.add(unsubscribeInUiThread(new Action0() { + @Override public void call() { + context.unregisterReceiver(receiver); + } + })); + } + }); + } + }); + } + /** * Opens {@link BluetoothServerSocket}, listens for a single connection request, releases socket * and returns a connected {@link BluetoothSocket} on successful connection. Notifies observers