Obtain the unique android device code, including the Cpu number and Mac address.

Source: Internet
Author: User

When developing Android applications, we often need a unique device code to determine the client.


Several Methods in Android are often unreliable in use


1. DEVICE_ID
If we do need the identifier of a real device, DEVICE_ID may be used. Obtained Through TelephonyManager. getDeviceId (). It returns IMEI, MEID, or ESN code based on different mobile devices.

Disadvantage: This vulnerability exists on a few devices and junk data is returned.

2. MAC ADDRESS
You can also obtain the mac address as the device id through Wifi.
Disadvantage: If Wifi is disabled, the hardware device may not return mac address ..

3. Serial Number
Android. OS. Build. SERIAL
Disadvantage: In a few devices, junk data is returned.
4. ANDROID_ID
ANDROID_ID is the number of 64bits generated and stored when the device is started for the first time,
Disadvantage: it is not applicable when the number of devices is changed after the device is set to wipe.


The android underlying layer is Linux. We can use the Linux method to obtain it:


1 cpu No:

File in:/proc/cpuinfo

Use the Adb shell to view the following information:

Adb shell cat/proc/cpuinfo

2 mac address

File Path:/sys/class/net/wlan0/address

Adb shell cat/sys/class/net/wlan0/address
Xx: aa

In this way, the serial numbers of the two can be obtained,


The method is determined, and the rest is the code.

Take the Mac address as an example:

String getMac (){
String macSerial = null;
String str = "";
Try {
Process pp = runtime.getruntime(cmd.exe c (
"Cat/sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader (pp. getInputStream ());
LineNumberReader input = new LineNumberReader (ir );


For (; null! = Str ;){
Str = input. readLine ();
If (str! = Null ){
MacSerial = str. trim (); // remove space
Break;
}
}
} Catch (IOException ex ){
// Assign the default value
Ex. printStackTrace ();
}
Return macSerial;
}


Author: hpccn

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.