question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

forceWifiUsage causes all network requests to fail

See original GitHub issue

I’m trying to connect to an IoT-like device that has a hotspot but does not have internet. If I don’t call forceWifiUsage(true), then I can get successful responses using fetch and RNFetchBlob. If I try to force wifi usage, however, all of my proceeding requests fail. When using fetch, I see TypeError: Network request failed and when using RNFetchBlob I’m seeing Error: socket failed: ENONET (Machine is not on the network).

I’m having a hell of a time trying to understand what’s happening. Any help would be greatly appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
jcarioticommented, May 26, 2020

Connecting and locking to a network:

        val hotspotSpecifier = WifiNetworkSpecifier.Builder()
                .setSsid(hotspotSSID);

        val hotspotConnectRequest = NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .setNetworkSpecifier(hotspotSpecifier.build())
                .build();

        connectivityCallback = object : ConnectivityManager.NetworkCallback() {
            override fun onAvailable(network: Network) {
                super.onAvailable(network);

                if (lock) {
                    // Lock the device to this network and force all network requests
                    connectivityManager.bindProcessToNetwork(network);
                    wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "WIFI_LOCK");
                }

                promise.resolve("SSID_CONNECTION_SUCCESS");
            }

            override fun onUnavailable() {
                super.onUnavailable();
                promise.reject("SSID_CONNECTION_FAILED", "Could not connect to $hotspotSSID");
            }

            override fun onLost(network: Network) {
                super.onLost(network)

                System.out.println("Lost network...")
                System.out.println(network.toString())
            }
        }

        connectivityManager.requestNetwork(hotspotConnectRequest, connectivityCallback);

Releasing a lock and disconnecting:

        try {
            connectivityManager.unregisterNetworkCallback(connectivityCallback)

            // Calling removeNetworkSuggestions with an empty list will remove
            // all network connections that have been suggested by this app.
            //
            // This should force a disconnect from the locked network and will
            // let the device go back to a known network.

            val networkSuggestions = listOf<WifiNetworkSuggestion>()
            wifiManager.removeNetworkSuggestions(networkSuggestions)

            if (::wifiLock.isInitialized && wifiLock.isHeld) {
                wifiLock.release();
            }

            connectivityManager.bindProcessToNetwork(null)

        } catch(e: Throwable){
            // Wifi Lock may have already been released at this point
        }
0reactions
github-actions[bot]commented, Jul 8, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community attention? This issue may be closed if no further activity occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flutter/Dart force request to use wifi, even with no internet
I'm sending requests to the local network device (with IP address in URL), but it fails randomly. I suspect Android tries cellular network...
Read more >
JuanSeBestia/react-native-wifi-reborn - GitHub
This will route all app network requests to the network (instead of the mobile connection). It is important to disable it again after...
Read more >
Flutter socket connect in Android 12 local IOT WiFi
forceWifiUsage(true); 4) Click connectSocket if it fails it will show socket exception on the top of the page. Feel free to check from...
Read more >
Handling “The network connection was lost” Errors
This error means that the underlying TCP connection that's carrying the HTTP request disconnected while the HTTP request was in progress ...
Read more >
Network Error Logging - HTTP - MDN Web Docs
In these examples, the entire reporting API payload is shown. The top-level "body" key contains the network error report. HTTP 400 (Bad Request) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found