0

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.

Gandhi
  • 11,875
  • 4
  • 39
  • 63
learner
  • 177
  • 19

1 Answers1

1

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.

Community
  • 1
  • 1
Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • @RSN Once you get the list of SSIDs as per the link i mentioned, you can connect to any particular SSID using "WifiWizard.connectNetwork(SSID, win, fail);" – Gandhi Jun 13 '16 at 12:20
  • @RSN Have not tried it. But should work fine as its a plain javascript. – Gandhi Jun 13 '16 at 12:26
  • instead of wifi enabled already, can i try to disable the wifi using the code wifiwizard.setWifiEnabled(false), whether it works if i try – learner Jun 13 '16 at 12:35
  • @RSN Should work perfectly fine.. "WifiWizard.setWifiEnabled(false, win, fail);" – Gandhi Jun 13 '16 at 12:37
  • thanks, so wifiwizard.connectNetwork how does it work, whether it validates the network by itself(password or any key press) or we have to write it manually. – learner Jun 13 '16 at 12:39
  • @RSN you may have to try it out once. As far as i looked at the android source code of the plugin, i dont see any password validation. So it may work just with SSID prompting you to key in password. Not sure though. But could see password attribute in add network. – Gandhi Jun 13 '16 at 12:56
  • @RSN I feel you need not have to delete you comments for the benefit of others. Also good to see a BE student enthusiastic in coding. – Gandhi Jun 13 '16 at 12:57
  • im sorry its actually asking me to continue conversation in chat, thats why I did. And some wifi will be password protected. so when we try to connect I have to do a prompt window to accept the password right! and what is the mistake in my code above angularjs, I too wrote the code to autostart the wifi after 500ms, why didn't work? – learner Jun 13 '16 at 13:16
  • And in your code, you haven't mentioned about the wifi list which is the scan list – learner Jun 13 '16 at 13:24
  • @RSN I feel chat would be a better option. Anyways the code i wrote was just for wifi check and enabling it. You should invoke this code inside deviceready event listener – Gandhi Jun 13 '16 at 13:38
  • it says not enough reputation to chat, – learner Jun 13 '16 at 13:41
  • @RSN Oh i m sorry about that. But ensure that you have added cordova.js file in your html and invoking plugin code inside deviceready event listener – Gandhi Jun 13 '16 at 13:43
  • this event listener or the deviceready and the click function, how can i do that using angularjs, bcos my boss needs in angular+ionic – learner Jun 13 '16 at 13:51
  • @RSN If its Ionic, just ensure to call this plugin code inside platform.ready function. – Gandhi Jun 13 '16 at 13:53
  • im sorry where will i find this platform.ready function? is it possible for u to send a mail for me regarding this, because it will not allow me after sometime to chat here, whether i can i share my email address here – learner Jun 13 '16 at 14:00
  • @RSN You can share your's and delete it later once i have noted it – Gandhi Jun 13 '16 at 14:04
  • @RSN Noted the details – Gandhi Jun 13 '16 at 14:08