Android Question MAC address Android 6

Rusty

Well-Known Member
Licensed User
Longtime User
I found the following code on the internet which purportedly allows access to the MAC address on Android 6.
I've tried to embed it into my code but it fails.
B4X:
#if java
    public static String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(Integer.toHexString(b & 0xFF) + ":");
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "02:00:00:00:00:00";
}
    #end if
   
Sub GetMAC As String
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("GetMacAddr", Null)
    Return s
End Sub

It says:
error: cannot find symbol
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());

any suggestions would be appreciated.
Rusty
 

JordiCP

Expert
Licensed User
Longtime User
You need to import the classes used

B4X:
#if java

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

    public static String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0"))
            continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(Integer.toHexString(b & 0xFF) + ":");
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "02:00:00:00:00:00";
}
    #end if
  
Sub GetMAC As String
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("GetMacAddr", Null)
    Return s
End Sub
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Hi guys, I am trying to use this method to get the bluetooth MAC in android 6. I've put the following in a service, when I call newGetMAC, I get the errors in the second block of text below. Any help appreciated. I added the JavaObject library.

B4X:
#if java

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

    public static String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0"))
            continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(Integer.toHexString(b & 0xFF) + ":");
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "02:00:00:00:00:00";
}
#end if

Sub newGetMAC As String
    Private NativeMe As JavaObject
    NativeMe.InitializeContext
    Dim s As String = NativeMe.RunMethod("GetMacAddr", Null)
    Return s
End Sub

 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Java syntax is case-sensitive, so you should call
B4X:
Dim s AsString = NativeMe.RunMethod("getMacAddr", Null)
 
Reactions: Arf
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Moved to main and fixed the case - all working, thank you very much.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
DISASTER!!!
This method works in debug mode, but not in release mode.
Can anyone help? My client is midway through a product trial and this is going to ruin it unless I fix it asap.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Oops - so that code gets the wifi MAC ("wlan0").
What interface name should I use to get the bluetooth adaptors MAC? I've tried "bth0" but that didn't work.
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Mmm, I got a list of network adaptors and the bluetooth one is not in there, so I guess there really is no way to get the bluetooth MAC.

I am debating working on the assumption that the bluetooth MAC is (wlan0_MAC - 1), seems true for all my devices, but risky I'm sure.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…