Skip to content

Added code to get bonded devices from adapter #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

import com.polidea.rxandroidble.internal.RxBleLog;

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

import rx.Observable;
@@ -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.
Original file line number Diff line number Diff line change
@@ -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;
@@ -65,6 +66,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) {

Original file line number Diff line number Diff line change
@@ -59,7 +59,8 @@ protected void protectedRun() {
}
}

public synchronized void stop() { // synchronized keyword added to be sure that operation will be stopped no matter which thread will call it
// synchronized keyword added to be sure that operation will be stopped no matter which thread will call it
public synchronized void stop() {
isStopped = true;
if (isStarted) {
// TODO: [PU] 29.01.2016 https://code.google.com/p/android/issues/detail?id=160503
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@
import android.bluetooth.BluetoothDevice;
import android.support.annotation.Nullable;

import java.util.Set;

public class RxBleAdapterWrapper {

private final BluetoothAdapter bluetoothAdapter;
@@ -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
@@ -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 {

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

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

MockRxBleAdapterWrapper() {
super(null)
@@ -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 {
@@ -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
@@ -34,6 +34,20 @@ class RxBleClientTest extends Specification {
adapterStateObservable.asObservable(), uuidParserSpy, Mock(BleConnectionCompat), locationServicesStatusMock)
}

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<>()
@@ -320,6 +334,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