読者です 読者をやめる 読者になる 読者になる

Log.i53

Themidaのアンパックを目指すブログ改め使い物になるえんじにゃを目指すブログ

AndroidにおけるUID(Uniqe IDentifier)とその取得方法の一覧

Androidにおける端末を個体識別する目的で利用可能なIDとその取得方法をまとめる.※ importやpermissionの記載は割愛

端末ID(IMEI)

// TelephonyManagerを利用するアクセス
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
// SystemPropertiesを利用するアクセス
String imei = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);

端末のソフトウェアバージョン

// TelephonyManagerを利用するアクセス
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String deviceSvn = tm.getDeviceSvn();

端末ソフトウェアバージョン番号(IMEI/SV)

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei_sv = tm.getDeviceSoftwareVersion();

加入者ID(IMSI)

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imsi = tm.getSubscriberId();

SIMシリアルID(ICCID)

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String iccid = tm.getSimSerialNumber();
// String iccid = tm.getIccSerialNumber();

電話番号(GSMのMSISDN)

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String line1Number = tm.getLine1Number();
// String msisdn = tm.getMsisdn();

GSMのアルファベットタグ

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String line1AlphaTag = tm. getLine1AlphaTag();

ボイスメール名

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String voiceMailAlphaTag = tm. getVoiceMailAlphaTag();

ボイスメール番号

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String voiceMai = tm.getVoiceMailNumber();

Android ID(OS初回起動時に生成される16桁の乱数=端末ID)

String udid = Settings.Secure.getString(getContentResolver(), Settings.System.ANDROID_ID);

MACアドレス

WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String mac = wifi.getConnectionInfo().getMacAddress();

UUIDの生成例

public class Installation {
    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();
    }
}

参照:Identifying App Installations | Android Developers Blog

複数のUIDを組み合わせたUUIDの生成例

TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice = "" + tm.getDeviceId();
final String tmSerial = "" + tm.getSimSerialNumber();
final String androidId = "" + Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String deviceId = deviceUuid.toString();

参照:java - Is there a unique Android device ID? - Stack Overflow