/**
* If WiFi is not enabled, you can still obtain the MAC address, but the IP address must be in the connected status or 0
*
* @ Param Context
*/
Public
Static void getmacaddress (context ){
String macaddress =
Null, IP = NULL;
Wifimanager wifimgr = (wifimanager) Context
. Getsystemservice (context. wifi_service );
Wifiinfo info = (null = wifimgr?
Null: wifimgr. getconnectioninfo ());
If (null! = Info ){
Macaddress = info. getmacaddress ();
//
IP = int2ip (info. getipaddress ());
}
System. Out. println ("Mac:" + macaddress +
", IP:" + IP );
}
/**
* Principle: each segment of an IP address can be regarded as an 8-bit unsigned integer (0-255). Each segment is split into a binary array, then convert the binary number into an unsigned 32 as an integer.
* Example: an IP address is 10.0.3.193. The number of binary numbers corresponding to each segment is 10 00001010 0 00000000 3 00000011 193
* 11000001 combination: 00001010 00000000 00000011
* 11000001, which is converted to decimal: 167773121, that is, the number after the IP address is converted to decimal.
*
* @ Param IP
* @ Return
*/
Public
Static long ip2int (string IP ){
String [] items = IP. Split ("\\.");
Return long. valueof (items [0]) <24 | long. valueof (items [1]) <16
| Long. valueof (items [2]) <8 | long. valueof (items [3]);
}
/**
* Principle: convert this integer into a 32-bit binary number. Split each eight bits from left to right to get the 4-Segment 8-bit binary number. convert these binary numbers into integers and then add "."
* This is the IP address. For example, the 167773121 binary representation is 00001010 00000000 00000011 11000001.
* Split it into four segments:, and. Then, convert them into integers and add "." To 10.0.3.193.
*
* @ Param ipint
* @ Return
*/
Public
Static string int2ip (long ipint ){
Stringbuilder sb =
New stringbuilder ();
SB. append (ipint & 0xff). append (".");
SB. append (ipint> 8) & 0xff). append (".");
SB. append (ipint> 16) & 0xff). append (".");
SB. append (ipint> 24) & 0xff );
Return sb. tostring ();
}