3

I am making an application which makes use of internet.

I am using WiFiInfo Class to know the status of network availble and using WifiManager to enable and manage Wi-fi connection if available.

But the result is Unknown ssid. Also I have modified AndroidManifest.xml to grant permission of android.permission.ACCESS_WIFI_STATE. Maybe someone of you guys can help me ... this is the code:

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 Answers3

0

It is strange that it would return unknown instead of null, but there is a bug documented on it here http://code.google.com/p/android/issues/detail?id=43336 since 1 year ago.

What is the detailed state and suplicant state?

Edit: Trying to make this more helpful see my answer here Get SSID when WIFI is connected for getting the wifi information when wifi is in the right state.

Community
  • 1
  • 1
Eric Woodruff
  • 6,380
  • 3
  • 36
  • 33
  • I am not OP, but I'm facing the same problem. `` is returned from wifiInfo.getSSID() . Adding ACCESS_NETWORK_STATE did not fix this. – zmbq Jan 27 '14 at 20:34
  • zmbq - Curious, are you still having this problem since I answered your question "Get SSID when WIFI is connected?" – Eric Woodruff Jan 31 '14 at 20:52
-1

EDIT: Make sure your wifi is turned on and connected to a network. Call getSSID(), and use the permission ACCESS_NETWORK_STATE.

EDIT2: I just checked one of my apps that does this and it has both ACCESS_WIFI_STATE and ACCESS_NETWORK_STATE.

Call getSSID() instead, like so:

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

This will return the name of the wifi network that the device is connected to. If the device is not connected to a wifi network, it will return unknown.

Adam
  • 959
  • 10
  • 23
  • @Eric Woodruff No it isn't. OP is calling the wrong method. He should call getSSID() instead of getIpAddress() and toString(); – Adam Feb 03 '14 at 22:08
  • That would still return the same "Unknown" value. – Eric Woodruff Feb 03 '14 at 22:12
  • The premise of the question is that he is connected "But the result is Unknown ssid" – Eric Woodruff Feb 03 '14 at 22:14
  • I agree that it's implied, but he doesn't actually state that in the question. In any case, he should ensure he's connected to WIFI (not just mobile data), he should call getSSID(), and he needs the permission ACCESS_WIFI_STATE. – Adam Feb 03 '14 at 22:26
-1

try with this:

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;
}
}
Fco P.
  • 2,486
  • 17
  • 20
  • How does this help get the SSID? – Eric Woodruff Feb 02 '14 at 20:57
  • By calling the second method you get the connection status, and if you are on WIFI, it adds the SSID (the connectedTo variable)... I have a little service showing a toast to know to which network I'm connected to without looking the settings. – Fco P. Feb 02 '14 at 21:48