Cordova plugin for accessing Android WifiManager.
cordova plugin add cordova-plugin-android-wifi-manager
Access the plugin after the device is ready, exposed at window.cordova.plugins.WifiManager
.
var WifiManager = cordova.plugins.WifiManager
WifiManager.onwifistatechanged = function (data) {
console.log(data.previousWifiState, '->', data.wifiState)
}
// Turn on Wifi
WifiManager.setWifiEnabled(true, function (err, success) {
console.log(err, success)
})
All the exposed methods and events have matching WifiManager counterparts. The methods accept an optional callback as last argument, which is either called with an error object first, or with the value returned by the corresponding Java method.
Add a new network to the set of configured networks.
The method accepts a JavaScript object representing a WifiConfiguration Java instance.
Note that the Java bitsets are represented as objects where the keys match the corresponding WifiConfiguration constants, e.g. wifiConfiguration.allowedAuthAlgorithms.get(WifiConfiguration.AuthAlgorithm.LEAP)
maps to wifiConfiguration.allowedAuthAlgorithms.LEAP
in JavaScript
.
Example of a WifiConfiguration object.
{
BSSID: 'any',
SSID: 'my-ssid',
allowedAuthAlgorithms: {
LEAP: true,
OPEN: true,
SHARED: true
},
allowedGroupCiphers: {
CCMP: true,
TKIP: true,
WEP104: true,
WEP40: true
},
allowedKeyManagement: {
IEEE8021X: true,
NONE: true,
WPA_EAP: true,
WPA_PSK: true
},
allowedPairwiseCiphers: {
CCMP: true,
NONE: true,
TKIP: true
},
allowedProtocols: {
RSN: true,
WPA: true
},
hiddenSSID: false,
networkId: 0,
preSharedKey: 'psk',
status: 'ENABLED',
wepKeys: [
null,
null,
null,
null
],
wepTxKeyIndex: 0
}
Possible values for status: ENABLED
, DISABLED
or CURRENT
.
Disable a configured network.
Disassociate from the currently active access point.
Allow a previously configured network to be associated with.
Get a list of all configured networks. The callback recevies a list of WifiConfiguration objects.
Get information about the currently active WiFi connection. The callback receives a WifiInfo object.
Example of a WifiInfo object.
{
BSSID: 'any',
frequency: 2456,
hiddenSSID: false,
ipAddress: 2130706433,
linkSpeed: 2300,
macAddress: '00:14:22:01:23:45',
networkId: 0,
rssi: -15,
SSID: 'my-ssid',
supplicantState: 'COMPLETED'
}
See SupplicantState for possible values for the supplicantState property.
Get the assigned addresses from the last successfull DHCP request. The callback receives a DhcpInfo object.
Example of a DhcpInfo object.
{
dns1: 2130706433,
dns2: 2130706434,
gateway: 2130706433,
ipAddress: 2130706433,
leaseDuration: 60,
netmask: 4278190080,
serverAddress: 2130706433
}
Get the results from the latest WiFi scan. The callback receives a list of ScanResult objects.
Example of a ScanResult object.
{
BSSID: 'any',
SSID: 'my-ssid',
capabilities: '[WPA2-PSK-CCMP]',
centerFreq0: 80,
centerFreq1: 80,
channelWidth: '80MHZ',
frequency: 2456,
level: -15,
timestamp: 1507117436782
}
Possible values for channelWidth: 20MHZ
, 40MHZ
, 80MHZ
, 160MHZ
or 80MHZ_PLUS_MHZ
.
Get WiFi enabled state. Possible values are DISABLED
, DISABLING
, ENABLED
, ENABLING
and UNKNOWN
.
Check if scanning is always available.
Check if WiFi is either enabled or disabled.
Reconnect to the currently active network, even if we already connected.
Reconnect to the currently active network, if we are currently disconnected.
Remove a configured network.
Persist the current list of configured networks.
Enable or disable WiFi.
Request a scan for WiFi networks.
Update an already configured network.
The plugin also emits an event for each available broadcast intent action. The event callbacks are called with an object containing all the extra information from the intent.
The IDs of the configured networks might have changed.
WiFi connectivity changed. The callback receives a NetworkInfo object, a BSSID string and a WifiInfo object.
Example of a NetworkInfo object.
{
detailedState: 'AUTHENTICATING',
extraInfo: null,
reason: null,
state: 'CONNECTING',
subtype: 0,
subtypeName: '',
type: 'WIFI',
typeName: 'WIFI',
available: true,
connected: false,
connectedOrConnecting: true,
failover: false,
roaming: false
}
See NetworkInfo.DetailedState for possible values for the detailedState property, and NetworkInfo.State for possible values for the state property.
Network signal strength changed.
WiFi network scan completed. Results can be retrieved using the getScanResults
method.
Connection to the supplicant has been established or lost.
The state of establishing a connection to a network has changed. newState
contains the new SupplicantState value, and supplicantError
indicates an error (e.g. ERROR_AUTHENTICATING
).
WiFi state changed.
Called on all events with the event name (e.g. wifistatechanged
) and extra information.
Called on internal errors.
The Android API does not expose a way to modify the hotspot configurations. But it is possible using reflection to access private and undocumented methods of the WifiManager
instance. This approach is fragile and has some quirks, but has been tested to work with API version 23.
Get the current WifiConfiguration object for the hotspot.
Get WiFi access point state. Possible values are DISABLED
, DISABLING
, ENABLED
, ENABLING
and FAILED
.
Check if WiFi access point is either enabled or disabled.
Set the WiFi access point configuration.
Enable or disable WiFi access point with the given configuration.
Event triggered when the WiFi access point state changes.