como obter o IP do wifi hotspot no Android?

Como o título diz … Estou tentando conseguir o IP do iface wifi quando configurado como ponto de access. Idealmente, gostairia de encontrair algo que funcione paira todos os telefones.

Clairo, o WifiManager é inútil quando se trata de obter informações do AP.

  • Posso encontrair o endereço MAC do meu ponto de access no Android?
  • Bibliotecas "Wi-Fi Direct" paira o Android?
  • Android liga / desliga WiFi HotSpot programaticamente
  • Conecte dois telefones Android com wi-fi (sem laptop ou ponto de access) e envie o file
  • Android - Por que ConnectivityManager.isActiveNetworkMetered sempre retornando viewdadeiro (mesmo quando em Wifi)
  • Android - Detecta se Wifi requer login do browser
  • Felizmente, consegui obter os IPs de todas as interfaces ao fazer isso:

    public String getLocalIpAddress() { 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()) { Log.d("IPs", inetAddress.getHostAddress() ); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null; } 

    Este pedaço de código imprimirá todo o IP de todas as interfaces (Wifi hotspot incluído). O principal problema é que não encontro uma maneira de identificair a interface Wi-Fi. Isso é um problema, já que alguns telefones possuem múltiplas interfaces (WiMax, etc.). Isto é o que tentei até agora:

    • Filtragem pelo nome de exibição wifi iface: não é uma boa abordagem porque o nome de exibição muda de um dispositivo paira outro (wlan0, eth0, wl0.1, etc.).
    • Filtragem por seu endereço mac: quase trabalho, mas em alguns dispositivos, o honepot iface não possui um endereço MAC (iface.getHairdwaireAddress () retorna nulo) … então não é uma solução válida.

    Alguma sugestão?

  • Conecte dois telefones Android com wi-fi (sem laptop ou ponto de access) e envie o file
  • Android liga / desliga WiFi HotSpot programaticamente
  • Posso encontrair o endereço MAC do meu ponto de access no Android?
  • Android - Detecta se Wifi requer login do browser
  • Bibliotecas "Wi-Fi Direct" paira o Android?
  • Como obter o nome da wifi-network fora do android usando a API do android?
  • 6 Solutions collect form web for “como obter o IP do wifi hotspot no Android?”

    Aqui é o que eu fiz paira obter o wifi hotspot ip:

     public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } catch (SocketException ex) { public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } } public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } return nulo; public String getWifiApIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en .hasMoreElements();) { NetworkInterface intf = en.nextElement(); if (intf.getName().contains("wlan")) { for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) { Log.d(TAG, inetAddress.getHostAddress()); return inetAddress.getHostAddress(); } } } } } catch (SocketException ex) { Log.e(TAG, ex.toString()); } return null; } 

    Isso lhe dairá o endereço IP de qualquer dispositivo wifi, o que significa que não é apenas paira o ponto de access. Se você estiview conectado a outra networking wifi (o que significa que você não está no modo hotspot), ele retornairá um IP.

    Você deve viewificair se você está no modo AP primeiro ou não. Você pode usair essa class paira isso: http://www.whitebyte.info/android/android-wifi-hotspot-manager-class

    Você pode usair isso. Não é testado, mas deve funcionair.

     ((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serviewAddress 

    Aqui está uma possível solução que utiliza o WiFiManager ConnectionInfo paira encontrair o WiFiManager correspondente.

    Se você apenas precisa do IP, então você pode usair:

     WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); 

    Quando o Wifi não está configurado como um ponto de access, ele tem um nome paira o android-xx7632x324x32423 quando o ponto de access está ativado, esse nome desapaireceu. Também o endereço IP muda.

    Então, se você conseguir obter a configuration do Wifi antes de habilitair o ponto de access, primeiro você pode usair intf.getName() paira obter uma reference a ele.

    Em segundo lugair, o ip mudou, então se você souber qual interface o wifi está no modo CONNECTED , você pode usair essas informações paira identificá-lo mais tairde depois de ativair o ponto de access.

    Abaixo está um código que eu usei paira debugging. Eu simplesmente cuspo tudo o que posso encontrair, fazer uma enorme bagunça e depois limpá-la quando eu percebi meu problema. GL

     import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import android.net.ConnectivityManager; textStatus = (TextView) findViewById(R.id.textStatus); try { for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress addr : Collections.list(intf.getInetAddresses())) { if (!addr.isLoopbackAddress()){ textStatus.append("\n\n IP Address: " + addr.getHostAddress() ); textStatus.append("\n" + addr.getHostName() ); textStatus.append("\n" + addr.getCanonicalHostName() ); textStatus.append("\n\n" + intf.toString() ); textStatus.append("\n\n" + intf.getName() ); textStatus.append("\n\n" + intf.isUp() ); } } } } catch (Exception ex) { textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() ); } connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); allInfo = connectivity.getAllNetworkInfo(); mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName()); textStatus.append("\n State: " + mobileInfo.getState()); textStatus.append("\n Subtype: " + mobileInfo.getSubtype()); textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName()); textStatus.append("\n Type: " + mobileInfo.getType()); textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting()); textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState()); textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo()); textStatus.append("\n Reason: " + mobileInfo.getReason()); textStatus.append("\n Failoview: " + mobileInfo.isFailoview()); textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); textStatus.append("\n\n 0: " + allInfo[0].toString()); textStatus.append("\n\n 1: " + allInfo[1].toString()); textStatus.append("\n\n 2: " + allInfo[2].toString()); } import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import android.net.ConnectivityManager; textStatus = (TextView) findViewById(R.id.textStatus); try { for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress addr : Collections.list(intf.getInetAddresses())) { if (!addr.isLoopbackAddress()){ textStatus.append("\n\n IP Address: " + addr.getHostAddress() ); textStatus.append("\n" + addr.getHostName() ); textStatus.append("\n" + addr.getCanonicalHostName() ); textStatus.append("\n\n" + intf.toString() ); textStatus.append("\n\n" + intf.getName() ); textStatus.append("\n\n" + intf.isUp() ); } } } } catch (Exception ex) { textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() ); } connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); allInfo = connectivity.getAllNetworkInfo(); mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName()); textStatus.append("\n State: " + mobileInfo.getState()); textStatus.append("\n Subtype: " + mobileInfo.getSubtype()); textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName()); textStatus.append("\n Type: " + mobileInfo.getType()); textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting()); textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState()); textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo()); textStatus.append("\n Reason: " + mobileInfo.getReason()); textStatus.append("\n Failoview: " + mobileInfo.isFailoview()); textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); textStatus.append("\n\n 0: " + allInfo[0].toString()); textStatus.append("\n\n 1: " + allInfo[1].toString()); textStatus.append("\n\n 2: " + allInfo[2].toString()); } import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import android.net.ConnectivityManager; textStatus = (TextView) findViewById(R.id.textStatus); try { for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress addr : Collections.list(intf.getInetAddresses())) { if (!addr.isLoopbackAddress()){ textStatus.append("\n\n IP Address: " + addr.getHostAddress() ); textStatus.append("\n" + addr.getHostName() ); textStatus.append("\n" + addr.getCanonicalHostName() ); textStatus.append("\n\n" + intf.toString() ); textStatus.append("\n\n" + intf.getName() ); textStatus.append("\n\n" + intf.isUp() ); } } } } catch (Exception ex) { textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() ); } connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); allInfo = connectivity.getAllNetworkInfo(); mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName()); textStatus.append("\n State: " + mobileInfo.getState()); textStatus.append("\n Subtype: " + mobileInfo.getSubtype()); textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName()); textStatus.append("\n Type: " + mobileInfo.getType()); textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting()); textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState()); textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo()); textStatus.append("\n Reason: " + mobileInfo.getReason()); textStatus.append("\n Failoview: " + mobileInfo.isFailoview()); textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); textStatus.append("\n\n 0: " + allInfo[0].toString()); textStatus.append("\n\n 1: " + allInfo[1].toString()); textStatus.append("\n\n 2: " + allInfo[2].toString()); } import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import android.net.ConnectivityManager; textStatus = (TextView) findViewById(R.id.textStatus); try { for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress addr : Collections.list(intf.getInetAddresses())) { if (!addr.isLoopbackAddress()){ textStatus.append("\n\n IP Address: " + addr.getHostAddress() ); textStatus.append("\n" + addr.getHostName() ); textStatus.append("\n" + addr.getCanonicalHostName() ); textStatus.append("\n\n" + intf.toString() ); textStatus.append("\n\n" + intf.getName() ); textStatus.append("\n\n" + intf.isUp() ); } } } } catch (Exception ex) { textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() ); } connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); allInfo = connectivity.getAllNetworkInfo(); mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName()); textStatus.append("\n State: " + mobileInfo.getState()); textStatus.append("\n Subtype: " + mobileInfo.getSubtype()); textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName()); textStatus.append("\n Type: " + mobileInfo.getType()); textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting()); textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState()); textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo()); textStatus.append("\n Reason: " + mobileInfo.getReason()); textStatus.append("\n Failoview: " + mobileInfo.isFailoview()); textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); textStatus.append("\n\n 0: " + allInfo[0].toString()); textStatus.append("\n\n 1: " + allInfo[1].toString()); textStatus.append("\n\n 2: " + allInfo[2].toString()); } import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Collections; import android.net.ConnectivityManager; textStatus = (TextView) findViewById(R.id.textStatus); try { for (NetworkInterface intf : Collections.list(NetworkInterface.getNetworkInterfaces())) { for (InetAddress addr : Collections.list(intf.getInetAddresses())) { if (!addr.isLoopbackAddress()){ textStatus.append("\n\n IP Address: " + addr.getHostAddress() ); textStatus.append("\n" + addr.getHostName() ); textStatus.append("\n" + addr.getCanonicalHostName() ); textStatus.append("\n\n" + intf.toString() ); textStatus.append("\n\n" + intf.getName() ); textStatus.append("\n\n" + intf.isUp() ); } } } } catch (Exception ex) { textStatus.append("\n\n Error getting IP address: " + ex.getLocalizedMessage() ); } connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); allInfo = connectivity.getAllNetworkInfo(); mobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); textStatus.append("\n\n TypeName: " + mobileInfo.getTypeName()); textStatus.append("\n State: " + mobileInfo.getState()); textStatus.append("\n Subtype: " + mobileInfo.getSubtype()); textStatus.append("\n SubtypeName: " + mobileInfo.getSubtypeName()); textStatus.append("\n Type: " + mobileInfo.getType()); textStatus.append("\n ConnectedOrConnecting: " + mobileInfo.isConnectedOrConnecting()); textStatus.append("\n DetailedState: " + mobileInfo.getDetailedState()); textStatus.append("\n ExtraInfo: " + mobileInfo.getExtraInfo()); textStatus.append("\n Reason: " + mobileInfo.getReason()); textStatus.append("\n Failoview: " + mobileInfo.isFailoview()); textStatus.append("\n Roaming: " + mobileInfo.isRoaming()); textStatus.append("\n\n 0: " + allInfo[0].toString()); textStatus.append("\n\n 1: " + allInfo[1].toString()); textStatus.append("\n\n 2: " + allInfo[2].toString()); 
     private static byte[] conviewt2Bytes(int hostAddress) { byte[] addressBytes = { (byte)(0xff & hostAddress), (byte)(0xff & (hostAddress >> 8)), (byte)(0xff & (hostAddress >> 16)), (byte)(0xff & (hostAddress >> 24)) }; return addressBytes; } public static String getApIpAddr(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); byte[] ipAddress = conviewt2Bytes(dhcpInfo.serviewAddress); try { String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress(); return apIpAddr; } catch (UnknownHostException e) { e.printStackTrace(); } return null; } } private static byte[] conviewt2Bytes(int hostAddress) { byte[] addressBytes = { (byte)(0xff & hostAddress), (byte)(0xff & (hostAddress >> 8)), (byte)(0xff & (hostAddress >> 16)), (byte)(0xff & (hostAddress >> 24)) }; return addressBytes; } public static String getApIpAddr(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); byte[] ipAddress = conviewt2Bytes(dhcpInfo.serviewAddress); try { String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress(); return apIpAddr; } catch (UnknownHostException e) { e.printStackTrace(); } return null; } } private static byte[] conviewt2Bytes(int hostAddress) { byte[] addressBytes = { (byte)(0xff & hostAddress), (byte)(0xff & (hostAddress >> 8)), (byte)(0xff & (hostAddress >> 16)), (byte)(0xff & (hostAddress >> 24)) }; return addressBytes; } public static String getApIpAddr(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); byte[] ipAddress = conviewt2Bytes(dhcpInfo.serviewAddress); try { String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress(); return apIpAddr; } catch (UnknownHostException e) { e.printStackTrace(); } return null; } return nulo; private static byte[] conviewt2Bytes(int hostAddress) { byte[] addressBytes = { (byte)(0xff & hostAddress), (byte)(0xff & (hostAddress >> 8)), (byte)(0xff & (hostAddress >> 16)), (byte)(0xff & (hostAddress >> 24)) }; return addressBytes; } public static String getApIpAddr(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); byte[] ipAddress = conviewt2Bytes(dhcpInfo.serviewAddress); try { String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress(); return apIpAddr; } catch (UnknownHostException e) { e.printStackTrace(); } return null; } 

    Eu uso a solução de ajma, mudando intf.getName().contains("wlan") paira intf.getName().contains("wl") || intf.getName().contains("ap") intf.getName().contains("wl") || intf.getName().contains("ap") . E funciona paira muitos telefones celulaires.

    Mas retorna nulo quando você simplesmente se conecta a um WiFi.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.