Skip to content

Commit

Permalink
feat: provide option to disable legacy scan for Android (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmiszewski-intent authored Oct 16, 2023
1 parent 6c6bfe2 commit fe7f5af
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 6 deletions.
6 changes: 5 additions & 1 deletion android/src/main/java/com/bleplx/BlePlxModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void startDeviceScan(@Nullable ReadableArray filteredUUIDs, @Nullable Rea

int scanMode = DEFAULT_SCAN_MODE_LOW_POWER;
int callbackType = DEFAULT_CALLBACK_TYPE_ALL_MATCHES;
boolean legacyScan = true;

if (options != null) {
if (options.hasKey("scanMode") && options.getType("scanMode") == ReadableType.Number) {
Expand All @@ -196,11 +197,14 @@ public void startDeviceScan(@Nullable ReadableArray filteredUUIDs, @Nullable Rea
if (options.hasKey("callbackType") && options.getType("callbackType") == ReadableType.Number) {
callbackType = options.getInt("callbackType");
}
if (options.hasKey("legacyScan") && options.getType("legacyScan") == ReadableType.Boolean) {
legacyScan = options.getBoolean("legacyScan");
}
}

bleAdapter.startDeviceScan(
filteredUUIDs != null ? ReadableArrayConverter.toStringArray(filteredUUIDs) : null,
scanMode, callbackType,
scanMode, callbackType, legacyScan,
new OnEventCallback<ScanResult>() {
@Override
public void onEvent(ScanResult data) {
Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/com/bleplx/adapter/BleAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void startDeviceScan(
String[] filteredUUIDs,
int scanMode,
int callbackType,
boolean legacyScan,
OnEventCallback<ScanResult> onEventCallback,
OnErrorCallback onErrorCallback);

Expand Down
7 changes: 5 additions & 2 deletions android/src/main/java/com/bleplx/adapter/BleModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public String getCurrentState() {
public void startDeviceScan(String[] filteredUUIDs,
int scanMode,
int callbackType,
boolean legacyScan,
OnEventCallback<ScanResult> onEventCallback,
OnErrorCallback onErrorCallback) {
UUID[] uuids = null;
Expand All @@ -189,7 +190,7 @@ public void startDeviceScan(String[] filteredUUIDs,
}
}

safeStartDeviceScan(uuids, scanMode, callbackType, onEventCallback, onErrorCallback);
safeStartDeviceScan(uuids, scanMode, callbackType, legacyScan, onEventCallback, onErrorCallback);
}

@Override
Expand Down Expand Up @@ -1159,7 +1160,8 @@ private String mapNativeAdapterStateToLocalBluetoothState(int adapterState) {

private void safeStartDeviceScan(final UUID[] uuids,
final int scanMode,
int callbackType,
final int callbackType,
final boolean legacyScan,
final OnEventCallback<ScanResult> onEventCallback,
final OnErrorCallback onErrorCallback) {
if (rxBleClient == null) {
Expand All @@ -1170,6 +1172,7 @@ private void safeStartDeviceScan(final UUID[] uuids,
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(scanMode)
.setCallbackType(callbackType)
.setLegacy(legacyScan)
.build();

int length = uuids == null ? 0 : uuids.length;
Expand Down
1 change: 1 addition & 0 deletions example/src/components/atoms/Button/Button.styled.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AppText } from '../AppText/AppText'
export const Container = styled(TouchableOpacity)`
${({ theme }) => css`
background-color: ${theme.colors.mainRed};
margin: 10px;
padding: 12px;
align-items: center;
border-radius: 100px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,17 @@ export function DashboardScreen({ navigation }: DashboardScreenProps) {
)}
<AppButton
label="Look for devices"
onPress={() => BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice))}
onPress={() => {
setFoundDevices([])
BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice, null, true))
}}
/>
<AppButton
label="Look for devices (legacy off)"
onPress={() => {
setFoundDevices([])
BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice, null, false))
}}
/>
<AppButton label="Ask for permissions" onPress={BLEService.requestBluetoothPermission} />
<AppButton label="Go to nRF test" onPress={() => navigation.navigate('DEVICE_NRF_TEST_SCREEN')} />
Expand Down
4 changes: 2 additions & 2 deletions example/src/services/BLEService/BLEService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class BLEServiceInstance {
this.showErrorToast('Bluetooth is turned off')
}

scanDevices = async (onDeviceFound: (device: Device) => void, UUIDs: UUID[] | null = null) => {
this.manager.startDeviceScan(UUIDs, null, (error, device) => {
scanDevices = async (onDeviceFound: (device: Device) => void, UUIDs: UUID[] | null = null, legacyScan?: boolean) => {
this.manager.startDeviceScan(UUIDs, { legacyScan }, (error, device) => {
if (error) {
this.onError(error)
console.error(error.message)
Expand Down
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ declare module 'react-native-ble-plx' {
* Scan callback type for Bluetooth LE scan [Android only]
*/
callbackType?: ScanCallbackType
/**
* Use legacyScan (default true) [Android only]
* https://developer.android.com/reference/android/bluetooth/le/ScanSettings.Builder#setLegacy(boolean)
*/
legacyScan?: boolean
}

/**
Expand Down

0 comments on commit fe7f5af

Please sign in to comment.