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?