简体   繁体   中英

How to validate the bluetooth MAC address correctly?

I have a developer's options pane inside my app, where a developer can input test MAC address of the device. The question is how to validate it correctly? Does Android have some methods out of the box to do this?

Thank everybody for your help. I found the solution. BluetoothAdapter.checkBluetoothAddress(String) validates the MAC address. It checks if MAC matches the pattern given here , if length equals 17 chars, if all letters are uppercase and whether all chars are hex chars.

MAC addresses have this format:

String formatMAC = "%02X:%02X:%02X:%02X:%02X:%02X";

So you can check that a device's MAC address matches this format using String's matches() method:

public boolean matches(String regex) {
    return Pattern.matches(regex, this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM