Skip to content

Commit

Permalink
Merge pull request #10 from robholmes/observable-bond-state
Browse files Browse the repository at this point in the history
Added bond state observable
  • Loading branch information
IvBaranov authored Feb 3, 2017
2 parents 5ed8cba + d91f6aa commit 4b570ea
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<BondStateEvent>() {
@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.

Expand Down
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,44 @@ public Observable<ConnectionStateEvent> observeConnectionState() {
});
}

/**
* Observes bond state of devices.
*
* @return RxJava Observable with {@link BondStateEvent}
*/
public Observable<BondStateEvent> observeBondState() {
final IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);

return Observable.defer(new Func0<Observable<BondStateEvent>>() {
@Override public Observable<BondStateEvent> call() {

return Observable.create(new Observable.OnSubscribe<BondStateEvent>() {

@Override public void call(final Subscriber<? super BondStateEvent> 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
Expand Down

0 comments on commit 4b570ea

Please sign in to comment.