Unique android device id
-
Hello. How to get unique android device id?
-
Hi @m-kuncevicius
I'm not a mobile developer, but this thread seems to have a good answer:
https://forum.qt.io/topic/75670/i-need-to-get-a-serial-number-of-deviceDo note, that the Device id can change if the end user wipes their device.
-
@tekojo Thanks for link. I'm going to use Secure Android ID (Device Id). Also I found a good article about it. How to retrieve an Unique ID to identify Android devices ?
-
Hello all, I'm doing something like that, just in case is helpfull for someone; this tries to got MAC, then ANDROID_ID then RandomId and return sha1 hash of this number. Also I'm saving it somewhere just in case I'm working in a device which doesn't has MAC neither ANDROID_ID and because of that I'm working just with random id.
QString AndroidUtils::_getAndroidUuid(){ // Trying to get MAC Address Q_FOREACH(QNetworkInterface netInterface, QNetworkInterface::allInterfaces()) { // Return only the first non-loopback MAC Address if (!(netInterface.flags() & QNetworkInterface::IsLoopBack)) { QString macAddress = netInterface.hardwareAddress(); // On Android 6 you cannot retrieve MAC address with regular way, this is a workaround for getting it. if(macAddress.isEmpty()){ qDebug()<< Q_FUNC_INFO <<"MAC address cannot be retrieved with regular way, trying to obtain it from: /sys/class/net/"+netInterface.name()+"/address"; QFile macFile("/sys/class/net/"+netInterface.name()+"/address"); if (macFile.open(QFile::ReadOnly)){ QTextStream textStream(&macFile); macAddress= QString(textStream.readLine()); macFile.close(); } } if(!macAddress.isEmpty()&&!macAddress.endsWith("02:00:00:00:00:00")){ qDebug()<< Q_FUNC_INFO <<"Using MAC address"<<macAddress; return _getSha1Hash(macAddress); } qDebug()<< Q_FUNC_INFO <<"MAC Address is:"<<macAddress; } } // Trying to get ANDROID_ID from system QAndroidJniObject myID = QAndroidJniObject::fromString("android_id"); QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;"); QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;"); QAndroidJniObject androidId = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;", contentR.object<jobject>(), myID.object<jstring>()); if(!_isJvmException()){ if(androidId.isValid() && !androidId.toString().isEmpty()){ if(androidId.toString().endsWith("9774d56d682e549c")){ qWarning()<< Q_FUNC_INFO <<"This device has the bug with unique id"; //https://code.google.com/p/android/issues/detail?id=10603 }else{ qDebug()<< Q_FUNC_INFO <<"Using androidId"<<androidId.toString(); return _getSha1Hash(androidId.toString()); } } } qDebug()<< Q_FUNC_INFO <<"Using randomUuid"; return _getSha1Hash(QUuid::createUuid().toString()); }
-
Hello all, I'm doing something like that, just in case is helpfull for someone; this tries to got MAC, then ANDROID_ID then RandomId and return sha1 hash of this number. Also I'm saving it somewhere just in case I'm working in a device which doesn't has MAC neither ANDROID_ID and because of that I'm working just with random id.
QString AndroidUtils::_getAndroidUuid(){ // Trying to get MAC Address Q_FOREACH(QNetworkInterface netInterface, QNetworkInterface::allInterfaces()) { // Return only the first non-loopback MAC Address if (!(netInterface.flags() & QNetworkInterface::IsLoopBack)) { QString macAddress = netInterface.hardwareAddress(); // On Android 6 you cannot retrieve MAC address with regular way, this is a workaround for getting it. if(macAddress.isEmpty()){ qDebug()<< Q_FUNC_INFO <<"MAC address cannot be retrieved with regular way, trying to obtain it from: /sys/class/net/"+netInterface.name()+"/address"; QFile macFile("/sys/class/net/"+netInterface.name()+"/address"); if (macFile.open(QFile::ReadOnly)){ QTextStream textStream(&macFile); macAddress= QString(textStream.readLine()); macFile.close(); } } if(!macAddress.isEmpty()&&!macAddress.endsWith("02:00:00:00:00:00")){ qDebug()<< Q_FUNC_INFO <<"Using MAC address"<<macAddress; return _getSha1Hash(macAddress); } qDebug()<< Q_FUNC_INFO <<"MAC Address is:"<<macAddress; } } // Trying to get ANDROID_ID from system QAndroidJniObject myID = QAndroidJniObject::fromString("android_id"); QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;"); QAndroidJniObject contentR = appctx.callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;"); QAndroidJniObject androidId = QAndroidJniObject::callStaticObjectMethod("android/provider/Settings$Secure","getString", "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;", contentR.object<jobject>(), myID.object<jstring>()); if(!_isJvmException()){ if(androidId.isValid() && !androidId.toString().isEmpty()){ if(androidId.toString().endsWith("9774d56d682e549c")){ qWarning()<< Q_FUNC_INFO <<"This device has the bug with unique id"; //https://code.google.com/p/android/issues/detail?id=10603 }else{ qDebug()<< Q_FUNC_INFO <<"Using androidId"<<androidId.toString(); return _getSha1Hash(androidId.toString()); } } } qDebug()<< Q_FUNC_INFO <<"Using randomUuid"; return _getSha1Hash(QUuid::createUuid().toString()); }
@delamor cool - thanks for your solution. In an upcoming app I'll need this.
do you also have something similar for iOS ? -
Hi all,
V-Play Engine also allows to use a Unique Device ID, with different implementations to retrieve a unique id on each target platform.import VPlayApps 1.0 App { AppText { text: "Device Id: " + system.UDID } }
Best,
GT -
@delamor cool - thanks for your solution. In an upcoming app I'll need this.
do you also have something similar for iOS ?@ekkescorner do you have the code for this on some of Your Github repos?
-
@ekkescorner do you have the code for this on some of Your Github repos?
@Aleksey_K-0 I'm using instance IDs. works on Android, iOS, W10, ...
see also https://developer.android.com/training/articles/user-data-ids -
@ekkescorner so You just create random UUID once and store it on the device forever?
-
@ekkescorner so You just create random UUID once and store it on the device forever?
@Aleksey_K-0 yep. store at app data location