Android Wifi Address Get getDeviceUniqueIdentificator( @Nonnull Context context, @Nonnull String appId)

Here you can find the source of getDeviceUniqueIdentificator( @Nonnull Context context, @Nonnull String appId)

Description

Generates an unique, stable UID that identifies the device where the user is currently logged.

License

Apache License

Parameter

Parameter Description
context A Context to retrieve data from the TelephonyManager and ContentResolver
appId An unique and possibly secret app ID to "secure" the generated value

Return

An alphanumeric (+ hyphens) string

Declaration

@Nonnull
public static String getDeviceUniqueIdentificator(
        @Nonnull Context context, @Nonnull String appId) 

Method Source Code

//package com.java2s;
/*//from w ww.  j  a v  a  2  s. c  om
 * Copyright 2013 Luluvise Ltd
 * Copyright 2013 Marco Salis - fast3r(at)gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.UUID;

import javax.annotation.Nonnull;

import android.annotation.TargetApi;

import android.content.Context;

import android.content.pm.PackageManager;

import android.os.Build;

import android.telephony.TelephonyManager;

public class Main {
    /**
     * Generates an unique, stable UID that identifies the device where the user
     * is currently logged.
     * 
     * Requires the <code>READ_PHONE_STATE</code> permission in the Manifest.
     * 
     * @param context
     *            A {@link Context} to retrieve data from the
     *            {@link TelephonyManager} and {@link ContentResolver}
     * @param appId
     *            An unique and possibly secret app ID to "secure" the generated
     *            value
     * @return An alphanumeric (+ hyphens) {@link UUID} string
     */
    @Nonnull
    public static String getDeviceUniqueIdentificator(
            @Nonnull Context context, @Nonnull String appId) {
        String telephonyId = null;
        String androidId = null;

        if (hasTelephony(context)) {
            final TelephonyManager telephony = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            telephonyId = telephony.getDeviceId();
        }
        if (telephonyId == null) {
            telephonyId = "";
        }

        if (Build.VERSION.SDK_INT >= 9) { // Build.VERSION_CODES.GINGERBREAD
            androidId = getAndroidBuildSerial();
        } else {
            androidId = android.provider.Settings.Secure.getString(
                    context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }
        // if ("9774d56d682e549c".equals(androidId)) { // broken Android ID
        // FIXME: fallback
        // }
        if (androidId == null) {
            androidId = "";
        }

        UUID deviceUuid = new UUID(androidId.hashCode(),
                ((long) telephonyId.hashCode() << 32) | appId.hashCode());

        return deviceUuid.toString();
    }

    /**
     * Checks if the device has the {@link PackageManager#FEATURE_TELEPHONY}.
     * 
     * @param context
     * @return true if the telephony is available, false otherwise
     */
    public static boolean hasTelephony(@Nonnull Context context) {
        final PackageManager manager = context.getPackageManager();
        return manager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    }

    /**
     * Returns the {@link #android.os.Build.SERIAL} string.<br>
     * Not guaranteed to be unique and not null, only available from API 9.
     */
    @TargetApi(9)
    private static String getAndroidBuildSerial() {
        return android.os.Build.SERIAL;
    }
}

Related

  1. getWifiMacAddress()
  2. getDeviceUDID(Context context)