findAndConnect static method
Scan for Wi-Fi networks and connect to the requested AP Wi-Fi network if found. Android only.
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
the network is not password protected.
@param joinOnce
If true, the network will be removed on exit.
@param withInternet
Whether the connected network has internet access.
Android only.
@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
/// the network is not password protected.
///
/// @param [joinOnce] If true, the network will be removed on exit.
///
/// @param [withInternet] Whether the connected network has internet access.
/// Android only.
///
/// @returns True in case the requested network could be connected to, false
/// otherwise.
static Future<bool> findAndConnect(
String ssid, {
String? bssid,
String? password,
bool joinOnce = true,
bool withInternet = 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 (!await isEnabled()) {
await setEnabled(true);
}
bool? bResult;
try {
bResult = await _channel.invokeMethod('findAndConnect', {
"ssid": ssid.toString(),
"bssid": bssid?.toString(),
"password": password?.toString(),
"join_once": joinOnce,
"with_internet": withInternet,
"timeout_in_seconds": timeoutInSeconds,
});
} on MissingPluginException catch (e) {
print("MissingPluginException : ${e.toString()}");
}
return bResult ?? false;
}