因为我将有多个应用程序使用相同的实现连接到 BLE 设备,所以我试图将该代码移动到模块中。但是我遇到了一个奇怪的问题,即扫描代码在应用程序内的文件夹中起作用时,扫描代码在模块中不起作用。
我已经使用以下代码进行了测试:
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();
},
);
};
在模块内部时,根本没有发现任何设备,在应用程序中不是作为模块时,它按预期工作。
该模块将本机库作为对等依赖项:
"peerDependencies": {
"react-native": "^0.64.x",
"react-native-ble-plx": "^2.x"
},
该应用程序以这种方式包含模块
"dependencies": {
"device-control": "file:../device-control",
},
我已经验证我在应用程序的 node_modules 中有正确的代码。
让我更加困惑的是,在使用 react-native-ble-plx 的应用程序中,可以使用manager.onStateChange
.
我在 logcat 中没有发现错误,metro 中也没有错误。谁能告诉我错误的原因可能是什么?