Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 532
Nazarii Moshenskiy Avatar asked Nov 20 '19 10:11

Nazarii Moshenskiy


People also ask

How do you validate a MAC address?

One way to represent them is to form six pairs of the characters separated with a hyphen (-) or colon(:). For example, 01-23-45-67-89-AB is a valid MAC address. Another way to represent them is to form three groups of four hexadecimal digits separated by dots(.). For example, 0123.4567.

What does a valid MAC address look like?

A MAC address is a 48-bit hexadecimal address. It's usually six sets of two digits or characters, separated by colons. An example MAC address would be 00:00:5e:00:53:af. Many network card and other hardware manufacturers use a unique sequence at the beginning of their products' MAC addresses.

Does Bluetooth Show MAC address?

Bluetooth devices shows as MAC addresses, and only after a couple refreshes only SOME of them have names.


2 Answers

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.

like image 186
Nazarii Moshenskiy Avatar answered Sep 28 '22 16:09

Nazarii Moshenskiy


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);
}
like image 23
matdev Avatar answered Sep 28 '22 15:09

matdev