# Is there anyway to get a Unique device ID on Android?

**URL:** <https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411>\
**Category:** Blueprint\
**Tags:** unreal-engine, Android, nodes, bug-report, Blueprint, question\
**Created:** [December 14, 2015, 10:04am UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411 "2015-12-14T10:04:17Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![m-motion](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@m-motion](https://forums.unrealengine.com/u/m-motion)\
**Post date:** [December 14, 2015, 10:04am UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/1 "2015-12-14T10:04:17Z")

</div>

Unique Device ID Node on Android doesn’t seem to work.

i am trying to get unique device ID , it works great on PC but on Android it always gives me the same code, i tested it on a couple of phones and tablet and they all have the same code. which makes it quite useless.

is there anyway to get a Unique ID for Android devices ? (preferably with blueprint)

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [March 5, 2016, 3:21pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/2 "2016-03-05T15:21:11Z")

</div>

This is my problem too. They didn’t write any code for this on android.

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [March 7, 2016, 4:38am UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/3 "2016-03-07T04:38:51Z")

</div>

Hi  
I write a blueprint function in my blueprint helper function library to get device id with JNI in android. I replaced this function with get Unique device ID in my blueprints.

Tutorial to create blueprint helper function Library [A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums](https://wiki.unrealengine.com/Blueprint_Function_Library,_Create_Your_Own_to_Share_With_Others)

I hope this also help you.

in .h file

```
UFUNCTION(BlueprintPure, Category = "SkyHeroes")
static FString GetDeviceId();

```

in .cpp file

```
#ifdef __ANDROID__
#include "Android/AndroidApplication.h"
#endif

```

and

```
FString USkyHeroesHelperFunctionLibrary::GetDeviceId()
{
#ifdef __ANDROID__
	JNIEnv* env = FAndroidApplication::GetJavaEnv();
	jobject activity = FAndroidApplication::GetGameActivityThis();
	jmethodID mid = env->GetMethodID(env->GetObjectClass(activity),
		"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
	jobject telephony_manager = env->CallObjectMethod(activity, mid,
		env->NewStringUTF("phone"));	
	mid = env->GetMethodID(env->GetObjectClass(telephony_manager),
		"getDeviceId", "()Ljava/lang/String;");
	jstring str = (jstring)env->CallObjectMethod(telephony_manager, mid);
	jsize len = env->GetStringUTFLength(str);
	char* deviceId = (char*)calloc(len + 1, 1);
	env->GetStringUTFRegion(str, 0, len, deviceId);
	env->DeleteLocalRef(str);
	return FMD5::HashAnsiString(*FString(deviceId));
#else
	return FPlatformMisc::GetHashedMacAddressString();
#endif
}

```

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [March 18, 2016, 3:39pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/4 "2016-03-18T15:39:47Z")

</div>

This code works fine on android phones but it is not working on tablets that don’t have sim card chip.

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [March 19, 2016, 9:16pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/5 "2016-03-19T21:16:45Z")

</div>

I changed the code to below code.

```
FString UShekarchiHelperFunctionLibrary::GetDeviceId()
{
#ifdef __ANDROID__
	jstring str;
	JNIEnv* env = FAndroidApplication::GetJavaEnv();
	jobject activity = FAndroidApplication::GetGameActivityThis();
	jmethodID mid = env->GetMethodID(env->GetObjectClass(activity),
		"getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
	// try to get the phone ID
	jobject telephony_manager = env->CallObjectMethod(activity, mid,
		env->NewStringUTF("phone"));
	jmethodID mid2 = env->GetMethodID(env->GetObjectClass(telephony_manager),
		"getDeviceId", "()Ljava/lang/String;");
	// It is a phone
	str = (jstring)env->CallObjectMethod(telephony_manager, mid2);

	jsize len = env->GetStringUTFLength(str);

	if (len == 0)
	{
		// try to get the wifi mac address
		jobject wifi_manager = env->CallObjectMethod(activity, mid,
			env->NewStringUTF("wifi"));
		mid = env->GetMethodID(env->GetObjectClass(wifi_manager),
			"getConnectionInfo", "()Landroid/net/wifi/WifiInfo;");
		jobject wifiinfo = env->CallObjectMethod(wifi_manager, mid);
		mid = env->GetMethodID(env->GetObjectClass(wifiinfo),
			"getMacAddress", "()Ljava/lang/String;");
		str = (jstring)env->CallObjectMethod(wifiinfo, mid);	
		len = env->GetStringUTFLength(str);
	}

	char* deviceId = (char*)calloc(len + 1, 1);
	env->GetStringUTFRegion(str, 0, len, deviceId);
	env->DeleteLocalRef(str);
	FString id(deviceId);
	free(deviceId);
	return FMD5::HashAnsiString(*id);
#else
	return FPlatformMisc::GetHashedMacAddressString();
#endif
}
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_089a4978](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_089a4978](https://forums.unrealengine.com/u/anonymous_user_089a4978)\
**Post date:** [October 9, 2016, 3:59pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/6 "2016-10-09T15:59:34Z")

</div>

Hi ,  
Thanks very much for this solution! 🙂  
Do you have an idea to implement this kind of method in iOS too?

---

<div class="post-metadata">

**Author:** ![zshj0914](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@zshj0914](https://forums.unrealengine.com/u/zshj0914)\
**Post date:** [November 17, 2016, 11:12pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/7 "2016-11-17T23:12:51Z")

</div>

hi , i tried these code on our project and it works fine on PC, but on gearVR, everytime we run these, it said “unexpected error” and the game crashed. do you have any clue what could possibly cause this? we are using unreal 4.12 source compiled version.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_089a4978](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_089a4978](https://forums.unrealengine.com/u/anonymous_user_089a4978)\
**Post date:** [February 12, 2017, 3:13pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/8 "2017-02-12T15:13:09Z")

</div>

Hi , after collecting thousands of data from our games downloads, I noticed that this function is causing lots of crashes in certain devices, which are 10% of all downloads.

Have a look at this: [Crash on many Android devices - World Creation - Epic Developer Community Forums](https://answers.unrealengine.com/questions/551772/crash-on-many-android-devices.html)

Do you have any idea about where the problem is?

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [February 12, 2017, 10:38pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/9 "2017-02-12T22:38:34Z")

</div>

This code also works on IOS and other devices because unreal support them natively.

```
#else
     return FPlatformMisc::GetHashedMacAddressString();
 #endif
```

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [February 12, 2017, 10:45pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/10 "2017-02-12T22:45:22Z")

</div>

Hello can you send the crash report?

---

<div class="post-metadata">

**Author:** ![Kochol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kochol](https://forums.unrealengine.com/u/Kochol)\
**Post date:** [February 12, 2017, 10:58pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/11 "2017-02-12T22:58:48Z")

</div>

Sorry for the problem.

I don’t have any device that gets a crash on this code if you have one, please help me to fix this bug.

I read your crash reports and it crashes on GetStringUTFLength function but we use this function two times in our code can you put some logs to see which one cause the crashes.

Please test below code on the device.

```
     jsize len = 0;
     if (str)
     	len = env->GetStringUTFLength(str);
 
     if (len == 0)
     {
         // Try to get the wifi mac address.
	 // On some devices maybe it fails if the wifi is switched off.
         jobject wifi_manager = env->CallObjectMethod(activity, mid,
             env->NewStringUTF("wifi"));
         mid = env->GetMethodID(env->GetObjectClass(wifi_manager),
             "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;");
         jobject wifiinfo = env->CallObjectMethod(wifi_manager, mid);
         mid = env->GetMethodID(env->GetObjectClass(wifiinfo),
             "getMacAddress", "()Ljava/lang/String;");
         str = (jstring)env->CallObjectMethod(wifiinfo, mid);
	 if (str)    
         	len = env->GetStringUTFLength(str);
     }
     
     if (len == 0)
     {
         // TODO: If two above methods fail we must get the id from Android ID.
	 // This ID is very stable but it will be changed with every OS update.
	 //
     }
 
     // Same as before
```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_089a4978](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_089a4978](https://forums.unrealengine.com/u/anonymous_user_089a4978)\
**Post date:** [September 30, 2018, 2:29pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/12 "2018-09-30T14:29:18Z")

</div>

Hi , thank you very much for your code, we are using it for 2 years. However, now we upgraded to 4.20 and we have a crash in this code. Could you please have a look at here?

> **[Crash at C++ line: CallObjectMethod(...);](https://forums.unrealengine.com/questions/833129/crash-at-c-line-callobjectmethod.html)**
>
> Bold line below was working fine in 4.15. However, this line is crashing in 4.20: JNIEnv\* env = FAndroidApplication::GetJavaEnv(); jobject activity = FAndroidApplication::GetGameActivityThis(); jmethodID mid =...

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [October 1, 2018, 12:48pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/13 "2018-10-01T12:48:09Z")

</div>

UE4 already binds GetDeviceId function mentioned above

FAndroidMisc::GetDeviceId()

look up FAndroidMisc there more functions

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_089a4978](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_089a4978](https://forums.unrealengine.com/u/anonymous_user_089a4978)\
**Post date:** [January 1, 2019, 3:31pm UTC](https://forums.unrealengine.com/t/is-there-anyway-to-get-a-unique-device-id-on-android/341411/14 "2019-01-01T15:31:35Z")

</div>

WELL, I FOUND OUT THAT THIS FUNCTION WON’T WORK ANDROID SDK 6.0 AND ABOVE.

> <https://stackoverflow.com/questions/33078003/android-6-0-permission-error>

You need to request runtime permission but it is a terrible way to get device id. Since therefore Google doesn’t accept apks below Android SDK 8.0, this code is therefore useless at all. We had to switch to Unreal’s getDeviceIdentifier method. Our databases are messed up because of this, everyones device id has changed ☹
