public class networkSetup
{
private static int MAX_SCAN_TRY = 3;
+ private static bool scanning = false;
+ private static WiFiConnectionState connectionState = WiFiConnectionState.Disconnected;
private networkSetup()
{
}
+ private static void EventHandlerScanState(object sender, ScanStateChangedEventArgs State)
+ {
+ if (State.State == WiFiScanState.Scanning)
+ {
+ Tizen.Log.Info(Globals.LogTag, "ScanState is changed: Scanning");
+ scanning = true;
+ }
+ else
+ {
+ Tizen.Log.Info(Globals.LogTag, "ScanState is changed: Done");
+ scanning = false;
+ }
+ }
+
+ private static void EventHandlerConnectionStateChangedCBState(object sender, ConnectionStateChangedEventArgs e)
+ {
+ connectionState = e.State;
+ Tizen.Log.Info(Globals.LogTag, "ConnectionState is changed: " + connectionState.ToString());
+ }
+
+ private static async Task waitScanning()
+ {
+ int count = 0;
+ while (true)
+ {
+ await Task.Delay(1000);
+ count++;
+ if (!scanning)
+ {
+ Tizen.Log.Info(Globals.LogTag, "Scan Done");
+ break;
+ }
+
+ if (count == 30)
+ {
+ Tizen.Log.Info(Globals.LogTag, "Scanning timeout is reached");
+ break;
+ }
+ }
+ }
+
+ private static async Task waitConnecting()
+ {
+ int count = 0;
+ while (true)
+ {
+ await Task.Delay(1000);
+ count++;
+ if (connectionState == WiFiConnectionState.Connected)
+ {
+ Tizen.Log.Info(Globals.LogTag, "Connected");
+ break;
+ }
+
+ if (count == 30)
+ {
+ Tizen.Log.Info(Globals.LogTag, "connecting timeout is reached");
+ break;
+ }
+ }
+ }
public static async Task ForgetCurrentAP()
{
}
}
-
private static WiFiAP FindAP(string WiFiName)
{
Tizen.Log.Info(Globals.LogTag, "Find AP " + WiFiName + " from scan results");
return null;
}
+ public static async Task waitBgScan()
+ {
+ WiFiManager.ScanStateChanged += EventHandlerScanState;
+ await waitScanning();
+ WiFiManager.ScanStateChanged -= EventHandlerScanState;
+ Tizen.Log.Info(Globals.LogTag, "bg scan is done");
+ }
+
/* Connect to a WANTED AP */
public static async Task ConnectWiFi(string WiFiName, networkParameter.wType type, string WiFiPass)
{
}
Tizen.Log.Info(Globals.LogTag, "No connected AP");
+ WiFiManager.ScanStateChanged += EventHandlerScanState;
+
/**
* Find AP from scan results
* If it fails, scan Wi-Fi APs maximum three times
if (ap == null)
{
Tizen.Log.Error(Globals.LogTag, "AP " + WiFiName + " is not found");
+ WiFiManager.ScanStateChanged -= EventHandlerScanState;
return;
}
/**
- * Auto-connection might occurs after scan.
- * Check connection state before requesting connect to AP.
+ * Sometimes, bg scan can occurs.
+ * To avoid it, wait until finishing scan.
*/
- if (WiFiManager.ConnectionState == WiFiConnectionState.Connected)
- {
- Tizen.Log.Info(Globals.LogTag, "Already connected");
- return;
- }
+ await waitScanning();
// Connect to AP
Tizen.Log.Info(Globals.LogTag, "Set passphrase " + WiFiPass);
ap.SecurityInformation.SetPassphrase(WiFiPass);
+ WiFiManager.ConnectionStateChanged += EventHandlerConnectionStateChangedCBState;
try
{
await ap.ConnectAsync();
Tizen.Log.Info(Globals.LogTag, "Connected");
}
+ catch(NowInProgressException)
+ {
+ Tizen.Log.Info(Globals.LogTag, "Connecting..");
+ await waitConnecting();
+ }
catch (System.Exception e)
{
- Tizen.Log.Info(Globals.LogTag, "Exception during Wi-Fi Association: " + e);
- await ap.ConnectAsync();
+ if (WiFiManager.ConnectionState == WiFiConnectionState.Connected)
+ {
+ Tizen.Log.Info(Globals.LogTag, "Already connected");
+ }
+ else
+ {
+ Tizen.Log.Info(Globals.LogTag, "Exception during Wi-Fi Association: " + e);
+ await ap.ConnectAsync();
+ }
}
+ WiFiManager.ScanStateChanged -= EventHandlerScanState;
+ WiFiManager.ConnectionStateChanged -= EventHandlerConnectionStateChangedCBState;
} // ConnectWiFi()
}
}