如何使用PhoneGap的获得无线路由器的SSID? [英] How to get wifi router's ssid using phonegap?

查看:238
本文介绍了如何使用PhoneGap的获得无线路由器的SSID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用的PhoneGap和Java的Andr​​oid手机应用程序。我的要求是捕捉到无线路由器的SSID,并将其存储到数据库中。

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.

反正是有捕捉 SSID

在此先感谢。

推荐答案

请尝试以下操作(适用于Android的唯一)。包括以下级到的src 文件夹

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

WifiInfoPlugin.class:

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;
    }

}

之后,在你的index.html的脚本是这样的:

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);
}

然后创建getWifiInfoFromPLT.js就像是包括这个js索引页面

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

var WifiInfo = {

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

添加下面的 RES / XML / cofig.xml 文件夹

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

和添加必要PERMISSONS在你的清单。让我知道,任何困难

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

示例输出:

Sample Output:

这篇关于如何使用PhoneGap的获得无线路由器的SSID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