安卓获取接入的Wifi热点设备的Ip地址

最近在做一个安卓设备间传输文件的app,建立热点让两部设备在同一个局域网之间传输文件,需要知道连接热点的设备的ip地址,这边记录一下获取的方式:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
        int ip = dhcpInfo.serverAddress;
        //此处获取ip为整数类型,需要进行转换
        String strIp = intToIp(ip);

   private String intToIp(int i) {
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
                + ((i >> 24) & 0xFF);
    }

你可能感兴趣的:(Android)