Skip to content

WiFi

onWifiConnected

TIP

The API usage is as follows: wx.onWifiConnected(function listener)

TIP

Support:

  • Android: Support
  • iOS: Support
  • Functional description: Listen for events connected to Wi-Fi
  • Parameters and descriptions: function listener,Listener function for Wi-Fi connected events, parameter Object res is as follows:
    PropertiesTypeDescription
    wifiWifiInfoWi-Fi information

offWifiConnected

TIP

The API usage is as follows: wx.offWifiConnected(function listener)

  • Functional description: Remove listener function for events connected to Wi-Fi

  • Parameters and descriptions: function listener,Listener function passed in by onWifiConnected. If this parameter is not passed, all listening functions will be removed

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onWifiConnected(listener)
wx.offWifiConnected(listener) // Pass the same function object used for listening.

stopWifi

TIP

The API usage is as follows: wx.stopWifi(Object object)

  • Functional description: Turn off Wi-Fi module

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Error code

    Error codeError messageDescription
    0okNormal
    12000not initThe startWifi interface was not called first
    12001system not supportThe current system does not support related capabilities
    12002password error Wi-FiWrong password
    12003connection timeoutConnection timeout, only supported by Android
    12004duplicate requestRepeatedly connect to Wi-Fi
    12005wifi not turned onAndroid-specific, Wi-Fi switch is not turned on
    12006gps not turned onAndroid-specific, GPS positioning switch is not turned on
    12007user deniedUser refuses to authorize Wi-Fi connection
    12008invalid SSIDInvalid SSID
    12009system config errSystem operator configuration refuses to connect to Wi-Fi
    12010system internal errorOther system errors, the specific error reason needs to be printed in errmsg
    12011weapp in backgroundThe application cannot configure Wi-Fi in the background
    12013wifi config may be expiredThe Wi-Fi configuration saved by the system is expired, it is recommended to forget Wi-Fi and try again, only supported by Android
    12014invalid WEP / WPA passwordiOS-specific, invalid WEP / WPA password
  • Sample code:

js
wx.stopWifi({
  success (res) {
    console.log(res.errMsg)
  }
})

startWifi

TIP

The API usage is as follows: wx.startWifi(Object object)

  • Functional description: Initialize Wi-Fi module

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • Error code

    Error codeError messageDescription
    0okNormal
    12000not initThe startWifi interface was not called first
    12001system not supportThe current system does not support related capabilities
    12002password error Wi-FiWrong password
    12003connection timeoutConnection timeout, only supported by Android
    12004duplicate requestRepeatedly connect to Wi-Fi
    12005wifi not turned onAndroid-specific, Wi-Fi switch is not turned on
    12006gps not turned onAndroid-specific, GPS positioning switch is not turned on
    12007user deniedUser refuses to authorize Wi-Fi connection
    12008invalid SSIDInvalid SSID
    12009system config errSystem operator configuration refuses to connect to Wi-Fi
    12010system internal errorOther system errors, the specific error reason needs to be printed in errmsg
    12011weapp in backgroundThe application cannot configure Wi-Fi in the background
    12013wifi config may be expiredThe Wi-Fi configuration saved by the system is expired, it is recommended to forget Wi-Fi and try again, only supported by Android
    12014invalid WEP / WPA passwordiOS-specific, invalid WEP / WPA password
  • Sample code:

js
wx.startWifi({
  success (res) {
    console.log(res.errMsg)
  }
})

onGetWifiList

TIP

The API usage is as follows: wx.onGetWifiList(function listener)

  • Functional description: Listen for events of getting Wi-Fi list data
  • Parameters and descriptions: function listener,Listening function for events that obtain Wi-Fi list data. Parameter Object res is as follows:
    PropertiesTypeDescription
    wifiListArray.<WifiInfo>Wi-Fi list data

offGetWifiList

TIP

The API usage is as follows: wx.offGetWifiList(function listener)

  • Functional description: Remove listener function for events of getting Wi-Fi list data

  • Parameters and descriptions: function listener,Listening function passed in by onGetWifiList. If this parameter is not passed, all listening functions will be removed

  • Sample code:

js
const listener = function (res) { console.log(res) }

wx.onGetWifiList(listener)
wx.offGetWifiList(listener) // Pass the same function object used for listening.

getConnectedWifi

TIP

The API usage is as follows: wx.getConnectedWifi(Object object)

  • Functional description: Get connected Wi-Fi information

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    partialInfobooleanfalseNoDo you need to return partial Wi-Fi information?
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: object res。

    PropertiesTypeDescription
    wifiWifiInfoWi-Fi information
  • Error code

    Error codeError messageDescription
    0okNormal
    12000not initThe startWifi interface was not called first
    12001system not supportThe current system does not support related capabilities
    12002password error Wi-FiWrong password
    12003connection timeoutConnection timeout, only supported by Android
    12004duplicate requestRepeatedly connect to Wi-Fi
    12005wifi not turned onAndroid-specific, Wi-Fi switch is not turned on
    12006gps not turned onAndroid-specific, GPS positioning switch is not turned on
    12007user deniedUser refuses to authorize Wi-Fi connection
    12008invalid SSIDInvalid SSID
    12009system config errSystem operator configuration refuses to connect to Wi-Fi
    12010system internal errorOther system errors, the specific error reason needs to be printed in errmsg
    12011weapp in backgroundThe application cannot configure Wi-Fi in the background
    12013wifi config may be expiredThe Wi-Fi configuration saved by the system is expired, it is recommended to forget Wi-Fi and try again, only supported by Android
    12014invalid WEP / WPA passwordiOS-specific, invalid WEP / WPA password

WifiInfo

  • Functional description: Wifi information

TIP

  • Android wx.connectWifi / wx.getConnectedWifi If partialInfo:true is set, or the wx.onWifiConnectedWithPartialInfo event is called, a WifiInfo object containing only the SSID attribute will be returned.
  • iOS wx.getConnectedWifi If partialInfo:true is set, a WifiInfo object containing only the SSID and BSSID attributes will be returned, and the user needs to enable the host client location permission to return the result correctly
  • In some cases, the Wi-Fi may have been successfully connected, but an error will be reported because the complete WifiInfo object cannot be obtained. The specific error message is errCode:12010, errMsg: can't gain current wifi or no wifi is connected. If the developer does not need a complete WifiInfo object, the error problem can be solved by adopting the above strategy

Properties

  • string SSID: Wi-Fi SSID
  • string BSSID: Wi-Fi BSSID
  • boolean secure: Is Wi-Fi safe
  • number signalStrength: Wi-Fi signal strength, Android takes values
  • number frequency: Wi-Fi frequency band unit MHz