Get IP from wifi hotspot in android

一世执手 提交于 2019-12-04 13:16:32

问题


I want to get IP of wifi hotspot (from another computer) that android device connect via wifi, not IP local of android. I run application in real device. I can scan all wifi and get name of them.

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 IP local by use 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 IP local of android device is 192.168.2.101, but how to get IP of wifi hotspot is 192.168.2.1 in code. Thanks!


回答1:


Not all WiFi access points even have IP addresses! It is not a requirement. It runs on a different layer.

That being said, you can use reverse-ARP on the wireless MAC of the AP to get its IP address, if it has one. Also note that this IP is sometimes different than the wired interface.

For home all-in-one wireless routers, you can also check whatever DHCP assigns as a gateway address, but again, this doesn't have a direct correlation to the access point.




回答2:


If you are using default subnets then a host Ip address of 192.168.2.101 directly implies an access point address of 192.168.2.1. Your IP address is group C. So for Any address x.x.x.y in this group, the subnet itself is x.x.x.0 and the first host address x.x.x.1 is assigned to the access point. All other addresses: x.x.x.2 to x.x.x.254 are assigned to connected hosts and x.x.x.255 is the local broadcast address.



来源:https://stackoverflow.com/questions/6561317/get-ip-from-wifi-hotspot-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!