Skip to content

Commit

Permalink
Added code to get bonded devices from adapter (#46)
Browse files Browse the repository at this point in the history
* Added support for getBondedDevices
  • Loading branch information
fracturedpsyche authored and uKL committed Jul 22, 2016
1 parent 341c5fe commit bf82ef8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.polidea.rxandroidble.internal.RxBleLog;

import java.util.Set;
import java.util.UUID;

import rx.Observable;
Expand Down Expand Up @@ -41,6 +42,8 @@ public static void setLogLevel(@RxBleLog.LogLevel int logLevel) {
*/
public abstract RxBleDevice getBleDevice(@NonNull String macAddress);

public abstract Set<RxBleDevice> getBondedDevices();

/**
* Returns an infinite observable emitting BLE scan results.
* Scan is automatically started and stopped based on the Observable lifecycle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.polidea.rxandroidble.internal.util.UUIDUtil;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -69,6 +70,17 @@ public RxBleDevice getBleDevice(@NonNull String macAddress) {
return rxBleDeviceProvider.getBleDevice(macAddress);
}

@Override
public Set<RxBleDevice> getBondedDevices() {
Set<RxBleDevice> rxBleDevices = new HashSet<>();
Set<BluetoothDevice> bluetoothDevices = rxBleAdapterWrapper.getBondedDevices();
for (BluetoothDevice bluetoothDevice : bluetoothDevices) {
rxBleDevices.add(getBleDevice(bluetoothDevice.getAddress()));
}

return rxBleDevices;
}

@Override
public Observable<RxBleScanResult> scanBleDevices(@Nullable UUID... filterServiceUUIDs) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.bluetooth.BluetoothDevice;
import android.support.annotation.Nullable;

import java.util.Set;

public class RxBleAdapterWrapper {

private final BluetoothAdapter bluetoothAdapter;
Expand Down Expand Up @@ -31,4 +33,8 @@ public boolean startLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
public void stopLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
bluetoothAdapter.stopLeScan(leScanCallback);
}

public Set<BluetoothDevice> getBondedDevices() {
return bluetoothAdapter.getBondedDevices();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.polidea.rxandroidble
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import com.polidea.rxandroidble.internal.util.RxBleAdapterWrapper
import org.apache.maven.artifact.versioning.OverConstrainedVersionException

class MockRxBleAdapterWrapper extends RxBleAdapterWrapper {

Expand All @@ -19,6 +20,7 @@ class MockRxBleAdapterWrapper extends RxBleAdapterWrapper {
}

private List<ScanData> scanDataList = new ArrayList<>()
private Set<BluetoothDevice> bondedDevices = new HashSet<>()

MockRxBleAdapterWrapper() {
super(null)
Expand All @@ -32,6 +34,10 @@ class MockRxBleAdapterWrapper extends RxBleAdapterWrapper {
scanDataList.add(new ScanData(bluetoothDevice, rssi, scanResult))
}

def addBondedDevice(BluetoothDevice bluetoothDevice) {
bondedDevices.add(bluetoothDevice)
}

@Override
BluetoothDevice getRemoteDevice(String macAddress) {
scanDataList.find {
Expand Down Expand Up @@ -63,4 +69,9 @@ class MockRxBleAdapterWrapper extends RxBleAdapterWrapper {
void stopLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {

}

@Override
Set<BluetoothDevice> getBondedDevices() {
return bondedDevices
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ class RxBleClientTest extends Specification {
)
}

def "should return bonded devices"() {
given:
bluetoothDeviceBonded("AA:AA:AA:AA:AA:AA")
bluetoothDeviceBonded("BB:BB:BB:BB:BB:BB")
bluetoothDeviceDiscovered deviceMac: "AA:AA:AA:AA:AA:AA", rssi: 0, scanRecord: [] as byte[]
bluetoothDeviceDiscovered deviceMac: "BB:BB:BB:BB:BB:BB", rssi: 50, scanRecord: [] as byte[]

when:
def results = objectUnderTest.getBondedDevices()

then:
assert results.size() == 2
}

def "should start BLE scan if subscriber subscribes to the scan observable"() {
given:
TestSubscriber testSubscriber = new TestSubscriber<>()
Expand Down Expand Up @@ -335,6 +349,13 @@ class RxBleClientTest extends Specification {
bleAdapterWrapperSpy.addScanResult(mock, scanData['rssi'], scanData['scanRecord'])
}
def bluetoothDeviceBonded(String address) {
def mock = Mock(BluetoothDevice)
mock.getAddress() >> address
mock.hashCode() >> address.hashCode()
bleAdapterWrapperSpy.addBondedDevice(mock);
}
/**
* This test reproduces issue: https://github.com/Polidea/RxAndroidBle/issues/17
* It first calls startLeScan method which takes 100ms to finish
Expand Down

0 comments on commit bf82ef8

Please sign in to comment.