Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Unique android device id

Unique android device id

Scheduled Pinned Locked Moved Solved Mobile and Embedded
10 Posts 6 Posters 6.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    m.kuncevicius
    wrote on 13 Mar 2017, 14:40 last edited by
    #1

    Hello. How to get unique android device id?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tekojo
      wrote on 14 Mar 2017, 08:26 last edited by
      #2

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

      Do note, that the Device id can change if the end user wipes their device.

      1 Reply Last reply
      3
      • M Offline
        M Offline
        m.kuncevicius
        wrote on 14 Mar 2017, 09:15 last edited by
        #3

        @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 ?

        1 Reply Last reply
        1
        • D Offline
          D Offline
          delamor
          wrote on 21 Mar 2017, 15:24 last edited by
          #4

          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());
          
          }
          
          E 1 Reply Last reply 21 Mar 2017, 15:39
          6
          • D delamor
            21 Mar 2017, 15:24

            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());
            
            }
            
            E Online
            E Online
            ekkescorner
            Qt Champions 2016
            wrote on 21 Mar 2017, 15:39 last edited by
            #5

            @delamor cool - thanks for your solution. In an upcoming app I'll need this.
            do you also have something similar for iOS ?

            ekke ... Qt Champion 2016 | 2024 ... mobile business apps
            5.15 --> 6.8 https://t1p.de/ekkeChecklist
            QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

            Aleksey_K 0A 1 Reply Last reply 20 Dec 2018, 10:51
            1
            • GTDevG Offline
              GTDevG Offline
              GTDev
              wrote on 3 Apr 2017, 07:05 last edited by GTDev 4 Apr 2017, 11:36
              #6

              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

              Senior Developer at Felgo - https://felgo.com/qt

              Develop mobile Apps for iOS & Android with Qt
              Felgo is an official Qt Technology Partner

              1 Reply Last reply
              0
              • E ekkescorner
                21 Mar 2017, 15:39

                @delamor cool - thanks for your solution. In an upcoming app I'll need this.
                do you also have something similar for iOS ?

                Aleksey_K 0A Offline
                Aleksey_K 0A Offline
                Aleksey_K 0
                wrote on 20 Dec 2018, 10:51 last edited by
                #7

                @ekkescorner do you have the code for this on some of Your Github repos?

                E 1 Reply Last reply 20 Dec 2018, 11:08
                0
                • Aleksey_K 0A Aleksey_K 0
                  20 Dec 2018, 10:51

                  @ekkescorner do you have the code for this on some of Your Github repos?

                  E Online
                  E Online
                  ekkescorner
                  Qt Champions 2016
                  wrote on 20 Dec 2018, 11:08 last edited by
                  #8

                  @Aleksey_K-0 I'm using instance IDs. works on Android, iOS, W10, ...
                  see also https://developer.android.com/training/articles/user-data-ids

                  ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                  5.15 --> 6.8 https://t1p.de/ekkeChecklist
                  QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                  1 Reply Last reply
                  1
                  • Aleksey_K 0A Offline
                    Aleksey_K 0A Offline
                    Aleksey_K 0
                    wrote on 20 Dec 2018, 11:20 last edited by
                    #9

                    @ekkescorner so You just create random UUID once and store it on the device forever?

                    E 1 Reply Last reply 20 Dec 2018, 11:22
                    0
                    • Aleksey_K 0A Aleksey_K 0
                      20 Dec 2018, 11:20

                      @ekkescorner so You just create random UUID once and store it on the device forever?

                      E Online
                      E Online
                      ekkescorner
                      Qt Champions 2016
                      wrote on 20 Dec 2018, 11:22 last edited by
                      #10

                      @Aleksey_K-0 yep. store at app data location

                      ekke ... Qt Champion 2016 | 2024 ... mobile business apps
                      5.15 --> 6.8 https://t1p.de/ekkeChecklist
                      QMake --> CMake https://t1p.de/ekkeCMakeMobileApps

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved