Skip to content

Commit

Permalink
Add and use AdvertisingSidExtractor, add ScanResult constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMamak authored and dariuszseweryn committed Sep 9, 2024
1 parent 27dd3f4 commit ea0989e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import com.polidea.rxandroidble2.helpers.LocationServicesOkObservable;
import com.polidea.rxandroidble2.internal.DeviceComponent;
import com.polidea.rxandroidble2.internal.RxBleLog;
import com.polidea.rxandroidble2.internal.scan.AdvertisingSidExtractor;
import com.polidea.rxandroidble2.internal.scan.AdvertisingSidExtractorApi18;
import com.polidea.rxandroidble2.internal.scan.AdvertisingSidExtractorApi26;
import com.polidea.rxandroidble2.internal.scan.BackgroundScannerImpl;
import com.polidea.rxandroidble2.internal.scan.InternalToExternalScanResultConverter;
import com.polidea.rxandroidble2.internal.scan.IsConnectableChecker;
Expand Down Expand Up @@ -358,6 +361,19 @@ static IsConnectableChecker provideIsConnectableChecker(
return isConnectableCheckerApi26.get();
}

@Provides
@ClientScope
static AdvertisingSidExtractor provideAdvertisingSidExtractor(
@Named(PlatformConstants.INT_DEVICE_SDK) int deviceSdk,
Provider<AdvertisingSidExtractorApi18> advertisingSidExtractorApi18,
Provider<AdvertisingSidExtractorApi26> advertisingSidExtractorApi26
) {
if (deviceSdk < Build.VERSION_CODES.O) {
return advertisingSidExtractorApi18.get();
}
return advertisingSidExtractorApi26.get();
}

@Provides
@Named(BluetoothConstants.ENABLE_NOTIFICATION_VALUE)
static byte[] provideEnableNotificationValue() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.polidea.rxandroidble2.internal.scan;

import android.bluetooth.le.ScanResult;

import androidx.annotation.RestrictTo;

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public interface AdvertisingSidExtractor {
Integer extract(ScanResult scanResult);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.polidea.rxandroidble2.internal.scan;

import android.bluetooth.le.ScanResult;

import bleshadow.javax.inject.Inject;

public class AdvertisingSidExtractorApi18 implements AdvertisingSidExtractor {
@Inject
public AdvertisingSidExtractorApi18() {
}

@Override
public Integer extract(ScanResult scanResult) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.polidea.rxandroidble2.internal.scan;

import android.bluetooth.le.ScanResult;

import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;

import bleshadow.javax.inject.Inject;

@RequiresApi(26 /* Build.VERSION_CODES.O */)
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class AdvertisingSidExtractorApi26 implements AdvertisingSidExtractor {
@Inject
public AdvertisingSidExtractorApi26() {
}

@Override
public Integer extract(ScanResult scanResult) {
return scanResult.getAdvertisingSid();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
Expand All @@ -27,11 +26,14 @@ public class InternalScanResultCreator {

private final ScanRecordParser scanRecordParser;
private final IsConnectableChecker isConnectableChecker;
private final AdvertisingSidExtractor advertisingSidExtractor;

@Inject
public InternalScanResultCreator(ScanRecordParser scanRecordParser, IsConnectableChecker isConnectableChecker) {
public InternalScanResultCreator(ScanRecordParser scanRecordParser, IsConnectableChecker isConnectableChecker,
AdvertisingSidExtractor advertisingSidExtractor) {
this.scanRecordParser = scanRecordParser;
this.isConnectableChecker = isConnectableChecker;
this.advertisingSidExtractor = advertisingSidExtractor;
}

public RxBleInternalScanResult create(BluetoothDevice bluetoothDevice, int rssi, byte[] scanRecord) {
Expand All @@ -43,17 +45,15 @@ public RxBleInternalScanResult create(BluetoothDevice bluetoothDevice, int rssi,
@RequiresApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
public RxBleInternalScanResult create(ScanResult result) {
final ScanRecordImplNativeWrapper scanRecord = new ScanRecordImplNativeWrapper(result.getScanRecord(), scanRecordParser);
final Integer advertisingSid = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? result.getAdvertisingSid() : null;
return new RxBleInternalScanResult(result.getDevice(), result.getRssi(), result.getTimestampNanos(), scanRecord,
ScanCallbackType.CALLBACK_TYPE_BATCH, isConnectableChecker.check(result), advertisingSid);
ScanCallbackType.CALLBACK_TYPE_BATCH, isConnectableChecker.check(result), advertisingSidExtractor.extract(result));
}

@RequiresApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
public RxBleInternalScanResult create(int callbackType, ScanResult result) {
final ScanRecordImplNativeWrapper scanRecord = new ScanRecordImplNativeWrapper(result.getScanRecord(), scanRecordParser);
final Integer advertisingSid = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? result.getAdvertisingSid() : null;
return new RxBleInternalScanResult(result.getDevice(), result.getRssi(), result.getTimestampNanos(), scanRecord,
toScanCallbackType(callbackType), isConnectableChecker.check(result), advertisingSid);
toScanCallbackType(callbackType), isConnectableChecker.check(result), advertisingSidExtractor.extract(result));
}

@RequiresApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.polidea.rxandroidble2.scan;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

import com.polidea.rxandroidble2.RxBleDevice;
import com.polidea.rxandroidble2.internal.logger.LoggerUtil;
Expand All @@ -15,6 +16,18 @@ public class ScanResult {
private final IsConnectable isConnectable;
private final Integer advertisingSid;

public ScanResult(RxBleDevice bleDevice, int rssi, long timestampNanos, ScanCallbackType callbackType,
ScanRecord scanRecord, IsConnectable isConnectable) {
this.bleDevice = bleDevice;
this.rssi = rssi;
this.timestampNanos = timestampNanos;
this.callbackType = callbackType;
this.scanRecord = scanRecord;
this.isConnectable = isConnectable;
this.advertisingSid = null;
}

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public ScanResult(RxBleDevice bleDevice, int rssi, long timestampNanos, ScanCallbackType callbackType,
ScanRecord scanRecord, IsConnectable isConnectable, Integer advertisingSid) {
this.bleDevice = bleDevice;
Expand Down

0 comments on commit ea0989e

Please sign in to comment.