how to get the IP of the wifi hotspot in Android?

后端 未结 7 1256
半阙折子戏
半阙折子戏 2021-02-02 14:11

As the title says... I\'m trying to be able to get the IP of the wifi iface when it is configured as hotspot. Ideally, I would like to find something that works for all the phon

7条回答
  •  旧时难觅i
    2021-02-02 15:07

    You can use this.

    ((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress
    

    Full Code

    private String getHotspotIPAddress() {
    
        int ipAddress = mContext.getSystemService(Context.WIFI_SERVICE)).getDhcpInfo().serverAddress;
    
        if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
            ipAddress = Integer.reverseBytes(ipAddress);
        }
    
        byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
    
        String ipAddressString;
        try {
            ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
        } catch (UnknownHostException ex) {
            ipAddressString = "";
        }
    
        return ipAddressString;
    
    }
    

提交回复
热议问题