There are a number of scenarios and requirements that you need to use a unique identifier for your mobile device.
In Android, there are several ways to get such an ID.
1. The IMEI: only valid for Android phones:
12 |
(telephonymanager)getsystemservice(telephony_servicetelephonymgr. Getdeviceid |
With this approach, you need to include a license in Androidmanifest.xml: Android.permission.READ_PHONE_STATE, and the user should be allowed to install the app. As a mobile phone, the IMEI is unique, it should be similar to 359881030314356 (unless you have a non-production cell phone (parallel) It may have invalid IMEI, such as: 0000000000000).
2. Pseudo-unique ID, this is valid on any Android phone
There are some special cases where some settings such as tablet are not calling, or you are reluctant to join the Read_phone_state license. And you still want to get a unique serial number and stuff like that. You can do this by removing the ROM version, the manufacturer, the CPU model, and other hardware information. The computed ID is not unique (since two phones have the same hardware and ROM images applied). It should be understood, however, that the possibility of a similar situation can be largely overlooked. To achieve this, you can use the build class:
1 2 3 4 5 6 7 8 9101112131415 |
StringM_szdevidshort="35"+We make a valid IMEIBuild.BOARD.Length()%10+Build.BRAND.Length()%10+Build.Cpu_abi.Length()%10+Build.DEVICE.Length()%10+Build.DISPLAY.Length()%10+Build.HOST.Length()%10+Build.Id.Length()%10+Build.Manufacturer.Length()%10+Build.MODEL.length ()%10 + build product. ()%10 + build. Tags. ()%10 + build. Type. ()%10 + build. User. ()%10 //13 digits |
Most of the build members are in the form of strings, and we only take their length information. We take 13 numbers and add "35" to the front. This ID looks like the 15-bit IMEI.
3. The Android ID
It is often considered to be not trusted because it is sometimes null. The development documentation explains: This ID will change if factory settings are made. Also, if a andorid phone is rooted, the ID can be changed arbitrarily.
1 |
Secure. GetString(getcontentresolverSecure. android_id); |
returns:9774d56d682e549c. No license is required.
4. The WLAN MAC Address string
is another unique ID. But you need to add Android.permission.ACCESS_WIFI_STATE permissions to your project, otherwise this address will be null.
12 |
(wifimanager)getsystemservice(Context. Wifi_servicewm. Getconnectioninfo(). Getmacaddress(); |
Returns:00:11:22:33:44:55 (This is not a real address.) And this address can easily be forged. ). The WLAN does not have to be opened to read some values.
5. The BT MAC Address string
Run only on devices that have Bluetooth. And to join Android.permission.BLUETOOTH permissions.
123 |
Nullbluetoothadapter. Getdefaultadapterm_bluetoothadapter. GetAddress(); |
returns:43:25:78:50:93:38. Bluetooth is not necessary to open, also can read.
6. Stitching to create UUID
1 //get the unique identity of the phone2 PublicString getphonesign () {3StringBuilder deviceId =NewStringBuilder ();4 //Channel Sign5Deviceid.append ("a");6 Try {7 //IMEI (IMEI)8Telephonymanager TM = (Telephonymanager) This. Getsystemservice (context.telephony_service);9String IMEI =Tm.getdeviceid ();Ten if(!Textutils.isempty (IMEI)) { OneDeviceid.append ("IMEI"); A deviceid.append (IMEI); - returndeviceid.tostring (); - } the //serial Number (SN) -String sn =Tm.getsimserialnumber (); - if(!textutils.isempty (SN)) { -Deviceid.append ("SN"); + deviceid.append (SN); - returndeviceid.tostring (); + } A //If none of the above, generate an ID: Random code atString uuid =Getuuid (); - if(!textutils.isempty (UUID)) { -Deviceid.append ("id"); - deviceid.append (UUID); - returndeviceid.tostring (); - } in}Catch(Exception e) { - e.printstacktrace (); toDeviceid.append ("id"). Append (Getuuid ()); + } - returndeviceid.tostring (); the } * /** $ * Get global unique UUIDPanax Notoginseng */ - PrivateString uuid; the PublicString Getuuid () { +Sharedpreferences Mshare = getsharedpreferences ("UUID", mode_private); A if(Mshare! =NULL){ theUUID = mshare.getstring ("uuid", "" "); + } - if(Textutils.isempty (UUID)) { $UUID =Uuid.randomuuid (). toString (); $Mshare.edit (). putstring ("UUID"), UUID). commit (); - } - returnuuid; the}
A UUID is a number generated on a machine that guarantees that all machines in the same time and space are unique. Typically, the platform provides the generated APIs. Based on standard calculations developed by the Open Software Foundation (OSF), Ethernet card addresses, nanosecond-seconds, chip ID codes, and many possible numbers are used |
How to get Android Unique ID (unique serial number)