x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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)

Product Version: UE 4.10
Tags:
more ▼

asked Dec 14 '15 at 10:04 AM in Blueprint Scripting

avatar image

m-motion
16 3 4 9

avatar image Kochol Mar 05 '16 at 03:21 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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 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
 }


[1]: https://wiki.unrealengine.com/Blueprint_Function_Library,_Create_Your_Own_to_Share_With_Others

more ▼

answered Mar 07 '16 at 04:38 AM

avatar image

Kochol
78 2 8 9

avatar image Kochol Mar 18 '16 at 03:39 PM

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

avatar image SalihBalkan Feb 12 '17 at 03:13 PM

Hi kochol, 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: https://answers.unrealengine.com/questions/551772/crash-on-many-android-devices.html

Do you have any idea about where the problem is?

avatar image Kochol Feb 12 '17 at 10:58 PM

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

avatar image Kochol Mar 19 '16 at 09:16 PM

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
 }
 
avatar image SalihBalkan Oct 09 '16 at 03:59 PM

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

avatar image Kochol Feb 12 '17 at 10:38 PM

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

 #else
      return FPlatformMisc::GetHashedMacAddressString();
  #endif
avatar image zshj0914 Nov 17 '16 at 11:12 PM

hi kochol, 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.

avatar image Kochol Feb 12 '17 at 10:45 PM

Hello can you send the crash report?

avatar image SalihBalkan Sep 30 '18 at 02:29 PM

Hi Kochol, 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?

https://answers.unrealengine.com/questions/833129/crash-at-c-line-callobjectmethod.html

avatar image SalihBalkan Jan 01 '19 at 03:31 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

UE4 already binds GetDeviceId function mentioned above

FAndroidMisc::GetDeviceId()

look up FAndroidMisc there more functions

more ▼

answered Oct 01 '18 at 12:48 PM

avatar image

Shadowriver
35.9k 929 169 1105

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question