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. How to get WiFi MAC address of Android device ?
Forum Updated to NodeBB v4.3 + New Features

How to get WiFi MAC address of Android device ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
17 Posts 7 Posters 8.1k Views 4 Watching
  • 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.
  • P Offline
    P Offline
    Pradeep P N
    wrote on 10 Feb 2018, 04:25 last edited by Pradeep P N 2 Oct 2018, 15:00
    #1

    Hi all,
    I want to get the MAC address from my android device can some one suggest me here.
    Bellow is my sample code

    QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();

    interfaceList.erase(std::remove_if(interfaceList.begin(), interfaceList.end(),
                                       [](const QNetworkInterface &iface) { return (iface.flags().testFlag(QNetworkInterface::IsLoopBack) || !iface.isValid()); }),
                        interfaceList.end());
    
    qWarning() << " List Count :: " << interfaceList.count() << endl;
    
    if (!interfaceList.isEmpty()) {
        const QString str = interfaceList.constFirst().hardwareAddress().remove(QLatin1Char(':'));
    
        qWarning() << " STR :: " << str << endl;
    
        return QByteArray::fromHex(str.toLatin1()).toStdString();
    }
    

    but the result what i get is

    W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:25 (std::string Widget::getMacAddress() const): List Count :: 1
    W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:30 (std::string Widget::getMacAddress() const): STR :: ""

    i also have permission enabled in my AndroidManifest.xml but nothing helped me here

    0_1518236725503_bb47db04-14be-439c-b260-7c3ed55e5b79-image.png

    Thank You in advance.

    Pradeep Nimbalkar.
    Upvote the answer(s) that helped you to solve the issue...
    Keep code clean.

    R 1 Reply Last reply 18 Jan 2019, 15:32
    0
    • V Offline
      V Offline
      Vinod Kuntoji
      wrote on 10 Feb 2018, 07:02 last edited by
      #2

      @Pradeep-P-N

      QString MainWindow::getMacAddress()
      {
      foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
      {
      if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
      return netInterface.hardwareAddress();
      }
      return QString();
      }

      C++, Qt, Qt Quick Developer,
      PthinkS, Bangalore

      P 1 Reply Last reply 10 Feb 2018, 14:59
      1
      • V Vinod Kuntoji
        10 Feb 2018, 07:02

        @Pradeep-P-N

        QString MainWindow::getMacAddress()
        {
        foreach(QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
        {
        if (!(netInterface.flags() & QNetworkInterface::IsLoopBack))
        return netInterface.hardwareAddress();
        }
        return QString();
        }

        P Offline
        P Offline
        Pradeep P N
        wrote on 10 Feb 2018, 14:59 last edited by
        #3

        @Vinod-Kuntoji did you try this code in Android ?

        My Question is clear I need to get MAC address of the Android device in which my code runs

        Pradeep Nimbalkar.
        Upvote the answer(s) that helped you to solve the issue...
        Keep code clean.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 12 Feb 2018, 02:10 last edited by
          #4

          This should print available network interface names and their hardware/MAC addresses.

          for(auto interface: QNetworkInterface::allInterfaces())
              qDebug() << interface.name() << interface.hardwareAddress();
          

          For WLAN you may do something like this:

          for(auto interface:  QNetworkInterface::allInterfaces())
          {
              if(interface.name() == QLatin1String("wlan0"))
              {
                  // do something with 'interface.hardwareAddress()'
          
                  break;
              }
          }
          

          Note that second code block assumes that device don't have multiple WLAN interfaces.

          P 1 Reply Last reply 12 Feb 2018, 04:11
          1
          • ? A Former User
            12 Feb 2018, 02:10

            This should print available network interface names and their hardware/MAC addresses.

            for(auto interface: QNetworkInterface::allInterfaces())
                qDebug() << interface.name() << interface.hardwareAddress();
            

            For WLAN you may do something like this:

            for(auto interface:  QNetworkInterface::allInterfaces())
            {
                if(interface.name() == QLatin1String("wlan0"))
                {
                    // do something with 'interface.hardwareAddress()'
            
                    break;
                }
            }
            

            Note that second code block assumes that device don't have multiple WLAN interfaces.

            P Offline
            P Offline
            Pradeep P N
            wrote on 12 Feb 2018, 04:11 last edited by
            #5

            @casdevel Thank you,
            but this doesn't provide me the WiFi MAC address of the Android device right ?

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on 12 Feb 2018, 06:25 last edited by
              #6

              In my case it does. I tested this code on my Android tablet.

              P 1 Reply Last reply 12 Feb 2018, 08:19
              1
              • ? A Former User
                12 Feb 2018, 06:25

                In my case it does. I tested this code on my Android tablet.

                P Offline
                P Offline
                Pradeep P N
                wrote on 12 Feb 2018, 08:19 last edited by
                #7

                Hi @casdevel

                Can you please tell me your Android version and which device are you using ?

                I use Galaxy S6 with Android 7.0
                the output is

                W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:45 (void Widget::getMacAddress() const): "lo" ""
                W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:45 (void Widget::getMacAddress() const): "wlan0" ""

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 12 Feb 2018, 10:08 last edited by
                  #8

                  Tablet is ASUS Fonepad 7 (FE170CG) and Android version is 4.4.2.

                  From log that you posted I see that network interfaces are recognized but hardware addresses are empty strings.
                  Here is log from my device so you can see what is missing:
                  D/libk012-server.so( 4097): (null):0 ((null)): "wlan0" "78:24:AF:65:B4:09"
                  D/libk012-server.so( 4097): (null):0 ((null)): "lo" "00:00:00:00:00:00"

                  I also played with user permissions to see if I can reproduce that empty string issue, but without success. Only permission needed for this to work is 'android.permission.INTERNET'.

                  Here is my development environment details if you are interested:
                  Qt Creator 5.10.0
                  Android SDK 26.1.1
                  Android NDK 16.1.4479499
                  OpenJDK 8

                  Android Studio is not installed, and development is done on Kubuntu 16.04.3 LTS.

                  M 1 Reply Last reply 12 Feb 2018, 14:21
                  0
                  • ? A Former User
                    12 Feb 2018, 10:08

                    Tablet is ASUS Fonepad 7 (FE170CG) and Android version is 4.4.2.

                    From log that you posted I see that network interfaces are recognized but hardware addresses are empty strings.
                    Here is log from my device so you can see what is missing:
                    D/libk012-server.so( 4097): (null):0 ((null)): "wlan0" "78:24:AF:65:B4:09"
                    D/libk012-server.so( 4097): (null):0 ((null)): "lo" "00:00:00:00:00:00"

                    I also played with user permissions to see if I can reproduce that empty string issue, but without success. Only permission needed for this to work is 'android.permission.INTERNET'.

                    Here is my development environment details if you are interested:
                    Qt Creator 5.10.0
                    Android SDK 26.1.1
                    Android NDK 16.1.4479499
                    OpenJDK 8

                    Android Studio is not installed, and development is done on Kubuntu 16.04.3 LTS.

                    M Offline
                    M Offline
                    mvuori
                    wrote on 12 Feb 2018, 14:21 last edited by
                    #9

                    Apparently, this behaviour changed when Android got a revised permission system for version 6. There are somewhat conflicting information in the net about this. Some claim that permissions in manifest are not enough, but you need to ask the user for the permission, some claim ways to get the MAC from IpV6 address... At least the problem is not related to Qt. Startt googling...

                    1 Reply Last reply
                    2
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on 13 Feb 2018, 03:07 last edited by
                      #10

                      @mvuori those changes shouldn't affect this particular problem, according to official docs: https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous.
                      If permissions are in play here dialog box should pop-up, so I guess something else is responsible for this issue.
                      Also I think that, at this point, Qt shouldn't be ruled out as a source of the problem.

                      P 1 Reply Last reply 20 Feb 2018, 08:01
                      0
                      • ? A Former User
                        13 Feb 2018, 03:07

                        @mvuori those changes shouldn't affect this particular problem, according to official docs: https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous.
                        If permissions are in play here dialog box should pop-up, so I guess something else is responsible for this issue.
                        Also I think that, at this point, Qt shouldn't be ruled out as a source of the problem.

                        P Offline
                        P Offline
                        Pradeep P N
                        wrote on 20 Feb 2018, 08:01 last edited by
                        #11

                        @casdevel
                        Even with the additional permission in manifest file i couldn't get any results.
                        Is it possible for you to try with Android 6.0 or latest ?
                        As i know the Android Update has removed its Access to BT and WiFi MAC Address.

                        Pradeep Nimbalkar.
                        Upvote the answer(s) that helped you to solve the issue...
                        Keep code clean.

                        1 Reply Last reply
                        1
                        • ? Offline
                          ? Offline
                          A Former User
                          wrote on 20 Feb 2018, 13:06 last edited by
                          #12

                          @Pradeep-P-N said in How to get WiFi MAC address of Android device ?:

                          Is it possible for you to try with Android 6.0 or latest ?

                          Unfortunately no, this is the only Android device that I have and latest supported Android version for this device is 5.0.

                          1 Reply Last reply
                          0
                          • Pablo J. RoginaP Offline
                            Pablo J. RoginaP Offline
                            Pablo J. Rogina
                            wrote on 20 Feb 2018, 13:52 last edited by
                            #13

                            @Pradeep-P-N it looks like the culprit is Android itself :-) as programmatic access to hardware MAC address was removed since version 6. You may want to see if this approach can help you.

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            P S 2 Replies Last reply 22 Feb 2018, 03:52
                            3
                            • Pablo J. RoginaP Pablo J. Rogina
                              20 Feb 2018, 13:52

                              @Pradeep-P-N it looks like the culprit is Android itself :-) as programmatic access to hardware MAC address was removed since version 6. You may want to see if this approach can help you.

                              P Offline
                              P Offline
                              Pradeep P N
                              wrote on 22 Feb 2018, 03:52 last edited by
                              #14

                              @Pablo-J.-Rogina
                              Thank you. I will check with the link :)

                              Pradeep Nimbalkar.
                              Upvote the answer(s) that helped you to solve the issue...
                              Keep code clean.

                              1 Reply Last reply
                              1
                              • Pablo J. RoginaP Pablo J. Rogina
                                20 Feb 2018, 13:52

                                @Pradeep-P-N it looks like the culprit is Android itself :-) as programmatic access to hardware MAC address was removed since version 6. You may want to see if this approach can help you.

                                S Offline
                                S Offline
                                sharath
                                wrote on 18 Jan 2019, 10:34 last edited by
                                #15

                                @Pablo-J.-Rogina I'm facing same problem, then is there any other technique to achieve this?

                                Pablo J. RoginaP 1 Reply Last reply 18 Jan 2019, 13:48
                                0
                                • S sharath
                                  18 Jan 2019, 10:34

                                  @Pablo-J.-Rogina I'm facing same problem, then is there any other technique to achieve this?

                                  Pablo J. RoginaP Offline
                                  Pablo J. RoginaP Offline
                                  Pablo J. Rogina
                                  wrote on 18 Jan 2019, 13:48 last edited by
                                  #16

                                  @sharath said in How to get WiFi MAC address of Android device ?:

                                  I'm facing same problem,

                                  Have you tried the approach I suggested? If so, what's the outcome>?

                                  Upvote the answer(s) that helped you solve the issue
                                  Use "Topic Tools" button to mark your post as Solved
                                  Add screenshots via postimage.org
                                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  0
                                  • P Pradeep P N
                                    10 Feb 2018, 04:25

                                    Hi all,
                                    I want to get the MAC address from my android device can some one suggest me here.
                                    Bellow is my sample code

                                    QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces();

                                    interfaceList.erase(std::remove_if(interfaceList.begin(), interfaceList.end(),
                                                                       [](const QNetworkInterface &iface) { return (iface.flags().testFlag(QNetworkInterface::IsLoopBack) || !iface.isValid()); }),
                                                        interfaceList.end());
                                    
                                    qWarning() << " List Count :: " << interfaceList.count() << endl;
                                    
                                    if (!interfaceList.isEmpty()) {
                                        const QString str = interfaceList.constFirst().hardwareAddress().remove(QLatin1Char(':'));
                                    
                                        qWarning() << " STR :: " << str << endl;
                                    
                                        return QByteArray::fromHex(str.toLatin1()).toStdString();
                                    }
                                    

                                    but the result what i get is

                                    W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:25 (std::string Widget::getMacAddress() const): List Count :: 1
                                    W libMAcAddressTest.so: ..\MAcAddressTest\widget.cpp:30 (std::string Widget::getMacAddress() const): STR :: ""

                                    i also have permission enabled in my AndroidManifest.xml but nothing helped me here

                                    0_1518236725503_bb47db04-14be-439c-b260-7c3ed55e5b79-image.png

                                    Thank You in advance.

                                    R Offline
                                    R Offline
                                    raven-worx
                                    Moderators
                                    wrote on 18 Jan 2019, 15:32 last edited by raven-worx
                                    #17

                                    @Pradeep-P-N

                                    Since Android 6.0:

                                    https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-hardware-id

                                    To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

                                    To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:

                                    WifiManager.getScanResults()
                                    BluetoothDevice.ACTION_FOUND
                                    BluetoothLeScanner.startScan()

                                    Note: When a device running Android 6.0 (API level 23) initiates a background Wi-Fi or Bluetooth scan, the operation is visible to external devices as originating from a randomized MAC address.

                                    I recommend you use Java (JNI) to get the WiFi MAC Address. (Add android.permission.ACCESS_WIFI_STATE permission)

                                    WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                                    WifiInfo info = manager.getConnectionInfo();
                                    String address = info.getMacAddress();
                                    

                                    Note: some Android devices also have a wired network connection (mostly Android TV devices though)

                                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                    If you have a question please use the forum so others can benefit from the solution in the future

                                    1 Reply Last reply
                                    4

                                    • Login

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