user3189916
user3189916

Reputation: 768

How to get wifi router's ssid using phonegap?

i am developing an android mobile app using phonegap and java. My requirement is to capture wifi router's ssid and store it to database.

Is there anyway to capture ssid?

Thanks in advance.

Upvotes: 0

Views: 1567

Answers (1)

Aravin
Aravin

Reputation: 4108

Please try the following(for android only). Include the following class into your src folder

WifiInfoPlugin.class:

package com.example.getmac;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.webkit.WebSettings.PluginState;

public class WifiInfoPlugin extends CordovaPlugin {
    public static final String SSID_NAME = "WifiInfo";

    @Override
    public boolean execute(String action, JSONArray args,
            CallbackContext callbackContext) throws JSONException {
        if (SSID_NAME.equals(action)) {
            String wifiInfo = this.getWifiInfo();
            Log.e("Wifi SSID", wifiInfo);
            if(wifiInfo != ""){
                JSONObject jsonResult = new JSONObject();
            try {
                    jsonResult.put("Wifi SSID", wifiInfo);
                    PluginResult r= new PluginResult(PluginResult.Status.OK,jsonResult);
                    callbackContext.success(wifiInfo);
                    r.setKeepCallback(true);
                    return true;

            } catch (JSONException e) {
                PluginResult r = new PluginResult(PluginResult.Status.JSON_EXCEPTION);
                callbackContext.error("error");
                r.setKeepCallback(true);
                callbackContext.sendPluginResult(r);
                return true;
            }
        }
        }
        return false;
    }

    private String getWifiInfo() {

        WifiManager manager = (WifiManager)this.cordova.getActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        //String address = info.getMacAddress();
        String address = info.getSSID ();
        Log.e("ssid address", address);
        return address;
    }

}

after that in your index.html script is like:

document.addEventListener("deviceready",onDeviceReady,false);

function onDeviceReady(){
     var success = function(result) { alert("The SSID is " + result); };
     var error = function(message) { alert("Oopsie! " + message); };
     WifiInfo.createEvent(success,error);
}

then create getWifiInfoFromPLT.js is like include this js in index page

var WifiInfo = {

createEvent : function(successCallback, failureCallback) {
    cordova.exec(successCallback, failureCallback, 'WifiInfoPlugin',
            'WifiInfo', []);
}
};

Add the below in your res/xml/cofig.xml folder

 <feature name="WifiInfoPlugin" >
    <param
        name="android-package"
        value="com.example.getWifiInfo.WifiInfoPlugin" >
    </param>
</feature>

and add the necessary permissons in your manifest. Let me know any difficulties

Sample Output: enter image description here

Upvotes: 3

Related Questions