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());
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
Error occurred on line: 570 (Unit_Comms)
java.lang.RuntimeException: Method: GetMacAddr not found in: GoSpiro.MACset.unit_comms
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
at GoSpiro.MACset.unit_comms._packethandler(unit_comms.java:560)
at GoSpiro.MACset.unit_comms._astream_newdata(unit_comms.java:527)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA$2.run(BA.java:328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
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.
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.