diff --git a/android/src/main/java/com/bleplx/BlePlxModule.java b/android/src/main/java/com/bleplx/BlePlxModule.java index b165a1b3..1a415025 100644 --- a/android/src/main/java/com/bleplx/BlePlxModule.java +++ b/android/src/main/java/com/bleplx/BlePlxModule.java @@ -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) { @@ -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() { @Override public void onEvent(ScanResult data) { diff --git a/android/src/main/java/com/bleplx/adapter/BleAdapter.java b/android/src/main/java/com/bleplx/adapter/BleAdapter.java index 9d63f3bd..e0502ae7 100644 --- a/android/src/main/java/com/bleplx/adapter/BleAdapter.java +++ b/android/src/main/java/com/bleplx/adapter/BleAdapter.java @@ -28,6 +28,7 @@ void startDeviceScan( String[] filteredUUIDs, int scanMode, int callbackType, + boolean legacyScan, OnEventCallback onEventCallback, OnErrorCallback onErrorCallback); diff --git a/android/src/main/java/com/bleplx/adapter/BleModule.java b/android/src/main/java/com/bleplx/adapter/BleModule.java index 4cba8f64..1c46cb51 100755 --- a/android/src/main/java/com/bleplx/adapter/BleModule.java +++ b/android/src/main/java/com/bleplx/adapter/BleModule.java @@ -177,6 +177,7 @@ public String getCurrentState() { public void startDeviceScan(String[] filteredUUIDs, int scanMode, int callbackType, + boolean legacyScan, OnEventCallback onEventCallback, OnErrorCallback onErrorCallback) { UUID[] uuids = null; @@ -189,7 +190,7 @@ public void startDeviceScan(String[] filteredUUIDs, } } - safeStartDeviceScan(uuids, scanMode, callbackType, onEventCallback, onErrorCallback); + safeStartDeviceScan(uuids, scanMode, callbackType, legacyScan, onEventCallback, onErrorCallback); } @Override @@ -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 onEventCallback, final OnErrorCallback onErrorCallback) { if (rxBleClient == null) { @@ -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; diff --git a/example/src/components/atoms/Button/Button.styled.tsx b/example/src/components/atoms/Button/Button.styled.tsx index c8cc1f6b..73a322dc 100644 --- a/example/src/components/atoms/Button/Button.styled.tsx +++ b/example/src/components/atoms/Button/Button.styled.tsx @@ -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; diff --git a/example/src/screens/MainStack/DashboardScreen/DashboardScreen.tsx b/example/src/screens/MainStack/DashboardScreen/DashboardScreen.tsx index 0fc6c65f..f59c86c7 100644 --- a/example/src/screens/MainStack/DashboardScreen/DashboardScreen.tsx +++ b/example/src/screens/MainStack/DashboardScreen/DashboardScreen.tsx @@ -75,7 +75,17 @@ export function DashboardScreen({ navigation }: DashboardScreenProps) { )} BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice))} + onPress={() => { + setFoundDevices([]) + BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice, null, true)) + }} + /> + { + setFoundDevices([]) + BLEService.initializeBLE().then(() => BLEService.scanDevices(addFoundDevice, null, false)) + }} /> navigation.navigate('DEVICE_NRF_TEST_SCREEN')} /> diff --git a/example/src/services/BLEService/BLEService.ts b/example/src/services/BLEService/BLEService.ts index 2f175525..35fdd94a 100644 --- a/example/src/services/BLEService/BLEService.ts +++ b/example/src/services/BLEService/BLEService.ts @@ -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) diff --git a/src/index.d.ts b/src/index.d.ts index f6e460d3..c313d5dc 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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 } /**