Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ext {
targetCompatibilityVersion = JavaVersion.VERSION_1_8
shadowPluginVersion = '7.1.2'
daggerVersion = '2.42'
publishingVersionName = '1.0.11'
libs = [
/* Library-only dependencies */
rxjava : "io.reactivex.rxjava2:rxjava:$rxJavaVersion",
Expand Down
33 changes: 33 additions & 0 deletions mockrxandroidble/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ apply plugin: 'groovyx.android'
apply plugin: 'com.vanniktech.maven.publish'
apply from: rootProject.file('gradle/gradle-mvn-config.gradle')

def githubProperties = new Properties()
githubProperties.load(new FileInputStream(rootProject.file("github.properties"))) //Set env variable GPR_USER & GPR_API_KEY if not adding a properties file

def getVersionName = { ->
return rootProject.ext.publishingVersionName// Replace with version Name
}

def getArtificatId = { ->
return "mockrxandroidble" // Replace with library name ID
}

android {
namespace 'com.polidea.rxandroidble2.mockrxandroidble'

Expand Down Expand Up @@ -42,6 +53,28 @@ android {
}
}
}

publishing {
publications {
gpr(MavenPublication) {
groupId 'com.hiorka' // Replace with group ID
artifactId getArtificatId()
version getVersionName()
artifact("$buildDir/outputs/aar/${getArtificatId()}-release.aar")
}
}

repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/Evocolabs/mockrxandroidble")
credentials {
username = githubProperties['gpr.usr'] ?: System.getenv("GPR_USER")
password = githubProperties['gpr.key'] ?: System.getenv("GPR_API_KEY")
}
}
}
}
}

