从android中的wifi热点获取IP

时间:2021-02-18 17:20:47

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.

我想获得通过wifi连接android设备的wifi热点(来自另一台计算机)的IP,而不是Android的IP本地。我在真实设备中运行应用程序。我可以扫描所有的wifi并获得它们的名称。

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:

当然,我可以使用以下代码获取IP本地:

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!

例如,我可以看到Android设备的IP本地是192.168.2.101,但是如何获取wifi热点的IP在代码中是192.168.2.1。谢谢!

1 个解决方案

#1


0  

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

并非所有WiFi接入点都有IP地址!这不是必需的。它运行在不同的层上。

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.

话虽这么说,您可以在AP的无线MAC上使用反向ARP来获取其IP地址(如果有的话)。另请注意,此IP有时与有线接口不同。

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.

对于家庭多功能一体无线路由器,您还可以检查DHCP分配的任何网关地址,但同样,这与接入点没有直接关联。

#1


0  

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

并非所有WiFi接入点都有IP地址!这不是必需的。它运行在不同的层上。

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.

话虽这么说,您可以在AP的无线MAC上使用反向ARP来获取其IP地址(如果有的话)。另请注意,此IP有时与有线接口不同。

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.

对于家庭多功能一体无线路由器,您还可以检查DHCP分配的任何网关地址,但同样,这与接入点没有直接关联。