3

我正在制作一个利用互联网的应用程序。

我正在使用 WiFiInfo 类来了解可用网络的状态,并使用 WifiManager 来启用和管理 Wi-fi 连接(如果可用)。

但结果是未知的 ssid。我还修改了 AndroidManifest.xml 以授予 android.permission.ACCESS_WIFI_STATE 的权限。也许你们中的某个人可以帮助我......这是代码:

TextView textWifiManager = (TextView)findViewById(R.id.WifiManager);
       TextView textWifiInfo = (TextView)findViewById(R.id.WifiInfo);
       TextView textIp = (TextView)findViewById(R.id.Ip);

       WifiManager myWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

       WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
       int myIp = myWifiInfo.getIpAddress();

       textWifiManager.setText(myWifiManager.toString());
       textWifiInfo.setText(myWifiInfo.toString());

       int intMyIp3 = myIp/0x1000000;
       int intMyIp3mod = myIp%0x1000000;

       int intMyIp2 = intMyIp3mod/0x10000;
       int intMyIp2mod = intMyIp3mod%0x10000;

       int intMyIp1 = intMyIp2mod/0x100;
       int intMyIp0 = intMyIp2mod%0x100;

       textIp.setText(String.valueOf(intMyIp0)
         + "." + String.valueOf(intMyIp1)
         + "." + String.valueOf(intMyIp2)
         + "." + String.valueOf(intMyIp3)
         );
   }
}
4

3 回答 3

0

奇怪的是它会返回未知而不是 null,但是自 1 年前以来,这里http://code.google.com/p/android/issues/detail?id=43336上记录了一个错误。

详细状态和请求状态是什么?

编辑:试图让这更有帮助,请在此处查看我的答案连接 WIFI 时获取 SSID,以便在 wifi 处于正确状态时获取 wifi 信息。

于 2014-01-27T20:20:24.103 回答
-1

试试这个:

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {

public static int TYPE_WIFI = 1;
public static int TYPE_MOBILE = 2;
public static int TYPE_NOT_CONNECTED = 0;
public static String connectionStatus = "";
public static String connectionKind = "";
public static String connectedTo = "";


public static int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);        
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){
            try{
                connectedTo = activeNetwork.getExtraInfo().replace("\"", "");                   
            }
            catch(Exception WifiNet){
                connectedTo = "WiFi";                   
            }                               
            return TYPE_WIFI;             
        }
        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
            try{
                connectedTo = activeNetwork.getExtraInfo();
            }
            catch(Exception MobileNet){
                connectedTo = "2g/3g/LTE";
            }
            return TYPE_MOBILE;
        }
    }
    connectedTo = "N/A";
    return TYPE_NOT_CONNECTED;
}

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    try{
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled"+" "+connectedTo;            
            connectionKind = "Wifi";
        } else if (conn == NetworkUtil.TYPE_MOBILE) {
            status = "Mobile data enabled";
            connectionKind = "Mobile";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
            connectionKind = "N/A";
        }           
    }
    catch(Exception mobile){
        if (conn == NetworkUtil.TYPE_WIFI) {
            status = "Wifi enabled"+" "+connectedTo;            
            connectionKind = "Wifi";
        } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
            status = "Not connected to Internet";
            connectionKind = "N/A";
        }
    }        
    return status;
}
}
于 2014-02-01T01:46:40.950 回答
-1

编辑:确保您的 wifi 已打开并连接到网络。调用 getSSID(),并使用权限 ACCESS_NETWORK_STATE。

EDIT2:我刚刚检查了我的一个应用程序,它同时具有 ACCESS_WIFI_STATE 和 ACCESS_NETWORK_STATE。

改为调用 getSSID(),如下所示:

public String getConnectedNetworkSSID() {
    WifiManager wifiMgr = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
    String name = wifiInfo.getSSID();
    return name;
}

这将返回设备连接到的 wifi 网络的名称。如果设备未连接到 wifi 网络,它将返回未知。

于 2014-01-31T21:18:04.817 回答