'SocketException: Connection failed (OS Error: Network is unreachable, errno = 101) - Flutter (Android platform)

I have a Flutter project that needs to run on Android and IOS devices.

I am trying to write native Java code in order to connect to a specific Wi-Fi connection on Android 10 above.

Note: The connection is to a device without internet

I followed Android official documentation and implement the connection like that:

private void connectWiFi(String ssid, String bssid){
    final NetworkSpecifier specifier =
            new WifiNetworkSpecifier.Builder()
                    .setSsid(ssid)
                    .setBssid(MacAddress.fromString(bssid))
                    .build();

    final NetworkRequest request =
            new NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .setNetworkSpecifier(specifier)
                    .build();

    final ConnectivityManager connectivityManager = (ConnectivityManager)
            getApplicationContext().getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);

    final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);
        }

        @Override
        public void onUnavailable() {
            super.onUnavailable();
        }
    };
    connectivityManager.requestNetwork(request, networkCallback);
}

After running the code on a real device, I've got a "Successfully connected" OS popup

Now I am trying to perform a simple GET request using the Flutter "http" package.

Here is my code for the GET request:

Response response = await get(urlStartRecord).catchError((onError) async {
  myLog("Error in _sendStartRecord urlStartRecord", "error",
      exception: "Cannot start record: ${onError.toString()}");
}

When urlStartRecord is defined like that:

    final urlStartRecord = Uri.parse('http://192.168.0.1:12913/startRecord');

After running the GET request I am receiving the following error message:

Cannot start record: SocketException: Connection failed (OS Error: Network is unreachable, errno = 101), address = 192.168.0.1, port = 12913

What confuses me more is that if I connected manually to the same network, it works well.

I already searched on the web but nothing solve the problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source