0

My app needs to connect to an access point, and after the connection make an http GET to know the version of the device.

The connection to the AP works good.

When is time to make the http call, i always get check WS error: SocketException: Connection failed (OS Error: Network is unreachable, errno = 101), address = 192.168.4.1, port = 80 on phones building Android 11 or above.

That's my code (i have to retry the connection if it fails, for MAX_ATTEMPT times):

  Future<bool> checkWS() async {
    try {
      print("checkWS attempt #$attempt");

      final uri = Uri.http("192.168.4.1", "xxxxxxxx.xxx");
      print("GET $uri");

      final response = await http.get(uri).timeout(const Duration(seconds: 5));

      if (response.statusCode > 400) return false;

      return !response.body.contains("913.3.");
    } catch (e) {
      print("check WS error: $e");
      if (attempt < MAX_ATTEMPT) {
        return await Future.delayed(const Duration(seconds: 1), () {
          attempt++;
          return checkWS();
        });
      } else {
        throw TimeoutException(null);
      }
    }
  }

This is my AndroidManifefest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="dsafadgadfgdwgfsd">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.NEARBY_WIFI_DEVICES" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:label="xxxxxxx"
        android:usesCleartextTraffic="true"
        android:networkSecurityConfig="@xml/network_security_config">


        <!-- for http support-->
        <uses-library android:name="org.apache.http.legacy" android:required="false"/>

        <activity
            android:name=".MainActivity"
.....

network_security_config:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.4.1</domain>
    </domain-config>
</network-security-config>

How can i make it working on A11+?

giordy16
  • 275
  • 1
  • 12

0 Answers0