Imei of an android device

Dear library what do you recommend to get the imei of my android device, or any unique identifier of the device?

@sonichel===

see here : https://developer.android.com/reference/android/telephony/TelephonyManager#getImei(int)

1 Like

Thx akenaton. I am new in processing and I am learning to program, regarding the link that you sent me that library should use to obtain the imei?

String IMEI;

void start(){
IMEI = getImei ();
println(IMEI);
}

@sonichel===

not so simple ! - You have to import TelephonyManager for that : code snippet below

import android.os.Build;
import android.content.Context;
import android.telephony.TelephonyManager;
Context ctx;
public String IME="";
public String SN="";

void setup(){
  size(800,600);
  background(255,0,0);
  ctx = this.getActivity().getApplicationContext();
  IME= getIme();
  
}

void draw(){
  background(255,0,0);
  textSize(18);
  text("IME=====" + IME, 100,200);
  text ("SERIAL NUMBER====="+ SN, 100,500);
}

public String getIme(){
TelephonyManager tm;
tm=(TelephonyManager)this.getActivity().getSystemService(ctx.TELEPHONY_SERVICE);  
//String imei = tm.getImei();///FOR DEVICES >>26//uncomment if your device is in this case

SN = Build.SERIAL;///for all devices

String IM=tm.getDeviceId();  ////for devices <<26,now deprecated; comment if your OS device is more recent
  
return IM;
}
1 Like

26

Apparently serial is obsolete …

@sonichel===
deprecated for >=oreo: in this case use Build.getSerial()
Yet this code has to work (as for the other warning you can get easily rid of it creating some static method)

1 Like

great now it seems to work getSerial … but it throws me this error … and in the android tablet says “the IMEI application has stopped working”

@sonichel===
add “READ_PHONE_STATE” permission to your sketch on runtime

if I realized that you had to give that permission, but the fault persists … look

friend, look, I reduced the sketch to this and it still fails…

@sonichel===

  • we cannot read your error, dont put a picture, cut & copy
  • yet probably this error is fired because you dont ak the permission on runtime: after Mashmallow “pernicious” permissions cannot be set only adding them to the Manifest.

@sonichel==== dont change the code, the error you get now is another one!!!

ok, I went back to how I should be … I enabled the permission “READ_PHONE_STATE” and in ctx.TELEPHONY_SERVICE appears "The static field Context.TELEPHONY_SERVICE should be accessed in a static way… and on line 30 String IM = tm.getDeviceId (); it indicates me “The method getDeviceId() from the type TelephonyManager is deprecated”.

when compiling I throw the following:

Build folder: /var/folders/_c/z2br0db57y560_mkrc58_d100000gn/T/android1206249006627969043sketch
:app:preBuild UP-TO-DATE
:app:preDebugBuild
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:checkDebugManifest
:app:generateDebugBuildConfig
:app:prepareLintJar
:app:generateDebugResValues
:app:generateDebugResources
:app:mergeDebugResources
:app:createDebugCompatibleScreenManifests
:app:processDebugManifest
:app:splitsDiscoveryTaskDebug
:app:processDebugResources
:app:generateDebugSources
:app:javaPreCompileDebug
:app:compileDebugJavaWithJavac
:app:compileDebugNdk NO-SOURCE
:app:compileDebugSources
:app:mergeDebugShaders
:app:compileDebugShaders
:app:generateDebugAssets
:app:mergeDebugAssets
:app:extractTryWithResourcesSupportJarDebug
:app:transformClassesWithStackFramesFixerForDebug
:app:transformClassesWithDesugarForDebug
:app:transformClassesWithDexBuilderForDebug
:app:transformDexArchiveWithExternalLibsDexMergerForDebug
:app:transformDexArchiveWithDexMergerForDebug
:app:mergeDebugJniLibFolders
:app:transformNativeLibsWithMergeJniLibsForDebug
:app:processDebugJavaRes NO-SOURCE
:app:transformResourcesWithMergeJavaResForDebug
:app:validateSigningDebug
:app:packageDebug
:app:assembleDebug

BUILD SUCCESSFUL in 28s
28 actionable tasks: 28 executed
FATAL EXCEPTION: Animation Thread
Process: processing.test.sketch_190120b, PID: 8388
java.lang.SecurityException: getImeiForSlot: Neither user 10074 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getImeiForSlot(ITelephony.java:4481)
at android.telephony.TelephonyManager.getImei(TelephonyManager.java:904)
at android.telephony.TelephonyManager.getImei(TelephonyManager.java:887)
at processing.test.sketch_190120b.sketch_190120b.getIme(sketch_190120b.java:48)
at processing.test.sketch_190120b.sketch_190120b.setup(sketch_190120b.java:34)
at processing.core.PApplet.handleDraw(PApplet.java:1838)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)
FATAL EXCEPTION: Animation Thread
Process: processing.test.sketch_190120b, PID: 8416
java.lang.SecurityException: getImeiForSlot: Neither user 10074 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getImeiForSlot(ITelephony.java:4481)
at android.telephony.TelephonyManager.getImei(TelephonyManager.java:904)
at android.telephony.TelephonyManager.getImei(TelephonyManager.java:887)
at processing.test.sketch_190120b.sketch_190120b.getIme(sketch_190120b.java:48)
at processing.test.sketch_190120b.sketch_190120b.setup(sketch_190120b.java:34)
at processing.core.PApplet.handleDraw(PApplet.java:1838)
at processing.core.PSurfaceNone.callDraw(PSurfaceNone.java:476)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:516)

@sonichel===
i have already explained that you have to get this permission on runtime and i think you have not asked for it: manifest has no sense in this type of cas (“dangerous permissions”) since Mashmallow…
deprecated has not any importance with android.

where do I give permission to “dangerous permissions” ??? should I declare it in the sketch setup or in the Sketch Permissions menu?

@sonichel===
see here=
https://android.processing.org/reference/permissions/request.html