Android WiFiInfo错误

时间:2013-05-05 19:42:37

标签: java android android-wifi

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

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

但结果是Unknown 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)
         );
   }
}

3 个答案:

答案 0 :(得分:0)

奇怪的是它会返回unknown而不是null,但是从1年前开始就记录了http://code.google.com/p/android/issues/detail?id=43336中的错误。

详细的州和支持州是什么?

编辑:试着让这个更有帮助,看看我的答案Get SSID when WIFI is connected,以便在wifi处于正确的状态时获取wifi信息。

答案 1 :(得分:-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网络,它将返回未知。

答案 2 :(得分:-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;
}
}