1.0.0 • Published 5 years ago
react-native-wifi-transfer v1.0.0
react-native-wifi-transfer
手机端开启一个http服务器 可以接收局域网上传的文件 保存到app文档中 支持ios和android
Getting started
$ npm install react-native-wifi-transfer --save
Mostly automatic installation
$ react-native link react-native-wifi-transfer
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-wifi-transfer
and addRNWifiTransfer.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNWifiTransfer.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Run your project (
Cmd+R
)<
Android
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.zt5.wifitrans.RNWifiTransferPackage;
to the imports at the top of the file - Add
new RNWifiTransferPackage()
to the list returned by thegetPackages()
method
- Append the following lines to
android/settings.gradle
:include ':react-native-wifi-transfer' project(':react-native-wifi-transfer').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-wifi-transfer/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-wifi-transfer')
Usage
//导入模块
import RNWifiTransfer from 'react-native-wifi-transfer';
//接收新文件事件
RNWifiTransfer.Emitter.addListener(RNWifiTransfer.FILE_UPLOAD_NEW, ({name: string,path: string})=>{});
//开启服务器 直到找到可用端口
let port = 10000;
openConnect = () => {
console.warn("开启端口" + port);
RNWifiTransfer.start(port).then((ip) => {
console.warn("开启成功" + ip);
}).catch(err => {
//打开错误码
//1. wifi没有打开 RNWifiTransfer.ERROR_WIFI_NOT_OPEN
//2. 打开错误 打开连接错误 RNWifiTransfer.ERROR_CONNECT_OPEN
//3. 打开错误 端口被占用 RNWifiTransfer.ERROR_PORT_ALREADY_BIND
if (err.code === RNWifiTransfer.ERROR_PORT_ALREADY_BIND) {
const newPort = port + 1;
console.warn("端口" + port + "被占用,尝试" + newPort);
port = newPort;
//端口被占用了
setTimeout(openConnect, 300);
} else {
console.error(err);
}
});
};
openConnect();
//关闭服务器
RNWifiTransfer.close();