React-Native,BleManager "Native Module cannot be null"
React-Native, BleManager "Native Module cannot be null"
我想试用 Polidea 的 react-native-ble-plx 库
https://github.com/Polidea/react-native-ble-plx
所以我在终端中创建了一个新应用程序并按照 Polidea 的说明安装了库
create-react-native-app playground
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
我然后运行npm start
然后i
(用于在iOS模拟器中打开)
然后我得到以下错误(在终端和模拟器中)
- Native module cannot be null
- node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:24 in invariant
- node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:16 in NativeEventEmitter
- node_modules/react-native-ble-plx/src/BleManager.js:52:25 in BleManager
- App.js:8:19 in App
- node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1679:33 in
- node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1610:15 in measureLifeCyclePerf
- ... 52 more stack frames from framework internals
这是App.js中的代码:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {BleManager} from 'react-native-ble-plx';
//importing BleManager without calling new BleManager() works
export default class App extends React.Component {
constructor(){
super();
this.manager = new BleManager();
}
//calling new BleManager() leads to error
render() {
return (
<View style={styles.container}>
<Text>nothing, just sucking errors</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
这是文件package.json
{
"name": "playground",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.5.0",
"jest-expo": "^21.0.2",
"react-test-renderer": "16.0.0-alpha.12"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^21.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.48.4",
"react-native-ble-plx": "^0.6.3"
}
}
我已经寻求帮助并尝试过
npm -rm rf node_modules
npm install
但没有用。
这是我的系统信息:
macOS High Sierra
iMac (27-inch, Mid 2010)
Processor 2.8 GHz Intel Core i5
Memory 16 GB 1333 MHz DDR3
我在 iPhone(6s, 10.3.1) 中打开应用程序时也遇到了同样的错误(从 npm start 在终端中扫描二维码后)
其他没有react-native-ble-plx库的应用程序正常工作。
期待得到一些帮助。
提前致谢。
F运行k
您已经使用 create-react-native-app 创建了您的 React Native 项目。所以你不能在这个项目中使用 react-native-ble-plx 库。因为要访问蓝牙硬件,react-native-ble-plx 包有一些本地代码分别写在 android 和 ios 中。
如果您想构建您的应用程序,您需要从 create-react-native-app 中退出并使用 Xcode 和 Android Studio。
使用以下命令:--
npm install -g react-native-cli
npm run eject
react-native link react-native-ble-plx
- 第一个命令是全局安装 react-native-cli
- 第二个命令是将您的项目从 Expo 项目转换为常规 React Native 项目
- 第三个命令是 link react-native-ble-plx。因为很可能您之前的 link 失败了。
我所做的是(在我开始之前通过丢弃 git 更改完全删除 ble-plx 之后)
导航到 /node_modules/react-native-ble-plx/ios/BleClient.xcodeproj 和
- 我把它拉到项目导航器的 Libraries 文件夹中
- 然后我转到我的目标 -> 构建阶段 -> link 带有库的二进制文件并添加 libBleClient.a
我想试用 Polidea 的 react-native-ble-plx 库
https://github.com/Polidea/react-native-ble-plx
所以我在终端中创建了一个新应用程序并按照 Polidea 的说明安装了库
create-react-native-app playground
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
我然后运行npm start
然后i
(用于在iOS模拟器中打开)
然后我得到以下错误(在终端和模拟器中)
- Native module cannot be null
- node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:24 in invariant
- node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:16 in NativeEventEmitter
- node_modules/react-native-ble-plx/src/BleManager.js:52:25 in BleManager
- App.js:8:19 in App
- node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1679:33 in
- node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1610:15 in measureLifeCyclePerf
- ... 52 more stack frames from framework internals
这是App.js中的代码:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {BleManager} from 'react-native-ble-plx';
//importing BleManager without calling new BleManager() works
export default class App extends React.Component {
constructor(){
super();
this.manager = new BleManager();
}
//calling new BleManager() leads to error
render() {
return (
<View style={styles.container}>
<Text>nothing, just sucking errors</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
这是文件package.json
{
"name": "playground",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.5.0",
"jest-expo": "^21.0.2",
"react-test-renderer": "16.0.0-alpha.12"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^21.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.48.4",
"react-native-ble-plx": "^0.6.3"
}
}
我已经寻求帮助并尝试过
npm -rm rf node_modules
npm install
但没有用。
这是我的系统信息:
macOS High Sierra
iMac (27-inch, Mid 2010)
Processor 2.8 GHz Intel Core i5
Memory 16 GB 1333 MHz DDR3
我在 iPhone(6s, 10.3.1) 中打开应用程序时也遇到了同样的错误(从 npm start 在终端中扫描二维码后)
其他没有react-native-ble-plx库的应用程序正常工作。
期待得到一些帮助。
提前致谢。
F运行k
您已经使用 create-react-native-app 创建了您的 React Native 项目。所以你不能在这个项目中使用 react-native-ble-plx 库。因为要访问蓝牙硬件,react-native-ble-plx 包有一些本地代码分别写在 android 和 ios 中。
如果您想构建您的应用程序,您需要从 create-react-native-app 中退出并使用 Xcode 和 Android Studio。
使用以下命令:--
npm install -g react-native-cli
npm run eject
react-native link react-native-ble-plx
- 第一个命令是全局安装 react-native-cli
- 第二个命令是将您的项目从 Expo 项目转换为常规 React Native 项目
- 第三个命令是 link react-native-ble-plx。因为很可能您之前的 link 失败了。
我所做的是(在我开始之前通过丢弃 git 更改完全删除 ble-plx 之后)
导航到 /node_modules/react-native-ble-plx/ios/BleClient.xcodeproj 和
- 我把它拉到项目导航器的 Libraries 文件夹中
- 然后我转到我的目标 -> 构建阶段 -> link 带有库的二进制文件并添加 libBleClient.a