智能客服
你问我答,随时在线为你解决问题
在manifest.json文件的 features属性中增加如下配置。
- {"name": "system.wifi"}
在调用接口页面的<script>部分增加如下配置。
- import wifi from '@system.wifi'
或
- var wifi = require("@system.wifi")
限制条件 | 说明 |
|---|---|
适用终端 | 手机、平板、智慧屏 |
适用区域 | 全球 |
在快应用中支持搜索周边的Wi-Fi,同时可以针对指定Wi-Fi,传入密码发起连接。
接口 | 描述 |
|---|---|
连接Wi-Fi。若已知Wi-Fi信息,可以直接调用此接口连接。 | |
请求获取Wi-Fi列表,在onscanned注册的回调中返回wifiList数据。 | |
监听获取Wi-Fi列表数据时的事件,在回调中将返回 wifiList。 | |
监听连接断开Wi-Fi的事件。 | |
获取已连接中的Wi-Fi信息。 |
描述
连接Wi-Fi。若已知Wi-Fi信息,可以直接调用此接口连接。
当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。
当系统版本为10.0或者以上版本时,调用此接口后,需要等待接到系统通知,并同意接入wifi后自动连接。
收到系统通知有如下限制:
如果未收到系统通知,需要用户进入系统设置手动链接Wi-Fi。
OBJECT参数
参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
SSID | string | 是 | Wi-Fi设备的ssid。 |
BSSID | string | 是 | Wi-Fi设备的bssid。 |
password | string | 是 | Wi-Fi设备的密码。 |
success | function | 否 | 接口调用成功的回调函数。 |
fail | function | 否 | 接口调用失败的回调函数。 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
示例代码
- wifi.connect({
- SSID:'',
- BSSID:'',
- password:'',
- success: function() {
- console.log('connect wifi success');
- },
- fail: function(errmsg, errorCode) {
- console.log('connect failed ' + errmsg+', error = '+errorCode);
- }
- })
描述
请求获取Wi-Fi列表,在onscanned注册的回调中返回wifiList数据。
当系统版本为6.0或者以上版本时,如果没有打开定位开关,将导致设备无法正常获取周边的Wi-Fi信息。同时,需要请求获取位置信息的权限。
OBJECT参数
参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
success | function | 否 | 接口调用成功的回调函数。 |
fail | function | 否 | 接口调用失败的回调函数。 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
示例代码
- wifi.scan({
- success:function() {
- console.log('scan wifi success');
- },
- fail: function(errmsg, errorCode) {
- console.log('scan failed ' + errmsg+', error = '+errorCode);
- }
- })
描述
监听获取Wi-Fi列表数据时的事件,在回调中将返回 wifiList。取消监听的方式:wifi.onscanned = null
当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。
OBJECT参数
参数 | 类型 | 说明 |
|---|---|---|
wifiList | array | Wi-Fi列表数据。 |
Wi-FI列表项说明:
参数 | 类型 | 说明 |
|---|---|---|
SSID | string | Wi-Fi的SSID。 |
BSSID | string | Wi-Fi的BSSID。 |
secure | boolean | Wi-Fi是否安全。 |
signalStrength | number | Wi-Fi信号强度。返回值为–100~0的整型数值:
|
示例代码
- wifi.onscanned = function(data) {
- console.log('scanned result data = '+ JSON.stringify(data));
- }
描述
监听连接断开Wi-Fi的事件。取消监听的方式:wifi.onstatechanged = null
当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。
OBJECT参数
参数 | 类型 | 说明 |
|---|---|---|
SSID | string | Wi-Fi的SSID。 |
BSSID | string | Wi-Fi的BSSID。 |
secure | boolean | Wi-Fi是否安全。 |
signalStrength | number | Wi-Fi信号强度。返回值为–100~0的整型数值:
|
state | number |
|
示例代码
- wifi.onstatechanged = function(data) {
- console.log(data.state+' ssid = '+data.SSID+', bssid = '+data.BSSID+', secure = '+data.secure+' signalStrength = '+data.signalStrength);
- }
描述
获取已连接中的Wi-Fi信息。
当系统版本为6.0及以上版本时,需要请求获取位置信息的权限。
OBJECT参数
参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
success | function | 否 | 接口调用成功的回调函数。 |
fail | function | 否 | 接口调用失败的回调函数。 |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
参数 | 类型 | 说明 |
|---|---|---|
SSID | string | Wi-Fi的SSID。 |
BSSID | string | Wi-Fi的BSSID。 |
secure | boolean | Wi-Fi是否安全。 |
signalStrength | number | Wi-Fi信号强度。返回值为–100~0的整型数值:
|
示例代码
- wifi.getConnectedWifi({
- success:function(ret) {
- console.log('ssid = '+ret.SSID+', BSSID = '+ ret.BSSID+', secure = '+ret.secure+', signalStrength = '+ret.signalStrength);
- },
- fail: function(errmsg, errorCode) {
- console.log('getConnectedWifi failed ')
- }
- })
- <template>
- <div class="container">
- <div class="item-content">
- <text class="txt">connectWifi:{{isSuccess}}</text>
- </div>
- <input type="button" class="btn" onclick="connectWifi" value="Connect Wifi" />
- <div class="item-content">
- <text class="txt">wifiResult:{{wifistate}}</text>
- </div>
- <input type="button" class="btn" onclick="onconnected" value="Wifi onstatechanged" />
- <input type="button" class="btn" onclick="cancelConnect" value="wifi cancelConnectlistening" />
- <div class="item-content">
- <text class="txt">wifi.scan:{{scan}}</text>
- </div>
- <input type="button" class="btn" onclick="scanWifi" value="Scan Wifi" />
- <div class="item-content">
- <text class="txt">wifi.scanResult:{{scanedresult}}</text>
- </div>
- <input type="button" class="btn" onclick="onscanned" value="wifi onscanned" />
- <input type="button" class="btn" onclick="cancelonScanned" value="wifi cancelonScanned" />
- <div class="item-content">
- <text class="txt">wifi.wifiinfo:{{wifiinfo}}</text>
- </div>
- <input type="button" class="btn" onclick="getConnectedWifiInfo" value="Wifi getConnectedWifiInfo" />
- </div>
- </template>
- <style>
- .container{
- flex: 1;
- flex-direction: column;
- }
- .item-container {
- margin-bottom: 50px;
- margin-right: 60px;
- margin-left: 60px;
- flex-direction: column;
- }
- .item-content {
- flex-direction: column;
- background-color: #ffffff;
- padding-left: 30px;
- padding-right: 30px;
- padding-top: 20px;
- padding-bottom: 20px;
- margin-bottom: 30px;
- align-items: flex-start;
- }
- .txt {
- padding-top: 15px;
- padding-bottom: 15px;
- }
- .btn {
- height: 80px;
- text-align: center;
- border-radius: 5px;
- margin-right: 60px;
- margin-left: 60px;
- margin-bottom: 50px;
- color: #ffffff;
- font-size: 30px;
- background-color: #0faeff;
- }
- </style>
- <script>
- import wifi from '@system.wifi'
- export default {
- data: {
- componentName: 'Wifi',
- isSuccess: '',
- wifistate: '',
- scan: '',
- scanedresult: '',
- wifiinfo: ''
- },
- onInit: function () {
- this.$page.setTitleBar({ text: 'Wifi' })
- },
- connectWifi: function () {
- var that = this
- wifi.connect({
- SSID: 'OPPO Reno7 5G',
- BSSID: '0e:71:f6:f1:b9:7a',
- password: 'wlj123456',
- success: function () {
- console.log('connect wifi success')
- that.isSuccess = 'Success'
- },
- fail: function (errmsg, errorCode) {
- console.log('connect---------failed ' + errmsg + ', error = ' + errorCode)
- that.isSuccess = 'Fail'
- }
- })
- },
- onconnected: function () {
- var that = this
- wifi.onstatechanged = function (ret) {
- that.wifistate = ret.state + ' ssid = ' + ret.SSID + ', bssid = ' + ret.BSSID + ', secure = ' + ret.secure + ' signalStrength = ' + ret.signalStrength
- }
- },
- cancelConnect: function () {
- wifi.onstatechanged = null
- },
- scanWifi: function () {
- var that = this
- wifi.scan({
- success: function () {
- that.scan = 'success'
- console.log('scan wifi success')
- },
- fail: function (errmsg, errorCode) {
- console.log('scan failed ' + errmsg + ', error = ' + errorCode)
- that.scan = 'fail'
- }
- })
- },
- onscanned: function () {
- var that = this
- wifi.onscanned = function (ret) {
- that.scanedresult = JSON.stringify(ret)
- console.log('scaned result success ret = ' + JSON.stringify(ret))
- }
- },
- cancelonScanned: function () {
- wifi.onscanned = null
- },
- getConnectedWifiInfo: function () {
- var that = this
- wifi.getConnectedWifi({
- success: function (ret) {
- that.wifiinfo = ret.SSID + ': BSSID' + ret.BSSID + ', secure = ' + ret.secure + ', signalStrength = ' + ret.signalStrength
- },
- fail: function (data, code) {
- console.log('getConnectedWifi failed ' + data)
- that.wifiinfo = 'fail'
- }
- })
- }
- }
- </script>
效果图如下:

错误码 | 描述 | 说明 |
|---|---|---|
1000 | password error | Wi-Fi密码错误。 |
1001 | connection timeout | 连接超时。 |
1002 | duplicate request | 重复连接 Wi-Fi。 |
1003 | wifi not turned on | 未打开Wi-Fi开关。 |
1004 | gps not turned on | 未打开GPS定位开关。 |
1005 | invalid SSID | 无效SSID。 |
500 | quickapp in background | 应用在后台无法进行操作。 |
版本 | 发布日期 | 描述 |
|---|---|---|
1010 | 2018-04-20 | 第一次正式发布。 |