connect static method

Future<bool> connect(
  1. String ssid, {
  2. String? bssid,
  3. String? password,
  4. NetworkSecurity security = NetworkSecurity.NONE,
  5. bool joinOnce = true,
  6. bool withInternet = false,
  7. bool isHidden = false,
  8. int timeoutInSeconds = 30,
})

Connect to the requested AP Wi-Fi network.

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

@param ssid The SSID of the network to connect to. In case multiple networks share the same SSID, which one is connected to is undefined. Use the optional bssid parameters if you want to specify the network. The SSID must be between 1 and 32 characters.

@param bssid The BSSID (unique id) of the network to connect to. This allows to specify exactly which network to connect to. 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. 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 joinOnce If true, the network will be removed on exit.

@param withInternet Whether the connected network has internet access. Android only.

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

@returns True in case the requested network could be connected to, 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.
///   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 [joinOnce] If true, the network will be removed on exit.
///
/// @param [withInternet] Whether the connected network has internet access.
///   Android only.
///
/// @param [isHidden] Whether the SSID is hidden (not broadcasted by the AP).
///
/// @returns True in case the requested network could be connected to, false
///   otherwise.
static Future<bool> connect(
  String ssid, {
  String? bssid,
  String? password,
  NetworkSecurity security = NetworkSecurity.NONE,
  bool joinOnce = true,
  bool withInternet = false,
  bool isHidden = false,
  int timeoutInSeconds = 30,
}) 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 {
    bResult = await _channel.invokeMethod('connect', {
      "ssid": ssid.toString(),
      "bssid": bssid?.toString(),
      "password": password?.toString(),
      "join_once": joinOnce,
      "with_internet": withInternet,
      "is_hidden": isHidden,
      "timeout_in_seconds": timeoutInSeconds,
      "security": serializeNetworkSecurityMap[security],
    });
  } on MissingPluginException catch (e) {
    print("MissingPluginException : ${e.toString()}");
  }
  return bResult ?? false;
}