Android Question Wrong BT MAC address with Serial on Android 6

FrankBerra

Active Member
Licensed User
Longtime User
Hi all

Some weeks ago i upgraded to Android M. Everything was OK before but now i doscovered that the Serial library doesn't show the correct BT MAC address of my device.

The code i use is simple:
B4X:
Dim MacBluetooth As Serial
MacBluetooth.Initialize("test")
ToastMessageShow(MacBluetooth.Address, True)

It displays "02:00:00:00:00:00" instead of the correct MAC address.

I am using Serial V. 1.23
Is out there another updated version?
How to get the mac address with other methods (like reflection)?

Thanks in advance
 

DonManfred

Expert
Licensed User
Longtime User
the link is broken/page not found
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject = Me
   Log(jo.RunMethod("getWifiMacAddress", Null))
End Sub

#if JAVA
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
public static String getWifiMacAddress() {
  try {
  String interfaceName = "wlan0";
  List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
  for (NetworkInterface intf : interfaces) {
  if (!intf.getName().equalsIgnoreCase(interfaceName)){
  continue;
  }

  byte[] mac = intf.getHardwareAddress();
  if (mac==null){
  return "";
  }

  StringBuilder buf = new StringBuilder();
  for (byte aMac : mac) {
  buf.append(String.format("%02X:", aMac));
  }
  if (buf.length()>0) {
  buf.deleteCharAt(buf.length() - 1);
  }
  return buf.toString();
  }
  } catch (Exception ex) {
     ex.printStackTrace();
  }
  return "";
   }
#end if
 
Upvote 0

Dey

Active Member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim jo As JavaObject = Me
   Log(jo.RunMethod("getWifiMacAddress", Null))
End Sub

#if JAVA
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
public static String getWifiMacAddress() {
  try {
  String interfaceName = "wlan0";
  List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
  for (NetworkInterface intf : interfaces) {
  if (!intf.getName().equalsIgnoreCase(interfaceName)){
  continue;
  }

  byte[] mac = intf.getHardwareAddress();
  if (mac==null){
  return "";
  }

  StringBuilder buf = new StringBuilder();
  for (byte aMac : mac) {
  buf.append(String.format("%02X:", aMac));
  }
  if (buf.length()>0) {
  buf.deleteCharAt(buf.length() - 1);
  }
  return buf.toString();
  }
  } catch (Exception ex) {
     ex.printStackTrace();
  }
  return "";
   }
#end if

Erel Thanks for the answer
with Genymotion emulator returns empty string
I do not have the ability to test on devices

change wlan0 in eth1 ok
String interfaceName = "eth1";
 
Upvote 0

johan vetsuypens

Member
Licensed User
Longtime User
The above Java source from Erel is getting the Wifi Mac address on Android 6.0. Does somebody has the Java code to get the Bluetooth adapter address via the inline Java code ?
Thanks,
Johan
 
Upvote 0

postasat

Active Member
Licensed User
Longtime User
Hi,

I have an app that use a list of MAC bluetooth address stored, and the user choose to connect to one of them.

I use as example
B4X:
'button1
serial1.Connect(macphone1)
'button2
serial1.Connect(macphone2)
'button3
serial1.Connect(macphone3)

Now with Android 6 how can I store address to connect only one specific device as above ?
Is there any possibilities to reach this goal ?

Thank you.
 
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
In android 6+ you can't get your own Bluetooth MAC but you are still able to scan for surrounding BT adapters and connect to them
 
Upvote 0

postasat

Active Member
Licensed User
Longtime User
Thank you for your answer.

If I have my phone in a place where there are 10 phone with bluetooth on and I want to connect only to 2 of them which I know, how can I do ?
Can I do it automatically ?
 
Upvote 0

Arf

Well-Known Member
Licensed User
Longtime User
Hi All,
This method is working for me, I am not sure how reliable it is though:

B4X:
Sub GetMac As String
   Dim p As Phone
   Dim provider As String = p.GetSettings("bluetooth_address")
   Return provider
End Sub

If anyone can comment on the theoretical reliability of this method, I'd be very pleased to hear your thoughts.
 
Last edited:
Upvote 0

FrankBerra

Active Member
Licensed User
Longtime User
Whoah!
It is surprisingly working! (Device: Oneplus3, Android 7, no root)
Anyone else that can test?
Anyone expert that can explain why this is working even if getting Bluetooth Mac address is blocked by OS?
 
Upvote 0
Top