Skip to content

Commit 86417b8

Browse files
adnreychdaadu
andauthored
feat(wifi_iot): Allows to change the default connection timeout from the application (#305)
* Allows to change the default connection timeout from the application * formatting * [dart] added `timeoutInSeconds` arg to `findAndConnect` method Co-authored-by: Harsh Bhikadia <harsh.bhikadiya@gmail.com>
1 parent 91c2b3f commit 86417b8

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

packages/wifi_iot/android/src/main/java/com/alternadom/wifiiot/WifiIotPlugin.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -889,8 +889,18 @@ public void run() {
889889
Boolean joinOnce = poCall.argument("join_once");
890890
Boolean withInternet = poCall.argument("with_internet");
891891
Boolean isHidden = poCall.argument("is_hidden");
892-
893-
connectTo(poResult, ssid, bssid, password, security, joinOnce, withInternet, isHidden);
892+
Integer timeoutInSeconds = poCall.argument("timeout_in_seconds");
893+
894+
connectTo(
895+
poResult,
896+
ssid,
897+
bssid,
898+
password,
899+
security,
900+
joinOnce,
901+
withInternet,
902+
isHidden,
903+
timeoutInSeconds);
894904
}
895905
}.start();
896906
}
@@ -1012,6 +1022,7 @@ public void run() {
10121022
String password = poCall.argument("password");
10131023
Boolean joinOnce = poCall.argument("join_once");
10141024
Boolean withInternet = poCall.argument("with_internet");
1025+
Integer timeoutInSeconds = poCall.argument("timeout_in_seconds");
10151026

10161027
String security = null;
10171028
List<ScanResult> results = moWiFi.getScanResults();
@@ -1026,7 +1037,16 @@ public void run() {
10261037
}
10271038
}
10281039

1029-
connectTo(poResult, ssid, bssid, password, security, joinOnce, withInternet, false);
1040+
connectTo(
1041+
poResult,
1042+
ssid,
1043+
bssid,
1044+
password,
1045+
security,
1046+
joinOnce,
1047+
withInternet,
1048+
false,
1049+
timeoutInSeconds);
10301050
}
10311051
}.start();
10321052
}
@@ -1256,7 +1276,8 @@ private void connectTo(
12561276
final String security,
12571277
final Boolean joinOnce,
12581278
final Boolean withInternet,
1259-
final Boolean isHidden) {
1279+
final Boolean isHidden,
1280+
final Integer timeoutInSeconds) {
12601281
final Handler handler = new Handler(Looper.getMainLooper());
12611282
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
12621283
final boolean connected =
@@ -1393,7 +1414,8 @@ public void onUnavailable() {
13931414
}
13941415
};
13951416

1396-
connectivityManager.requestNetwork(networkRequest, networkCallback, handler, 30 * 1000);
1417+
connectivityManager.requestNetwork(
1418+
networkRequest, networkCallback, handler, timeoutInSeconds * 1000);
13971419
}
13981420
}
13991421
}

packages/wifi_iot/lib/wifi_iot.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ class WiFiForIoTPlugin {
329329
bool joinOnce = true,
330330
bool withInternet = false,
331331
bool isHidden = false,
332+
int timeoutInSeconds = 30,
332333
}) async {
333334
// https://en.wikipedia.org/wiki/Service_set_(802.11_network)
334335
// According to IEEE Std 802.11, a SSID must be between 0 and 32 bytes
@@ -352,6 +353,7 @@ class WiFiForIoTPlugin {
352353
"join_once": joinOnce,
353354
"with_internet": withInternet,
354355
"is_hidden": isHidden,
356+
"timeout_in_seconds": timeoutInSeconds,
355357
"security": serializeNetworkSecurityMap[security],
356358
});
357359
} on MissingPluginException catch (e) {
@@ -457,11 +459,14 @@ class WiFiForIoTPlugin {
457459
///
458460
/// @returns True in case the requested network could be connected to, false
459461
/// otherwise.
460-
static Future<bool> findAndConnect(String ssid,
461-
{String? bssid,
462-
String? password,
463-
bool joinOnce = true,
464-
bool withInternet = false}) async {
462+
static Future<bool> findAndConnect(
463+
String ssid, {
464+
String? bssid,
465+
String? password,
466+
bool joinOnce = true,
467+
bool withInternet = false,
468+
int timeoutInSeconds = 30,
469+
}) async {
465470
// https://en.wikipedia.org/wiki/Service_set_(802.11_network)
466471
// According to IEEE Std 802.11, a SSID must be between 0 and 32 bytes
467472
// either with no encoding or UTF8-encoded.
@@ -485,6 +490,7 @@ class WiFiForIoTPlugin {
485490
"password": password?.toString(),
486491
"join_once": joinOnce,
487492
"with_internet": withInternet,
493+
"timeout_in_seconds": timeoutInSeconds,
488494
});
489495
} on MissingPluginException catch (e) {
490496
print("MissingPluginException : ${e.toString()}");

0 commit comments

Comments
 (0)