1
 WifiManager wm = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
 String macAddress = wm.getConnectionInfo().getMacAddress();

it is a String in hex format, for example:

"00:23:76:B7:2B:4D"

I want to convert this string into a byte array so that I can use MessageDigest sha1 on it

I got it worked in Python by using the excaping \x instead of : using the hashlib module.

But I would I do it in android/java? Thanks!

4 Answers 4

3

This code:

Byte.parseByte(mac[i], 16);

Not parse correctly hexadecimals numbers started with letters: "AE", "EF", etc...
Revised code:

WifiManager wm = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
if (wm != null) {
    String[] mac = wm.getConnectionInfo().getMacAddress().split(":");
    byte[] macAddress = new byte[6];        // mac.length == 6 bytes
    for(int i = 0; i < mac.length; i++) {
        macAddress[i] = Integer.decode("0x" + mac[i]).byteValue();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0
WifiManager wm = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
byte[] macAddress = wm.getConnectionInfo().getMacAddress().getBytes();

Revised Solution:

WifiManager wm = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
String[] mac = wm.getConnectionInfo().getMacAddress().split(":");
byte[] macAddress = new byte[6];
for(int i = 0; i < mac.length; i++) {            
    macAddress[i] = Byte.parseByte(mac[i], 16);
}

1 Comment

In this context, getBytes will return the 34 Unicode code points rather than the 6 bytes of the MAC address.
0

By this you get mac address in byte array so you no need to convert it.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class App{

   public static void main(String[] args){

    InetAddress ip;
    try {

        ip = InetAddress.getLocalHost();
        System.out.println("Current IP address : " + ip.getHostAddress());

        NetworkInterface network = NetworkInterface.getByInetAddress(ip);

        byte[] mac = network.getHardwareAddress();

        System.out.print("Current MAC address : ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
            sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
        }
        System.out.println(sb.toString());

    } catch (UnknownHostException e) {

        e.printStackTrace();

    } catch (SocketException e){

        e.printStackTrace();

    }

   }

}

/Copy from here : copy from http://www.mkyong.com/java/how-to-get-mac-address-in-java/comment-page-1/#comment-139182/

Comments

0

In android API level 28, there is an easier way of doing this: https://developer.android.com/reference/android/net/MacAddress.

android.net.MacAddress.fromString(s).toByteArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.