afterEvaluate { project ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class RxBleClientMock extends RxBleClient {
public static class Builder {

private ReplaySubject<RxBleDeviceMock> discoverableDevicesSubject;
private ReplaySubject<RxBleDeviceMock> discoverableBredrDevicesSubject;
private Set<RxBleDevice> bondedDevices;
private Set<RxBleDevice> connectedPeripherals;

Expand All @@ -47,6 +48,7 @@ public static class Builder {
*/
public Builder() {
this.discoverableDevicesSubject = ReplaySubject.create();
this.discoverableBredrDevicesSubject = ReplaySubject.create();
this.bondedDevices = new HashSet<>();
this.connectedPeripherals = new HashSet<>();
}
Expand All @@ -56,6 +58,11 @@ public Builder setDeviceDiscoveryObservable(@NonNull Observable<RxBleDeviceMock>
return this;
}

public Builder setBredrDeviceDiscoveryObservable(@NonNull Observable<RxBleDeviceMock> discoverableBredrDevicesObservable) {
discoverableBredrDevicesObservable.subscribe(this.discoverableBredrDevicesSubject);
return this;
}

/**
* Add a {@link RxBleDevice} to the mock client.
*
Expand Down Expand Up @@ -96,6 +103,7 @@ public RxBleClientMock build() {

/**
* Device builder class.
*
* @deprecated Use {@link RxBleDeviceMock.Builder}
*/
@Deprecated
Expand All @@ -118,7 +126,7 @@ public CharacteristicsBuilder() {
/**
* Adds a {@link BluetoothGattCharacteristic}
*
* @param characteristic The characteristic to add
* @param characteristic The characteristic to add
*/
public CharacteristicsBuilder addCharacteristic(BluetoothGattCharacteristic characteristic) {
this.bluetoothGattCharacteristics.add(characteristic);
Expand Down Expand Up @@ -148,9 +156,9 @@ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param properties OR-ed {@link BluetoothGattCharacteristic} property constants
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param properties OR-ed {@link BluetoothGattCharacteristic} property constants
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data,
Expand Down Expand Up @@ -202,8 +210,8 @@ public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
/**
* Adds a {@link BluetoothGattCharacteristic} with specified parameters.
*
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
* @param uuid characteristic UUID
* @param data locally stored value of the characteristic
*/
public CharacteristicsBuilder addCharacteristic(@NonNull UUID uuid,
@NonNull byte[] data) {
Expand Down Expand Up @@ -263,11 +271,13 @@ public List<BluetoothGattDescriptor> build() {
private Set<RxBleDevice> bondedDevices;
private Set<RxBleDevice> connectedPeripherals;
private ReplaySubject<RxBleDeviceMock> discoveredDevicesSubject;
private ReplaySubject<RxBleDeviceMock> discoveredBredrDevicesSubject;

private RxBleClientMock(Builder builder) {
bondedDevices = builder.bondedDevices;
connectedPeripherals = builder.connectedPeripherals;
discoveredDevicesSubject = builder.discoverableDevicesSubject;
discoveredBredrDevicesSubject = builder.discoverableBredrDevicesSubject;
}

@Override
Expand All @@ -283,6 +293,17 @@ public RxBleDevice getBleDevice(@NonNull final String macAddress) {
throw new IllegalStateException("Mock is not configured for a given mac address. Use Builder#addDevice method.");
}

@Override
public RxBleDevice getBredrDevice(@NonNull String macAddress) {
Object[] rxBredrDevices = discoveredBredrDevicesSubject.getValues();
for (Object bredrDevice : rxBredrDevices) {
if (((RxBleDevice) bredrDevice).getIsBredr() && ((RxBleDevice) bredrDevice).getMacAddress().equals(macAddress)) {
return (RxBleDevice) bredrDevice;
}
}
throw new IllegalStateException("Mock is not configured for a given mac address. Use Builder#addDevice method.");
}

@Override
public Set<RxBleDevice> getBondedDevices() {
return bondedDevices;
Expand Down Expand Up @@ -440,6 +461,13 @@ public String[] getRecommendedScanRuntimePermissions() {
return new String[0];
}

@Override
public Observable<RxBleDevice> scanBredrDevices() {
return discoveredBredrDevicesSubject
.filter(RxBleDeviceMock::getIsBredr)
.map(rxBleDeviceMock -> rxBleDeviceMock);
}

@Override
public String[] getRecommendedConnectRuntimePermissions() {
return new String[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import io.reactivex.Observable;
import io.reactivex.Single;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
Expand All @@ -52,17 +53,20 @@ public class RxBleDeviceMock implements RxBleDevice {
private List<UUID> advertisedUUIDs;
private BluetoothDevice bluetoothDevice;
private AtomicBoolean isConnected = new AtomicBoolean(false);
private final Boolean isBredr;

private RxBleDeviceMock(String name,
String macAddress,
@Nullable BluetoothDevice bluetoothDevice,
RxBleConnectionMock connectionMock) {
RxBleConnectionMock connectionMock,
boolean isBredr) {
this.name = name;
this.macAddress = macAddress;
this.rssi = connectionMock.getRssi();
this.advertisedUUIDs = connectionMock.getServiceUuids();
this.bluetoothDevice = bluetoothDevice;
this.rxBleConnection = connectionMock;
this.isBredr = isBredr;
connectionMock.setDeviceMock(this);
}

Expand All @@ -72,6 +76,7 @@ public RxBleDeviceMock(String name,
Integer rssi,
RxBleDeviceServices rxBleDeviceServices,
Map<UUID, Observable<byte[]>> characteristicNotificationSources,
Boolean isBredr,
@Nullable BluetoothDevice bluetoothDevice) {
this(
name,
Expand All @@ -84,22 +89,25 @@ public RxBleDeviceMock(String name,
new HashMap<UUID, RxBleCharacteristicReadCallback>(),
new HashMap<UUID, RxBleCharacteristicWriteCallback>(),
new HashMap<UUID, Map<UUID, RxBleDescriptorReadCallback>>(),
new HashMap<UUID, Map<UUID, RxBleDescriptorWriteCallback>>())
);
new HashMap<UUID, Map<UUID, RxBleDescriptorWriteCallback>>()),
isBredr
);
this.legacyScanRecord = scanRecord;
}

public RxBleDeviceMock(String name,
String macAddress,
ScanRecord scanRecord,
@Nullable BluetoothDevice bluetoothDevice,
RxBleConnectionMock connectionMock
RxBleConnectionMock connectionMock,
Boolean isBredr
) {
this(
name,
macAddress,
bluetoothDevice,
connectionMock
connectionMock,
isBredr
);
this.scanRecord = scanRecord;
}
Expand All @@ -111,6 +119,7 @@ public static class Builder {
private byte[] legacyScanRecord;
private ScanRecord scanRecord;
private BluetoothDevice bluetoothDevice;
private Boolean isBredr = false;
RxBleConnectionMock connectionMock;
RxBleConnectionMock.Builder connectionMockBuilder;

Expand All @@ -129,8 +138,9 @@ public Builder() {
* Create the {@link RxBleDeviceMock} instance using the configured values.
*/
public RxBleDevice build() {
if (this.deviceMacAddress == null) throw new IllegalStateException("DeviceMacAddress required."
+ " DeviceBuilder#deviceMacAddress should be called.");
if (this.deviceMacAddress == null)
throw new IllegalStateException("DeviceMacAddress required."
+ " DeviceBuilder#deviceMacAddress should be called.");
if (this.scanRecord == null && this.legacyScanRecord == null)
throw new IllegalStateException("ScanRecord required. DeviceBuilder#scanRecord should be called.");

Expand All @@ -143,6 +153,7 @@ public RxBleDevice build() {
connMock.getRssi(),
connMock.getRxBleDeviceServices(),
connMock.getCharacteristicNotificationSources(),
isBredr,
bluetoothDevice);
for (UUID service : connMock.getServiceUuids()) {
rxBleDeviceMock.addAdvertisedUUID(service);
Expand All @@ -153,8 +164,9 @@ public RxBleDevice build() {
deviceMacAddress,
scanRecord,
bluetoothDevice,
connMock
);
connMock,
isBredr
);
}

/**
Expand All @@ -164,7 +176,7 @@ public RxBleDevice build() {
* @param characteristics characteristics that the service should report. Use {@link RxBleClientMock.CharacteristicsBuilder} to
* create them.
* @deprecated Use {@link #connection(RxBleConnectionMock connectionMock)} and
* {@link RxBleConnectionMock.Builder#addService(UUID uuid, List characteristics)}
* {@link RxBleConnectionMock.Builder#addService(UUID uuid, List characteristics)}
*/
@Deprecated
public Builder addService(@NonNull UUID uuid, @NonNull List<BluetoothGattCharacteristic> characteristics) {
Expand Down Expand Up @@ -203,7 +215,7 @@ public Builder bluetoothDevice(@NonNull BluetoothDevice bluetoothDevice) {
* @param characteristicUUID UUID of the characteristic that will be observed for notifications
* @param sourceObservable Observable that will be subscribed to in order to receive characteristic change notifications
* @deprecated Use {@link #connectionMock} and
* {@link RxBleConnectionMock.Builder#notificationSource(UUID characteristicUUID, Observable sourceObservable)}
* {@link RxBleConnectionMock.Builder#notificationSource(UUID characteristicUUID, Observable sourceObservable)}
*/
@Deprecated
public Builder notificationSource(@NonNull UUID characteristicUUID, @NonNull Observable<byte[]> sourceObservable) {
Expand All @@ -213,6 +225,7 @@ public Builder notificationSource(@NonNull UUID characteristicUUID, @NonNull Obs

/**
* Set a rssi that will be reported. Calling this method is required.
*
* @deprecated Use {@link #connectionMock} and {@link RxBleConnectionMock.Builder#rssi(int rssi)}
*/
@Deprecated
Expand Down Expand Up @@ -244,6 +257,14 @@ public Builder connection(@NonNull RxBleConnectionMock connectionMock) {
this.connectionMock = connectionMock;
return this;
}

/**
* Set if the device is Bredr. Calling this method is not required. Default is false
*/
public Builder isBredr(Boolean isBredr) {
this.isBredr = isBredr;
return this;
}
}

public void addAdvertisedUUID(UUID advertisedUUID) {
Expand Down Expand Up @@ -288,6 +309,11 @@ public Observable<RxBleConnection> establishConnection(boolean autoConnect, Time
return establishConnection(autoConnect);
}

@Override
public Single<Boolean> createBond() {
throw new UnsupportedOperationException("Mock doesn't support createBond Operation");
}

private Observable<RxBleConnection> emitConnectionWithoutCompleting() {
connectionSubject = ReplaySubject.createWithSize(1);
connectionSubject.onNext(rxBleConnection);
Expand Down Expand Up @@ -327,6 +353,11 @@ public BluetoothDevice getBluetoothDevice() {
throw new IllegalStateException("Mock is not configured to return a BluetoothDevice");
}

@Override
public Boolean getA2dpConnected() {
return false;
}

@Override
public String getName() {
return name;
Expand All @@ -341,7 +372,7 @@ public byte[] getLegacyScanRecord() {
}

public Boolean getIsBredr() {
return false;
return isBredr;
}

public ScanRecord getScanRecord() {
Expand Down
3 changes: 1 addition & 2 deletions rxandroidble/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ apply plugin: 'groovyx.android'
apply from: rootProject.file('gradle/gradle-mvn-config.gradle')
apply plugin: 'maven-publish'


def githubProperties = new Properties()
githubProperties.load(new FileInputStream(rootProject.file("github.properties"))) //Set env variable GPR_USER & GPR_API_KEY if not adding a properties file

def getVersionName = { ->
return "1.0.11" // Replace with version Name
return rootProject.ext.publishingVersionName// Replace with version Name
}

def getArtificatId = { ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public class BluetoothDevice implements Parcelable {
TRANSPORT_BREDR,
TRANSPORT_LE,
}
)1
)
public @interface Transport {}
public static final int TRANSPORT_AUTO = 0;
public static final int TRANSPORT_BREDR = 1;
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include ':rxandroidble', ':dagger-compiler-shadow', ':dagger-library-shadow'
include ':rxandroidble', ':dagger-compiler-shadow', ':dagger-library-shadow', ':mockrxandroidble'
include ':sample', ':sample-kotlin'
//include 'mockrxandroidble'

Expand Down