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

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)

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

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

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
}

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

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
}

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

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.

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

Do you have any idea about where the problem is?

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

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

Hello can you send the crash report?

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

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?

UE4 already binds GetDeviceId function mentioned above

FAndroidMisc::GetDeviceId()

look up FAndroidMisc there more functions

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

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 :frowning: