Git Product home page Git Product logo

Comments (10)

jcarioti avatar jcarioti commented on July 20, 2024 1

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
        }

from react-native-wifi-reborn.

eliaslecomte avatar eliaslecomte commented on July 20, 2024

I assume with 'then I can get successful responses using fetch and RNFetchBlob' that you mean against a public url and not with your IoT-like device? What are your proceeding requests? To the same IoT-device or to an url on the internet?

By forceWifiUsage all of the requests your app triggers, are going to the IoT access point.
So if you want to call something on the IoT access point, and then do a regular api call to the internet you have to:

  1. forceWifiUsage(true)
  2. do api call against IoT AP
  3. forceWifiUsage(false)
  4. do other api calls (also, maybe disconnect from AP?)

from react-native-wifi-reborn.

jcarioti avatar jcarioti commented on July 20, 2024

I assume with 'then I can get successful responses using fetch and RNFetchBlob' that you mean against a public url and not with your IoT-like device? What are your proceeding requests? To the same IoT-device or to an url on the internet?

By forceWifiUsage all of the requests your app triggers, are going to the IoT access point.
So if you want to call something on the IoT access point, and then do a regular api call to the internet you have to:

  1. forceWifiUsage(true)
  2. do api call against IoT AP
  3. forceWifiUsage(false)
  4. do other api calls (also, maybe disconnect from AP?)

All requests are going to the IoT AP. I need to connect to the AP, make some API calls, and then disconnect without the OS disconnecting me automatically.

As of right now, I can't get requests to succeed if I force wifi usage before connecting to the AP.

Also, I cannot disconnect from the AP using the isRemoveWifiNetwork function. The device stays on the network until I force close the app.

from react-native-wifi-reborn.

eliaslecomte avatar eliaslecomte commented on July 20, 2024

You first need to connect to the AP, then you can forceWifiUsage.

isRemoveWifiNetwork on < Android 10 only works if the wifi network was not yet in the saved networks list. It can't remove a network that the user or another app created first.

from react-native-wifi-reborn.

jcarioti avatar jcarioti commented on July 20, 2024

You first need to connect to the AP, then you can forceWifiUsage.

That seems to be working. Thank you!

isRemoveWifiNetwork on < Android 10 only works if the wifi network was not yet in the saved networks list. It can't remove a network that the user or another app created first.

I'm on Android 10 and it won't disconnect immediately. When it does disconnect, it won't reconnect to the known, nearby home network afterwards. The phone just sits without a wifi connection until I force close the app. Once the app is closed, the phone will connect to the known home wifi again.

Is this the proper order to disconnect?:

await WifiManager.forceWifiUsage(false);
console.log('Disconnect from', this.state.ssid);
const disconnectSuccess = await WifiManager.isRemoveWifiNetwork(this.state.ssid);
console.log('Success', disconnectSuccess);

from react-native-wifi-reborn.

eliaslecomte avatar eliaslecomte commented on July 20, 2024

Yes the code looks correct to me. Did you test on multiple devices?

from react-native-wifi-reborn.

uranashel44 avatar uranashel44 commented on July 20, 2024

@eliaslecomte , my issue got the same with this issue.

from react-native-wifi-reborn.

jcarioti avatar jcarioti commented on July 20, 2024

Yes the code looks correct to me. Did you test on multiple devices?

I tested on a Pixel 3a. It didn't work, so I didn't move on to any other devices. I wrote my own using NativeBridge and it's working perfectly.

from react-native-wifi-reborn.

coffteam2020 avatar coffteam2020 commented on July 20, 2024

@jcarioti , can you please share your code?

from react-native-wifi-reborn.

github-actions avatar github-actions commented on July 20, 2024

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.

from react-native-wifi-reborn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.