我在使用 React Native 的 Android 上使用 USB 串口时遇到问题。 这是我的 App.jsx 代码:
import React, { useEffect } from 'react';
import { View, Text } from 'react-native';
import SerialPort from 'react-native-serialport';
const App = () => {
useEffect(() => {
SerialPort.read((data) => {
console.log('Received data:', data);
});
return () => {
SerialPort.close();
};
}, []);
return (
<View>
<Text>Serial Reader</Text>
</View>
);
};
export default App;
如果我在 Android 上运行它会显示错误
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':tasks'.
> Could not create task ':react-native-usb-serialport:compileDebugJavaWithJavac'.
> In order to compile Java 9+ source, please set compileSdkVersion to 30 or above
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 10s
我尝试了很多串口包,但没有发现任何有用的东西
在您的项目中,nodemodules->react-native-serialport-> android-> build.gradle 文件将compileSdkVersion更改为30..默认为28或29..对我有用