Obtain the mac ip address in Android

Source: Internet
Author: User

/**

* 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 ();

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.