从Android的WiFi热点获取IP热点、Android、WiFi、IP

2023-09-05 08:21:14 作者:偏偏囍歡伱

我想获得WiFi热点(从另一台计算机)的IP的Andr​​oid设备通过WiFi进行连接,没有IP本地的机器人。我运行的真正的设备应用程序。 我可以扫描所有的WiFi,并让他们的名字。

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);
        }
      }


}

当然,我可以得到IP本地通过使用这种code:

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;
         }

例如,我可以看到IP本地的Andr​​oid设备是192.168.2.101,但如何让WiFi热点的IP是192.168.2.1在code。 谢谢!

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!

推荐答案

不是所有的WiFi接入点,甚至有IP地址!这不是一个要求。它可以运行在不同的层。

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

话虽这么说,你可以使用反向ARP对AP的无线MAC地址来获得它的IP地址,如果它有一个。还要注意的是这个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.

有关家庭的所有功能于一身的无线路由器,你也可以检查任何DHCP作为网关地址分配,但是,这并不一定有直接的关系到接入点。

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.