public void getAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
Log.i("size", "IP: "+convertIpaddr2String(info.getIpAddress()));
Log.i("size", "MAC: "+info.getMacAddress());
}
public static String convertIpaddr2String(int ip_int){
byte[] bytes = convertInt2Bytes(ip_int);
InetAddress addr = null;
try {
addr = InetAddress.getByAddress(bytes);
} catch (UnknownHostException e) {
e.printStackTrace();
return null;
}
return addr.getHostAddress();
}
public static byte[] convertInt2Bytes(int value){
byte[] addr = new byte[4];
addr[3] = (byte) ((value >>> 24) & 0xFF);
addr[2] = (byte) ((value >>> 16) & 0xFF);
addr[1] = (byte) ((value >>> 8) & 0xFF);
addr[0] = (byte) (value & 0xFF);
return addr;
}
int轉為String