我想尝试一下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模拟器中打开)
然后我收到以下错误(在终端和模拟器中)
- 原生模块不能为空
- node_modules / react-native / node_modules / fbjs / lib / invariant.js:44:24 in invariant
- 在NativeEventEmitter中的node_modules / react-native / Libraries / EventEmitter / NativeEventEmitter.js:31:16
- 在BleManager中的node_modules / react-native-ble-plx / src / BleManager.js:52:25
- App.js:应用程序中的8:19
- 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个堆栈框架
以下是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英寸,2010年中)处理器2.8 GHz Intel Core i5
内存16 GB 1333 MHz DDR3
我在iPhone(6s,10.3.1)中打开应用程序时也遇到了同样的错误(从npm开始扫描终端中的QR码后)
没有react-native-ble-plx库的其他应用程序正常工作。
期待得到一些帮助
提前谢谢。
弗兰克
答案 0 :(得分:0)
您已使用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
答案 1 :(得分:0)
我所做的是(在开始之前通过丢弃git更改完全删除了ble-plx之后)
导航到/node_modules/react-native-ble-plx/ios/BleClient.xcodeproj和