Ewelina
Ewelina

Reputation: 43

Get UUID Android

I have problem with UUID Android.This method get UUID Android and add permission Manifest

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

this method this get UUID

   public String deviceUDID(Context ctx) {
        final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId(); //line 82
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + android.provider.Settings.Secure.getString(ctx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        String deviceId = deviceUuid.toString();
        Log.d("Device Id", deviceId);
        return deviceId;
    }

when click in button with show on textView UUID, app stopped.

my error log

03-08 14:22:39.496 20825-20825/com.novum.smrtkarta E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xxxx.smrtkarta, PID: 20825
java.lang.SecurityException: getDeviceId: Neither user 10155 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getDeviceId(ITelephony.java:4207)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:706)
at com.novum.smrtkarta.activity.GetNumberActivity.deviceUDID(GetNumberActivity.java:81)
at com.novum.smrtkarta.activity.GetNumberActivity$1.onClick(GetNumberActivity.java:62)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Upvotes: 2

Views: 2544

Answers (1)

w00ly
w00ly

Reputation: 475

You have to make sure your request the permission for READ_PHONE_STATE in your Manifest:

<manifest xmlns:android="schemas.android.com/apk/res/android"; package="your.package.name"> 

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

However, if you target Android M, declaring the permission in the Manifest will not be enough, you have to explicitly ask user at runtime : https://developer.android.com/training/permissions/requesting.html

To check if you have a permission, call the ContextCompat.checkSelfPermission() method. For example, this snippet shows how to check if the activity has permission to write to the calendar:

// Assume thisActivity is the current activity 
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.WRITE_CALENDAR);

So in your case, before calling the method deviceUDID, please make sure you have the READ_PHONE_STATE permission.

You can follow the steps as indicated here.

Upvotes: 1

Related Questions