Hi,
First ,thanks Cocos team, great work!!
I want to know is there any method or simple way in cocos 3.3 to get Device Id(android or ios)??
I read How get the unique identify for phone device under ios or android ,but i can’t understand how to get Device Id.
please help me.
Thanks.
We don’t have this built in. Search these forums, I gave some solutions to this a while back, probably May or June.
By searching in internet and reading some document about jni and looking the cocos2d source code finally i could write a method to get Device Id (IMEI) for android,and now i write the codes for those who want to know.
1- add bellow codes to
Java_org_cocos2dx_lib_Cocos2dxHelper.h /
Java_org_cocos2dx_lib_Cocos2dxHelper.cpp
that exist in YOUR_COCOS_PROJECT\cocos2d\cocos\platform\android\jni path,
in header file
extern std::string GetIdJNI();
in cpp file
std::string GetIdJNI()
{
JniMethodInfo t;
std::string a("");
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxActivity", "GetId", "()Ljava/lang/String;"))
{
jstring jstr = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
a = JniHelper::jstring2string(jstr);
t.env->DeleteLocalRef(jstr);
return a;
}
return NULL;
}
2-now we must write java code to get IMEI for this insert the following codes to
YOUR_COCOS_PROJECT\cocos2d\cocos\platform\android\java\src\org\cocos2dx\lib\Cocos2dxActivity.java
public static String GetId()
{
try
{
TelephonyManager tManager = (TelephonyManager)sContext.getSystemService(sContext.TELEPHONY_SERVICE);
if(tManager == null)
{
return "TelephonyManager is Null";
}
String uid = tManager.getDeviceId();
return uid;
}
catch(Exception e)
{
return e.getMessage();
}
}
3-In the end we must use GetIdJNI(); method to get IMEI, for this purpose add the following files to YOUR_COCOS_PROJECT\cocos2d\cocos\platform\CCDevice.h
YOUR_COCOS_PROJECT\cocos2d\cocos\platform\android\CCDevice-android.cpp
in header file
static std::string GetId();
in cpp file
std::string Device::GetId()
{
static std::string Id = "";
if (Id == "")
{
Id = GetIdJNI();
}
return Id;
}
Now everything is ready to Get IMEI ,Just call Device::GetId();
in your project.
Putting your code directly into cocos2d-x is not a very good practice. It makes it harder for you to upgrade in the future. You should only use that as last resort.
I implemented that on iOS and Android in FenneX recently, here is the relevant commit: https://github.com/FenneX/FenneX/commit/baf1d19d8e90e7b41f7d860c3c1c75be95fcd78e
Using the IMEI is not ideal, since it requires READ_PHONE_STATE permission. Using Android_ID doesn’t require any permission. For a full overview of device identification on Android, see this thread: http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
On iOS, please note that the ID will be unique only for your apps. The only way to have a global ID is to use the advertising identifier, which requires you to use ads in your apps (I do not, so I can’t use it).
Hi Fradow,
Can you confirm if I only need to copy the following files for Unique ID functionality to work.
FenneX/Classes/FenneX/NativeWrappers/FenneX.h
FenneX/Classes/FenneX/NativeUtility.h
FenneX/proj.android/jni/NativeUtility.cpp
Do I need to configure anything else for this to work on Cocos2dX C++ v3.10