从android中的wifi热点获取IP

发布于 2024-11-18 00:24:11 字数 2552 浏览 2 评论 0原文

我想获取 Android 设备通过 WiFi 连接的 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);
        }
      }
    
    
}

当然,我可以使用这段代码获取本地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;
         }

例如,我可以看到Android设备的本地IP地址是192.168.2.101,但是如何在代码中获取WiFi热点的IP是192.168.2.1呢?

I want to get the IP address of WiFi hotspot (from another computer) that android device connect via WiFi, not the local IP address 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 the local IP address 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 the local IP address of the Android device is 192.168.2.101, but how do I get IP of WiFi hotspot is 192.168.2.1 in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-11-25 00:24:11

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

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

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

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.

盗琴音 2024-11-25 00:24:11

如果您使用默认子网,则主机 IP 地址 192.168.2.101 直接意味着接入点地址 192.168.2.1。您的 IP 地址是组 C。因此,对于该组中的任何地址 xxxy,子网本身为 xxx0,并分配第一个主机地址 xxx1到接入点。所有其他地址:xxx2xxx254 均分配给连接的主机,xxx255 是本地广播地址。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文