如何从android代码中知道路由器的IP地址?

发布于 2024-12-29 18:26:43 字数 307 浏览 5 评论 0原文

如何从代码中找到路由器的IP地址(网关地址)?

WifiInfo.getIpAddress() - 返回设备的 IP 地址。

在 shell 命令中“ipconfig”不返回任何值。

这是我的解决方案,但如果有更好的方法,请告诉我:

WifiManager manager = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo info = manager.getDhcpInfo();
info.gateway;

How can you find the IP address of the router (gateway address) from code?

WifiInfo.getIpAddress() - returns IP address of device.

In a shell command "ipconfig" does not return any value.

Here is my solution, but please let me know if there is a better way to do this:

WifiManager manager = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo info = manager.getDhcpInfo();
info.gateway;

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

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

发布评论

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

评论(4

海的爱人是光 2025-01-05 18:26:43

嘿,这可能对您有帮助: DHCPInfo

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

将以下行添加到 AndroidManifest.xml 中为了访问 wifi 功能:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

由于 formatIpAddress 现在已弃用,您可以使用以下代码

byte[] myIPAddress = BigInteger.valueOf(manager.getIpAddress()).toByteArray();
ArrayUtils.reverse(myIPAddress);
InetAddress myInetIP = InetAddress.getByAddress(myIPAddress);
String myIP = myInetIP.getHostAddress();

Hey this might help you: DHCPInfo

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

Add following rows to AndroidManifest.xml in order to access wifi functionalities:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

As the formatIpAddress is deprecated now you can use below code

byte[] myIPAddress = BigInteger.valueOf(manager.getIpAddress()).toByteArray();
ArrayUtils.reverse(myIPAddress);
InetAddress myInetIP = InetAddress.getByAddress(myIPAddress);
String myIP = myInetIP.getHostAddress();
梦情居士 2025-01-05 18:26:43

我认为你这样做的方式是最好的(AFAIK),这里有一些来自 Cordova 插件的示例代码,它以同样的方式进行:

public class GetRouterIPAddress extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            String ip = getRouterIPAddress();
            if (ip.equals("0.0.0.0")) {
                callbackContext.error("No valid IP address");
                return false;
            }
            callbackContext.success(ip);
            return true;
        } catch(Exception e) {
            callbackContext.error("Error while retrieving the IP address. " + e.getMessage());
            return false;
        }
    }

    private String formatIP(int ip) {
        return String.format(
            "%d.%d.%d.%d",
            (ip & 0xff),
            (ip >> 8 & 0xff),
            (ip >> 16 & 0xff),
            (ip >> 24 & 0xff)
        );
    }

    private String getRouterIPAddress() {
        WifiManager wifiManager = (WifiManager) cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifiManager.getDhcpInfo();
        int ip = dhcp.gateway;
        return formatIP(ip);
    }
}

https://github.com/vallieres/cordova-plugin-get-router-ip-address/blob/master/src/android/GetRouterIPAddress.java

I think the way you're doing it is the best (AFAIK), here's some example code from a Cordova plugin that does it the same way:

public class GetRouterIPAddress extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            String ip = getRouterIPAddress();
            if (ip.equals("0.0.0.0")) {
                callbackContext.error("No valid IP address");
                return false;
            }
            callbackContext.success(ip);
            return true;
        } catch(Exception e) {
            callbackContext.error("Error while retrieving the IP address. " + e.getMessage());
            return false;
        }
    }

    private String formatIP(int ip) {
        return String.format(
            "%d.%d.%d.%d",
            (ip & 0xff),
            (ip >> 8 & 0xff),
            (ip >> 16 & 0xff),
            (ip >> 24 & 0xff)
        );
    }

    private String getRouterIPAddress() {
        WifiManager wifiManager = (WifiManager) cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifiManager.getDhcpInfo();
        int ip = dhcp.gateway;
        return formatIP(ip);
    }
}

https://github.com/vallieres/cordova-plugin-get-router-ip-address/blob/master/src/android/GetRouterIPAddress.java

十二 2025-01-05 18:26:43

试试这个:

$ busybox ip route show

它在我的平板电脑上使用终端模拟器运行良好!

Try this:

$ busybox ip route show

It worked fine in my tablet with Terminal Emulator!

节枝 2025-01-05 18:26:43

要获取 IP 地址,请尝试 getInetAddress();

To get the IP address, try getInetAddress();

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