Wifi配网助手,参考了react-native-wifi-reborn的代码
npm install react-native-wifi-connect-helper
或者
yarn add react-native-wifi-connect-helper
需要在/android/app/src/main/AndroidManifest.xml
,加入一下权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 连接wifi需要 -->
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
开启 Access WIFI Information
和 Hotspot Configuration
(连接wifi需要)。
还需要在 info.plist
加入 Privacy - Location When In Use Usage Description
描述。
React Native 60+ 以上会自动连接,无需其它操作 🎉.
需要手动运行以下命令
react-native link react-native-wifi-connect-helper
- 打开XCode, 在项目导航器中, 右键
Libraries
➜Add Files to [your project's name]
- 打开
node_modules
➜react-native-wifi-connect-helper
然后添加WifiConnectHelper.xcodeproj
- 打开XCode, 在项目导航器中, 选择你的项目. 添加
libWifiConnectHelper.a
到你项目的Build Phases
中➜Link Binary With Libraries
- 运行你的项目 (
Cmd+R
)
- 打开
android/app/src/main/java/[...]/MainActivity.java
- 添加
import com.reactnativewificonnecthelper.WifiConnectHelperPackage;
到文件顶部 - 在
getPackages()
方法中添加new WifiConnectHelperPackage()
到列表中
- 添加下列代码到
android/settings.gradle
:
include ':react-native-wifi-connect-helper'
project(':react-native-wifi-connect-helper').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-wifi-connect-helper/android')
- 将下列代码添加到
android/app/build.gradle
的dependencies
块中:
implementation project(':react-native-wifi-connect-helper')
import {
getCurrentWifiSSID,
connectToProtectedSSID,
} from 'react-native-wifi-connect-helper';
// ...
connectToProtectedSSID(ssid, password, isWep).then(
() => {
console.log("连接成功!");
},
() => {
console.log("连接失败!");
}
);
getCurrentWifiSSID().then(
ssid => {
console.log("你当前连接的 wifi ssid 是" + ssid);
},
() => {
console.log("无法获取当前ssid!");
}
);
下载项目:
git clone https://github.com/wcly/react-native-wifi-connect-helper.git
cd react-native-wifi-connect-helper
yarn
yarn example android
yarn example ios
打开项目react-native-wifi-connect-helper
cd react-native-wifi-connect-helper
安装依赖:
yarn
运行 Android 例子:
yarn example android
运行 iOS 例子:
运行 iOS 例子:
yarn example ios
名称 | 描述 | 支持的平台 |
---|---|---|
connectToProtectedSSID | 连接wifi | ios, android |
getCurrentWifiSSID | 获取当前连接的wifi名称 | ios, android |
checkIsWifiEnable | 检查wifi开关是否打开 | ios, android |
checkIsGPSEnable | 检查定位服务是否打开 | ios, android |
connectToSSID | 连接到开放的wifi | ios |
connectToProtectedSSIDOnce | 连接到wifi,只连一次 | ios |
loadWifiList | 获取wifi列表 | android |
getCurrentSignalStrength | 获取当前连接的wifi信号强度 | android |
setEnabled | 设置wifi开关 | android |
openGPSSetting | 跳转到定位服务设置页面 | android |
openWifiSetting | 跳转到wifi设置页面 | android |
open | 跳转到任意页面 | android |
连接wifi,连接成功触发resolve,连接失败触发reject,返回错误。
需要连接的wifi网络SSID。
需要连接的wifi网路密码。
在iOS中使用,如果是true
,网络是WEP Wi-Fi类型;否则,它是WPA或WPA2个人Wi-Fi网络。
获取当前连接wifi的SSID。
检查wifi开关是否打开,打开返回true
,否则返回false
。
检查定位服务开关是否打开,打开返回true
,否则返回false
。
connectToProtectedSSIDOnce(SSID: string, password: string, isWEP: boolean, joinOnce: boolean): Promise
iOS上使用。可选参数。默认值为false
。当joinOnce设置为true
时,hotspot只在配置它的应用程序在前台运行时才会保持配置和连接。当发生以下事件时,热点断开并删除其配置:
-
该应用程序在后台停留超过15秒。
-
设备睡眠。
-
该应用程序崩溃,退出或已卸载。
-
该应用将设备连接到其他Wi-Fi网络。
-
用户将设备连接到其他Wi-Fi网络。
获取wifi列表。
SSID
: wifi名称。BSSID
: wifiBSSID。capabilities
: 描述访问点支持的身份验证,密钥管理和加密方案。frequency
: 通道的主要20 MHz频率(以MHz为单位),客户端通过该频率与接入点通信。level
: dBm中检测到的信号水平,也称为RSSI。timestamp
: 以微秒为单位的时间戳(从启动开始)最后一次看到这个结果的时间。
获取当前连接wifi的信号强度。
设置wifi开关状态。
跳转到定位服务设置页面。
跳转到wifi设置页面。
跳转到指定页面。
action的值参考:https://blog.csdn.net/LJX646566715/article/details/79445624
请参阅 贡献指南 以了解如何为库和开发工作流程做出贡献
MIT