@yoda/wifi

Provides classes to manage Wi-Fi functions on the device.

Classes

NetworkListener

Members

(inner) WIFI_INIVATE :number

wifi is invalid

(inner) WIFI_SCANING :number

wifi is scanning, just waiting

(inner) WIFI_CONNECTED :number

wifi is connected.

(inner) WIFI_UNCONNECTED :number

the wifi is disconnected.

(inner) NETSERVER_CONNECTED :number

the networking is connected.

(inner) NETSERVER_UNCONNECTED :number

the networking is disconnected.

Methods

(inner) joinNetwork(ssid, pskopt, methodopt) → {number}

Sends the WI-FI config to network service, which would try to join immediately. You may need to use getWifiState() or getNetworkState() to get if the network is connected in interval.

Parameters:
Name Type Attributes Default Description
ssid string

the wifi name

psk string <optional>

the wifi psk, an empty string or blanks would be ignored.

method string <optional>
WPA2PSK

the key method, available methods are: "WPA2PSK", "WPAPSK", "WEP", "NONE".

Throws:

ssid must be a string.

Type
Error
Returns:
Type:
number

the wpa_supplicant network id

Example
var wifi = require('@yoda/wifi')
wifi.joinNetwork(config.ssid, config.psk)
check()

function check () {
  var state = wifi.getWifiState()
  if (state === wifi.WIFI_CONNECTED) {
    console.log('wifi is connected')
  } else {
    setTimeout(check, 500) // check after 500ms again
  }
}

(inner) checkNetwork(timeout, callback)

This would check the current network.

Parameters:
Name Type Description
timeout number
callback function

(inner) getWifiState() → {number}

Get current wifi state.

Returns:
Type:
number

available numbers are "WIFI_INIVATE", "WIFI_SCANING", "WIFI_CONNECTED" and "WIFI_UNCONNECTED".

Example
var wifi = require('@yoda/wifi')
if (wifi.getWifiState() === wifi.WIFI_UNCONNECTED) {
  console.log('wifi is not connected')
}

(inner) getNetworkState() → {number}

Get current networking state.

Returns:
Type:
number

the same to getWifiState but for networking.

Example
var wifi = require('@yoda/wifi')
if (wifi.getNetworkState() === wifi.NETSERVER_CONNECTED) {
  console.log('network is connected')
}

(inner) getWifiList() → {Array.<module:@yoda/wifi~WifiInfo>}

Get the current wifi list, before fetching the list, you may need to call scan().

Returns:
Type:
Array.<module:@yoda/wifi~WifiInfo>

(inner) enableScanPassively() → {number}

Enable scaning the WI-FI passively, it starts to scan and connect from histroy list automatically once the connection state is disconnected.

Returns:
Type:
number

the status code.

(inner) disableAll() → {number}

Disable the WI-FI. It is to disconnect the current WI-FI, and doesn't do any scan after that.

Returns:
Type:
number

the status code.

(inner) resetDns() → {boolean}

Reset the DNS resolver, commonly it needs a call when network is connected.

Returns:
Type:
boolean

(inner) resetWifi() → {boolean}

Reset the WIFI, it removes all the history WIFI config.

Returns:
Type:
boolean

(inner) scan() → {boolean}

scan the wifi list, and use getWifiList() to get the results.

Returns:
Type:
boolean

(inner) save() → {boolean}

Save the current WI-FI config in local file, in usual /etc/wpa_supplicant.

Returns:
Type:
boolean

(inner) getNumOfHistory() → {number}

Get the number of the history.

Returns:
Type:
number

(inner) removeNetwork(id) → {number}

remove assign network depend on joinNetwork'id

Parameters:
Name Type Description
id int

joinNetwork return id

Returns:
Type:
number

(inner) removeAllNetworks() → {number}

remove all networks

Returns:
Type:
number

Type Definitions

WifiInfo

Describe the WI-FI information.

Properties:
Name Type Description
ssid string

The ssid of the router.

signal number

The signal of the router, it's range are (0, -100).