arsenal942 / Cordova-Network-Manager

Cordova Network Manager enables Wifi management for both Android and iOS applications within Cordova/Phonegap projects.
Other
16 stars 8 forks source link

androidConnectNetwork requires ssidToDisable #4

Closed tripflex closed 6 years ago

tripflex commented 6 years ago

Regarding the androidConnectToNetwork though, it looks like an SSID to disconnect from MUST be passed otherwise it will error out, so from what I can tell, that's not an "optional" argument, which shouldn't it be?

My thought on this would be to check if WiFi is already connected, and if so, get the current SSID, and use that for the currentNetworkId ... right?

    private boolean androidConnectNetwork(CallbackContext callbackContext, JSONArray data) {
        Log.d(TAG, "cordovaNetworkManager: connectNetwork entered.");
        if(!validateData(data)) {
            callbackContext.error("cordovaNetworkManager: connectNetwork invalid data");
            Log.d(TAG, "cordovaNetworkManager: connectNetwork invalid data.");
            return false;
        }
        String ssidToConnect = "";
        String currentSSID = "";

        try {
            ssidToConnect = data.getString(0);
            currentSSID = data.getString(1);
        }
        catch (Exception e) {
            callbackContext.error(e.getMessage());
            Log.d(TAG, e.getMessage());
            return false;
        }

        int networkIdToConnect = ssidToNetworkId(ssidToConnect);
        int currentNetworkId = ssidToNetworkId(currentSSID);

        if (networkIdToConnect >= 0 && currentNetworkId >= 0) {
            // We disable the network before connecting, because if this was the last connection before
            // a disconnect(), this will not reconnect.

            wifiManager.disableNetwork(currentNetworkId);
            wifiManager.enableNetwork(networkIdToConnect, true);

            SupplicantState supState;
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            supState = wifiInfo.getSupplicantState();

            callbackContext.success(supState.toString());
            return true;

        }else{
            callbackContext.error("cordovaNetworkManager: Cannot connect to network");
            return false;
        }
    }
tripflex commented 6 years ago

Right now this is what I have to do to get this to work correctly, which seems kinda overkill:

    let ssid = "some-ssid";
    cordovaNetworkManager.getCurrentSSID( function( currentSSID ){

        console.log( 'GET CURRENT SSID', currentSSID );

        cordovaNetworkManager.androidConnectNetwork(ssid, currentSSID, function(s){
            console.log('Connected to device WiFi...', s );
        }, function(f){
            console.log( 'Failed to connect to ', ssid );
        });

    }, function( f ){

        console.log( 'FAILURE GET CURRENT SSID', f );

    });

But that only works when connected to existing network, if not connected to existing network, it's not even possible to connect ....

tripflex commented 6 years ago

Here's something i found on parent repo regarding wifi enabled: https://github.com/hoerresb/WifiWizard/issues/6

arsenal942 commented 6 years ago

Like I've said before, I made this particular part for my personal application/use. It probably wasn't the best for a global solution and therefore was an oversight on my behalf. I need to parse in a very specific network to disable/enable and it isn't always the current network that's connected.

That being said, for a global solution, it would be best to do what you have described above. I am going away next week for 3 weeks however I will look to implement these changes before I go.

tripflex commented 6 years ago

Yeah completely understandable, I think I may actually just fork the main WiFiManager plugin and update that one, unless you want me to just submit a PR to you?

arsenal942 commented 6 years ago

I do not mind you submitting a PR. Simply follow the PR template and I'll review it all accordingly. :D

If you want to become a contributor to this plugin, let me know. I will be rewriting the Android platform since the OG WifiWizard is a bit trash and therefore the code in this is trash as well.

Contact me on Discord and we can talk about it more: Nomm#0176

Also see the open issue: https://github.com/arsenal942/Cordova-Network-Manager/issues/8

arsenal942 commented 6 years ago

Fixed as of release v2.2.0.

You can now just give it an empty string or a null object and it will still work.

If you notice the issue again, just create a new issue referencing this.