Android获取手机IMEI,IMSI, MAC标识和唯一标识的生成

急的直接看这个:

android 获取设备IMEI与序列号:

​
//IMEI(imei)
TelephonyManager tm = (TelephonyManager) ChinaApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if(!TextUtils.isEmpty(imei)){
    deviceId.append("imei");
    deviceId.append(imei);
    return deviceId.toString();
}

//序列号(sn)
String sn = tm.getSimSerialNumber();
if(!TextUtils.isEmpty(sn)){
    deviceId.append("sn");
    deviceId.append(sn);
    return deviceId.toString();
}

​

Android的几种标识

1. IMEI

IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的"电子串号",它与每台移动电话机一一对应,而且该码是全世界唯一的。每一只移动电话机在组装完成后都将被赋予一个全球唯一的一组号码,这个号码从生产到交付使用都将被制造生产的厂商所记录。

PS:通俗来讲就是标识你当前设备(手机)全世界唯一,类似于个人身份证

权限要求:android.permission.READ_PHONE_STATE

​
public synchronized static String getid(Context context) { 
    TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);  
    String ID= TelephonyMgr.getDeviceId(); 
    return ID;
}

​

2. IMSI

国际移动用户识别码(IMSI:International Mobile Subscriber Identification

Number)是区别移动用户的标志,储存在SIM卡中,可用于区别移动用户的有效信息。其总长度不超过15位,同样使用0~9的数字。其中MCC是移动用户所属国家代号,占3位数字,中国的MCC规定为460;MNC是移动网号码,由两位或者三位数字组成,中国移动的移动网络编码(MNC)为00;用于识别移动用户所归属的移动通信网;MSIN是移动用户识别码,用以识别某一移动通信网中的移动用户

PS:通俗来讲就是标识你当前SIM卡(手机卡)唯一,同样类似于个人身份证


3. MAC

MAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制,或称为物理地址、硬件地址,用来定义网络设备的位置。在OSI模型中,第三层网络层负责 IP地址,第二层数据链路层则负责 MAC地址。因此一个主机会有一个MAC地址,而每个网络位置会有一个专属于它的IP地址

PS:通俗来讲就是标识你当前使用我这个软件(功能)时的地址

最主要的是:在平板设备上,无法通过imei标示设备,我们会将mac地址作为用户的唯一标识

权限要求:android.permission.ACCESS_WIFI_STATE 

​
public synchronized static String getMacid(Context context) { 
      WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); 
       String WLANMAC = wm.getConnectionInfo().getMacAddress();
       return WLANMAC ;
 }

​

4. 蓝牙

权限要求:android.permission.BLUETOOTH

​
public synchronized static String getMacid(Context context) { 
    BluetoothAdapter mBlueth= BluetoothAdapter.getDefaultAdapter();  
    String mBluethId= mBlueth.getAddress();
     return mBluethId; 
}

​

5. Installtion ID

     考虑到Android设备的多样性,比如一些平板没有通话功能,或者部分低价设备没有WLAN或者蓝牙,甚至用户不愿意赋予APP这些需要的权限,我们就使用无需权限的方法;这种方式的原理是在程序安装后第一次运行时生成一个ID,该方式和设备唯一标识不一样,不同的应用程序会产生不同的ID,同一个程序重新安装也会不同。所以这不是设备的唯一ID,但是可以保证每个用户的ID是不同的。可以说是用来标识每一份应用程序的唯一ID(即Installtion ID),可以用来跟踪应用的安装数量等。 

​
public class GetDeviceid { 
private static String sID = null; 
private static final String INSTALLATION = "INSTALLATION"; 
public synchronized static String id(Context context) { 
      if (sID == null) {
         File installation = new File(context.getFilesDir(), INSTALLATION); 
      try {
     if (!installation.exists()) 
        writeInstallationFile(installation);
         sID = readInstallationFile(installation); 
    } catch (Exception e) {
     throw new RuntimeException(e); 
    } 
  } 
      return sID;
 }

 private static String readInstallationFile(File installation) throws IOException { 
      RandomAccessFile f = new RandomAccessFile(installation, "r"); 
      byte[] bytes = new byte[(int) f.length()];
      f.readFully(bytes); 
      f.close(); 
      return new String(bytes);
 } 

private static void writeInstallationFile(File installation) throws IOException { 
      FileOutputStream out = new FileOutputStream(installation); 
      String id = UUID.randomUUID().toString(); 
      out.write(id.getBytes());
       out.close(); 
    }
}

​

5. Combined Device ID

      综上所述,我们有多种方式取得设备的唯一标识。它们中的一些可能会返回null,或者由于硬件缺失、权限问题等获取失败。但你总能获得至少一个能用。所以,最好的方法就是通过拼接,或者拼接后的计算出的MD5值来产生一个结果。

String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC;
     MessageDigest m = null;
     try { 
        m = MessageDigest.getInstance("MD5"); 
    } catch (NoSuchAlgorithmException e) { 
        e.printStackTrace(); 
  } 
      m.update(m_szLongID.getBytes(),0,m_szLongID.length()); 
    
// get md5 bytes  
      byte p_md5Data[] = m.digest(); 

// create a hex string  
      String m_szUniqueID = new String();
       for (int i=0;i

 

另外直接生成UUID也是一个额外的便捷选择。通常我们建议使用UUID来标识对象或持久化数据。UUID详情见这篇博客:https://blog.csdn.net/LucasXu01/article/details/82954844

 

  • 参考1:https://www.jianshu.com/p/b001bf005801
  • 参考2:https://blog.csdn.net/ysh06201418/article/details/79106200
  • 参考3:https://www.jianshu.com/p/81570792e226
  •  

你可能感兴趣的:(Android获取手机IMEI,IMSI, MAC标识和唯一标识的生成)