Search code examples
cordovacordova-pluginswifi

wifi configuration and wifi validation in wifiwizard cordova


I'm trying to imply some functions in my WifiApp to enable the wifi once the app is started. So I wrote a function startWifi() and I'm getting the error message first "There is no wifi connection" after then my wifi is not getting enabled. I'm using wifiWizard plugin.

Here is my program WifiService.js

function startWifi(e){
        window.setTimeout(function(){
        WifiWizard.setWifiEnabled(e, win_wifi, fail_wifi);  
        }, 500);

app.js

$scope.startWifi = function(enabled){
        WifiService.startWifi(enabled);
        alert("StartWifi");
    }

And please tell me how to write validation to connect to the wifi after scanning the wifi networks.


Solution

  • This is the sample code I wrote after creating a bare-bone cordova project and adding wifiwizard plugin, to check whether wifi is enabled in the device and to enable the same if it is disabled.

    index.html

    <html>
        <head>            
            <meta name="format-detection" content="telephone=no">
            <meta name="msapplication-tap-highlight" content="no">
            <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
            <link rel="stylesheet" type="text/css" href="css/index.css">
            <title>Wifi Wizard</title>
        </head>
        <body>      
            <br>        
            <br>
            Start Wifi <input type="button" value="wifi" name="Wifi" id="wifi"/>     <br>       
            <script type="text/javascript" src="js/jquery.js"></script> 
            <script type="text/javascript" src="cordova.js"></script>
            <script type="text/javascript" src="js/app.js"></script>
        </body>
    </html>
    

    app.js

    $(document).ready(function() {
        document.addEventListener("deviceready", onDeviceReady, false);
    });
    
    function onDeviceReady() {      
         $('#wifi').click( function() 
            {   
                try {               
                    WifiWizard.isWifiEnabled(win, fail);
                }
                catch(err) {
                    alert("Plugin Error - " + err.message);
                }
    
            }); 
    
        function win(e) {
            if(e) {
                alert("Wifi enabled already");
            }
            else {
                WifiWizard.setWifiEnabled(true, winEnable, failEnable);
            }
    
        }
    
        function fail(e) {
            alert("Error checking Wifi status");
        }
    
        function winEnable(e) {
            alert("Wifi enabled successfully");
        }
    
        function failEnable(e) {
            alert("Error enabling Wifi ");
        }
    }
    

    Please ensure to include jquery library file in your html. You can also check out this SO Post to get more info on performing wifi scan using the plugin.

    Have tested the same in Android 6 device and it works fine.