public class networkSetup
{
private static int MAX_SCAN_TRY = 3;
+ private static WiFiConnectionState connectionState = WiFiConnectionState.Disconnected;
private networkSetup()
{
}
+ 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 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()
{
Tizen.Log.Info(Globals.LogTag, "networkSetup.ForgetCurrentAP");
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.ConnectionStateChanged -= EventHandlerConnectionStateChangedCBState;
} // ConnectWiFi()
}
}