0

Because I will have multiple applications using same implementation to connected to a BLE device, I am trying to move that code inside a module. However I am experiencing a strange issue, that the scanning code does not work within the module when the same code works when inside a folder within the app.

I have tested with the following code:

import {
  BleError,
  BleManager,
  Device as BlxDevice,
  LogLevel,
} from 'react-native-ble-plx';
import { PermissionsAndroid } from 'react-native';

const manager = new BleManager();
manager.setLogLevel(LogLevel.Verbose);

export const tryScan = () => {
  console.log('In main app');
  PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  );

  PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  ),
    manager.startDeviceScan(
      null,
      { allowDuplicates: true },
      (error: BleError | null, scannedDevice: BlxDevice | null) => {
        console.log('Discovered device', scannedDevice);
        manager.stopDeviceScan();
      },
    );
};

When inside the module, no device is discovered at all, when within the app not as a module, it works as expected.

The module has the native library as a peer dependency:

  "peerDependencies": {
    "react-native": "^0.64.x",
    "react-native-ble-plx": "^2.x"
  },

The app is including the module in this way

  "dependencies": {
    "device-control": "file:../device-control",
  },

I have verified I have the correct code within the app's node_modules.

To add to my confusion, in the application using the react-native-ble-plx works for monitoring the BLE state with manager.onStateChange.

I did not find error in logcat, there is no error in the metro either. Could anyone advice me as to what might be the reason for the error?

RobC
  • 22,977
  • 20
  • 73
  • 80
Viktor
  • 521
  • 1
  • 4
  • 17
  • Not an actual answer, but as a reasonable workaround for anyone interested I am requiring the client projects to pass BleManager to the module. Since there should be only one BleManager it is actually a reasonable solution. – Viktor Sep 29 '21 at 22:25

1 Answers1

0

I was facing the same issue. Unless the compile SDK in not a requirement for you, I did download it from 33 to 31. Also downgrade the target SDK. I haven't tested different configurations but this configuration works for my prototype project.

buildToolsVersion = "29.0.3"

minSdkVersion = 23

compileSdkVersion = 31

targetSdkVersion = 30

Im requesting these permissions:

const _PERMISSIONS = [
  PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
  PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
  PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
  PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT
];

if (Platform.OS != "android") return;
const granted = await PermissionsAndroid.requestMultiple(_PERMISSIONS);

AndroidManifest.xml

<!-- Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in 
Android API 23+ -->
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

  <uses-feature android:name="android.hardware.bluetooth" android:required="true"/>
  <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
Neal Ratan
  • 111
  • 1
  • 3