B4J Question [SOLVED] Get a unique ID of a device using MAC Address... (continue)

Magma

Well-Known Member
Licensed User
Longtime User
Hi there...

is a continue of:
https://www.b4x.com/android/forum/threads/get-a-unique-id-of-a-device.84189/#post-533568

B4X:
Sub AppStart (Form1 As Form, Args() As String)

    Dim jo As JavaObject = Me
    jo.RunMethod("PrintAllInterfaces", Null)

End Sub


Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub
#if JAVA


import java.net.NetworkInterface;
import java.util.Enumeration;

public static void PrintAllInterfaces() throws Exception {
      for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();)
     {
          NetworkInterface network = e.nextElement();
        byte[] mac = network.getHardwareAddress();
     
                if (mac != null)  {
                 
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                          sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));      
                      }
                    //Interface properties
                    System.out.println("Current MAC address : " + sb.toString() +"\r\n");
                    System.out.println(network.getDisplayName());
                    System.out.println(network.getName());                     
                 
                }   
      }
}
#End If

That is ok printing at logs ... but when tried to have in string not coming into it :-(

B4X:
Sub getmacs As String
    Dim jo As JavaObject = Me
    Return jo.RunMethod("PrintAllInterfaces", Null)
End Sub

Seems that i must change something to java... but can't find what - anyone can help... ?
 

DonManfred

Expert
Licensed User
Longtime User
Raise a even in PrintAllInterfaces() giving the text and capture it then in the Event...

The method PrintAllInterfaces does not return anything. It is declared as void....
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
Hi there...

B4X:
Sub getmacs As String
    Dim jo As JavaObject = Me
    Return jo.RunMethod("PrintAllInterfaces", Null)
End Sub

Seems that i must change something to java... but can't find what - anyone can help... ?

This returns the first MAC Address found:

B4X:
Private id_MACAddress As String
Dim jo As JavaObject = Me
id_MACAddress=jo.RunMethod("PrintAllInterfaces2", Null)

B4X:
import java.net.NetworkInterface;
import java.util.Enumeration;

public static String PrintAllInterfaces2() throws Exception {
      for (Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces(); e.hasMoreElements();)
      {
           NetworkInterface network = e.nextElement();
        byte[] mac = network.getHardwareAddress();
        int j=0;
     
              if ((mac != null) && (j==0))  {
             
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));     
                    }
                 j=j+1;
                 //Interface properties
                  System.out.println(mac +" " + mac.length +"\r\n");
                 System.out.println("Current MAC address : " + sb.toString() +"\r\n");
                 System.out.println(network.getDisplayName());
                 System.out.println(network.getName()); 
                 System.out.println(network);              
                 return sb.toString();
            }
             
            }
     return "";
}
#End if
 
Upvote 0

behnam_tr

Active Member
Licensed User
 
Upvote 0
Top