registerWifiNetwork static method

Future<bool> registerWifiNetwork(
  1. String ssid, {
  2. String? bssid,
  3. String? password,
  4. NetworkSecurity security = NetworkSecurity.NONE,
  5. bool isHidden = false,
})

Register a network with the system in the device's wireless networks. Android only.

Once registered and connected, to route network traffic via the network use forceWifiUsage.

@param ssid The SSID of the network to register. The SSID must be between 1 and 32 characters.

@param bssid The BSSID (unique id) of the network to register. This allows to specify exactly which network to register in case of duplicated SSID. To obtain the BSSID, use loadWifiList (Android only) or save the value from a previous connection. On Android, specifying the BSSID will also result in no system message requesting permission being shown to the user when connecting. Does nothing on iOS.

@param password The password of the network. Should only be null in case security NetworkSecurity.NONE is used.

@param security The security type of the network. NetworkSecurity.NONE means no password is required. On Android, from version 10 (Q) onward, NetworkSecurity.WEP is no longer supported.

@param isHidden Whether the SSID is hidden (not broadcasted by the AP).

@returns True in case the requested network could be registered, false otherwise.

Implementation

// ignore: deprecated_member_use_from_same_package
///   To obtain the BSSID, use [loadWifiList] (Android only) or save the value
///   from a previous connection.
///   On Android, specifying the BSSID will also result in no system message
///   requesting permission being shown to the user when connecting.
///   Does nothing on iOS.
///
/// @param [password] The password of the network. Should only be null in case
///   [security] NetworkSecurity.NONE is used.
///
/// @param [security] The security type of the network. [NetworkSecurity.NONE]
///   means no password is required.
///   On Android, from version 10 (Q) onward, [NetworkSecurity.WEP] is no
///   longer supported.
///
/// @param [isHidden] Whether the SSID is hidden (not broadcasted by the AP).
///
/// @returns True in case the requested network could be registered, false
///   otherwise.
static Future<bool> registerWifiNetwork(
  String ssid, {
  String? bssid,
  String? password,
  NetworkSecurity security = NetworkSecurity.NONE,
  bool isHidden = false,
}) async {
  // https://en.wikipedia.org/wiki/Service_set_(802.11_network)
  // According to IEEE Std 802.11, a SSID must be between 0 and 32 bytes
  // either with no encoding or UTF8-encoded.
  // We do not accept 0 length SSID here since this is a probe request
  // (wildcard SSID), and thus does not have meaning in the context of
  // connecting to a specific network.
  // TODO: support any binary sequence as required instead of just strings.
  if (ssid.length == 0 || ssid.length > 32) {
    print("Invalid SSID");
    return false;
  }

  if (!Platform.isIOS && !await isEnabled()) await setEnabled(true);
  bool? bResult;
  try {
    await _channel.invokeMethod('registerWifiNetwork', {
      "ssid": ssid.toString(),
      "bssid": bssid?.toString(),
      "password": password?.toString(),
      "security": serializeNetworkSecurityMap[security],
      "is_hidden": isHidden,
    });
  } on MissingPluginException catch (e) {
    print("MissingPluginException : ${e.toString()}");
  }
  return bResult ?? false;
}