简体   繁体   English

Android获取设备的唯一ID

[英]Android get unique id for device

I need to restrict app subscription to certain number of devices per user. 我需要将应用程序订阅限制为每个用户一定数量的设备。 For this I was trying to get unique id for android phone ( Hardware ). 为此,我试图获取Android手机的唯一ID(硬件)。 I found below options but all of them has certain problems. 我发现以下选项,但所有选项都有某些问题。 Is there any solutions which does'nt have these problems ? 有没有解决这些问题的解决方案?

  • Imei / Mei numbers using TelephonyManager APIs ( getDeviceId ) - This will be problematic in case of dual sims there might be active and inactive sim slots. 使用TelephonyManager API( getDeviceId )的Imei / Mei号码-如果双卡同时存在有效和无效的SIM卡插槽,这将是一个问题。
  • AndroidId - Will be reset upon factory reset AndroidId-将在恢复出厂设置时重置
  • Wifi mac number - Need wifi connected to get this number and this will not be there for non wifi devices. Wifi Mac号码-需要连接wifi以获取此号码,非wifi设备将不存在此号码。
  • Advertaising id - Can be reset by user 广告ID-可由用户重置
  • Instance id - Can be reset on factory reset 实例ID-可以在恢复出厂设置时重置
  • Sim serial number - Not bind to device instead it is for sim SIM卡序列号 -不绑定到设备,而是用于SIM卡

After searching and thinking I come up with below solution. 经过搜索和思考,我提出了以下解决方案。

MacID can be used as the unique id. MacID可以用作唯一ID。 MacID can be get using permission android.permission.READ_PHONE_STATE even with Wifi switch off state. 即使在Wifi关机状态下,也可以使用权限android.permission.READ_PHONE_STATE获得MacID。 I have successfully tested these in few devices and it works fine. 我已经在少数设备中成功测试了这些设备,并且工作正常。 My testing is in progress will update here soon. 我的测试正在进行中,很快会在这里更新。

public static String getDeviceMacId(){
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }

                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:",b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception ex) {
            //handle exception
        }
        return "";
    }



Reference : https://stackoverflow.com/a/35830358/2788009 参考: https : //stackoverflow.com/a/35830358/2788009

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM