flutternetwork / WiFiFlutter

Flutter plugin suite for various WiFi services.
https://wifi.flutternetwork.dev
290 stars 182 forks source link

WiFiForIoTPlugin.isConnected() - help #368

Closed Bojan227 closed 9 months ago

Bojan227 commented 9 months ago

Thank you :)

Here is the full code: Future connectToWifi( ConnectionInfoDto connectionInfoDto, String? deviceWifi) async { final currentSsid = await WiFiForIoTPlugin.getSSID();

if (Platform.isAndroid) {
  final androidInfo = await deviceInfo.androidInfo;
  final sdk = androidInfo.version.sdkInt;

  if (sdk < 33) {
    if (currentSsid != null) {
      final disconnected = await disconnectFromWifi(currentSsid);
      log("$disconnected disconnected");
    }

    if (deviceWifi != null) {
      await WiFiForIoTPlugin.removeWifiNetwork(deviceWifi);
      await WiFiForIoTPlugin.disconnect();

      await WiFiForIoTPlugin.forceWifiUsage(false);
    }
  }
}

final isConnected = await WiFiForIoTPlugin.isConnected();

final response = await WiFiForIoTPlugin.connect(
  connectionInfoDto.ssid,
  password: connectionInfoDto.password,
  security: connectionInfoDto.securityType == SecurityType.secured
      ? NetworkSecurity.WPA
      : NetworkSecurity.NONE,
);

log("$response response");
if (!response) {
  throw 'Failed to Connect';
}

if (response) await WiFiForIoTPlugin.forceWifiUsage(true);

}