Jump to content

Récupération de l'adresse IP d'un appareil


Azuken
 Share

Recommended Posts

Bonjour,

Je cherche à créer une application qui mettrais en place un serveur web sur le téléphone. Sur l'exemple que j'ai repris, il fait une récupération de l'adresse ip en Wifi.

(l'exemple : https://gist.github.com/1893396)

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
 int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
 final String formatedIpAddress = String.format("%d.%d.%d.%d",
   (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
   (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));

Sauf que n'étant pas connecté en wifi, j'obtient 0.0.0.0...

J'ai cherché sur le net une fonction qui permettrais de récupérer l'adresse IP quand on est en 3G, mais je n'ai rien trouvé...

Auriez vous une solution ?

Merci d'avance,

Azuken

Je viens de trouver la solution, j'ai réupéré d'un tutoriel une fonction.

public String getMonAdresseIP()
{
   try
   {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
    {
	    NetworkInterface intf = en.nextElement();
	    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
	    {
		    InetAddress inetAddress = enumIpAddr.nextElement();
		    if (!inetAddress.isLoopbackAddress())
			 return inetAddress.getHostAddress();
	    }
    }
   }
   catch (SocketException e)
   {
    e.printStackTrace();
   }
   return null;
}

Link to comment
Share on other sites

  • 4 weeks later...

Je viens de tomber là dessus par hasard en cherchant la même chose que toi chez moi ta solution ne marche pas, la méthode getMonAdresseIP() renvoie systématiquement null. Voici donc une autre solution :

public static String getIP(WifiManager wifiManager)
{
int myIp = wifiManager.getConnectionInfo().getIpAddress();

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

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

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

String s = String.valueOf(intMyIp0) + "." + String.valueOf(intMyIp1) + "." + String.valueOf(intMyIp2) + "." + String.valueOf(intMyIp3);


boolean chaineOK = true;
for (int i = 0; i < s.length(); i++)
if(s.charAt(i) != '.' && !Character.isDigit(s.charAt(i)))
chaineOK = false;
if (!chaineOK)
s = null;

return s;

}

Que tu appelle de cette façon dans ton Activity :

getIP((WifiManager) getSystemService(WIFI_SERVICE));

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...