Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.wifi"} Add the following configuration to the script element on the page where the API will be called:
import wifi from '@system.wifi'
Or
var wifi = require("@system.wifi") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, and Vision products |
Applicable regions | Global |
Searches for nearby Wi-Fi signals and sends a password to initiate a connection to a specified Wi-Fi hotspot.
API | Description |
|---|---|
Connects to a Wi-Fi hotspot. If the information about a Wi-Fi hotspot has been known, you can call this API to connect to it directly. | |
Obtains the Wi-Fi list. The wifiList parameter and its value will be returned in the callback function registered with the onscanned API. | |
Listens to events triggered when obtaining Wi-Fi list data. The wifiList parameter and its value will be returned in the callback function. | |
Listens to events of connecting to and disconnecting from Wi-Fi hotspots. | |
Obtains connected Wi-Fi information. |
Description
Connects to a Wi-Fi hotspot. If the information about a Wi-Fi hotspot has been known, you can call this API to connect to it directly.
For Android 6.0 or later, the permission for obtaining the location information is required.
For Android 10.0 or later, after this API is called, the connection is set up only when a system notification is received and the user agrees to connect to the Wi-Fi hotspot.
A system notification can be received only when the following conditions are met:
If no system notification is received, the user needs to manually connect to the Wi-Fi hotspot.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
SSID | string | Yes | Wi-Fi device SSID. |
BSSID | string | Yes | Wi-Fi device BSSID. |
password | string | Yes | Wi-Fi device password. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion, regardless of whether the API call is successful or fails. |
Sample code
wifi.connect({
SSID:'',
BSSID:'',
password:'',
success: function() {
console.log('connect wifi success');
},
fail: function(errmsg, errorCode) {
console.log('connect failed ' + errmsg+', error = '+errorCode);
}
}) Description
Obtains the Wi-Fi list. The wifiList parameter and its value will be returned in the callback function registered with the onscanned API.
For Android 6.0 or later, if the location function is disabled, the device cannot search for nearby Wi-Fi signals. In addition, the permission for obtaining the location information is required.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion, regardless of whether the API call is successful or fails. |
Sample code
wifi.scan({
success:function() {
console.log('scan wifi success');
},
fail: function(errmsg, errorCode) {
console.log('scan failed ' + errmsg+', error = '+errorCode);
}
}) Description
Listens to events triggered when obtaining Wi-Fi list data. The wifiList parameter and its value will be returned in the callback function. To cancel listening, use wifi.onscanned = null.
For Android 6.0 or later, the permission for obtaining the location information is required.
OBJECT parameters
Parameter | Type | Description |
|---|---|---|
wifiList | array | Wi-Fi list data. |
Parameters on the Wi-Fi list
Parameter | Type | Description |
|---|---|---|
SSID | string | Wi-Fi device SSID. |
BSSID | string | Wi-Fi device BSSID. |
secure | boolean | Indicates whether the Wi-Fi hotspot is secure. |
signalStrength | number | Wi-Fi signal strength. The return value is an integer ranging from –100 to 0.
|
Sample code
wifi.onscanned = function(data) {
console.log('scanned result data = '+ JSON.stringify(data));
} Description
Listens to events of connecting to and disconnecting from Wi-Fi hotspots. To cancel listening, use wifi.onstatechanged = null.
For Android 6.0 or later, the permission for obtaining the location information is required.
OBJECT parameters
Parameter | Type | Description |
|---|---|---|
SSID | string | Wi-Fi device SSID. |
BSSID | string | Wi-Fi device BSSID. |
secure | boolean | Indicates whether the Wi-Fi hotspot is secure. |
signalStrength | number | Wi-Fi signal strength. The return value is an integer ranging from –100 to 0.
|
state | number |
|
Sample code
wifi.onstatechanged = function(data) {
console.log(data.state+' ssid = '+data.SSID+', bssid = '+data.BSSID+', secure = '+data.secure+' signalStrength = '+data.signalStrength);
} Description
Obtains connected Wi-Fi information.
For Android 6.0 or later, the permission for obtaining the location information is required.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion, regardless of whether the API call is successful or fails. |
Parameters returned by success
Parameter | Type | Description |
|---|---|---|
SSID | string | Wi-Fi device SSID. |
BSSID | string | Wi-Fi device BSSID. |
secure | boolean | Indicates whether the Wi-Fi hotspot is secure. |
signalStrength | number | Wi-Fi signal strength. The return value is an integer ranging from –100 to 0.
|
Sample code
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> The following figure shows the final effect.

Result Code | Message | Description |
|---|---|---|
1000 | password error | Incorrect Wi-Fi password. |
1001 | connection timeout | Connection timed out. |
1002 | duplicate request | The device has already connected to the Wi-Fi hotspot. |
1003 | wifi not turned on | The Wi-Fi switch is turned off. |
1004 | gps not turned on | GPS positioning is disabled. |
1005 | invalid SSID | Invalid Wi-Fi device SSID. |
500 | quickapp in background | The app cannot be operated in the background. |