Get IP address from Wi-Fi hotspot in android

I want to get the IP address of wifi hotspot (from another computer), with which the Android device is connected via Wi-Fi, not the local IP address of the android . I am running the application in a real device . I can scan all wifi and get their name.

public class WifiConnectorActivity extends Activity { TextView mainText; WifiManager mainWifi; WifiReceiver receiverWifi; List<ScanResult> wifiList; StringBuilder sb = new StringBuilder(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mainWifi = (WifiManager)getSystemService(Context.WIFI_SERVICE); mainText = (TextView) findViewById(R.id.text); mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); receiverWifi = new WifiReceiver(); if(!mainWifi.isWifiEnabled()){ mainWifi.setWifiEnabled(true); } registerReceiver(receiverWifi, new IntentFilter( WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); mainWifi.startScan(); mainText.setText("\nStarting Scan...\n"); } class WifiReceiver extends BroadcastReceiver { public void onReceive(Context c, Intent intent) { StringBuilder sb = new StringBuilder(); wifiList = mainWifi.getScanResults(); for(int i = 0; i < wifiList.size(); i++){ sb.append(new Integer(i+1).toString() + "."); sb.append((wifiList.get(i)).toString()); sb.append("\n"); } mainText.setText(sb); } } } 

Of course, I can get the IP address using this code:

 public static String getLocalIpAddressString() { 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().toString(); } } } } catch (Exception ex) { Log.e("IPADDRESS", ex.toString()); } return null; } 

For example, I can see that the local IP address of the Android device is 192.168.2.101, but how to get the IP address of the Wi-Fi access point in the code 192.168.2.1. Thanks!

+4
source share
1 answer

Not all WiFi access points have IP addresses! This is not a requirement. He works on a different level.

In doing so, you can use the reverse ARP on the AP wireless MAC address to get its IP address, if any. Also note that this IP is sometimes different from a wired interface.

For all-in-one home wireless routers, you can also check all DHCP destinations as the gateway address, but again, this has no direct correlation with the access point.

0
source

All Articles