Answer the question
In order to leave comments, you need to log in
What is the best way to store the MAC address in the database?
In particular, it is necessary to store BD_ADDR in Sqlite, but the question is also interesting for the general case.
I understand that you can convert it to INTEGER as you like, or store it in TEXT just as it is.
Since the address formats are standardized (EUI-48, EUI-64), maybe there are generic storage solutions? It would suit the conversion by some general convention to INTEGER, since it is logical to make addresses the keys of the table. But it was not possible to google something like that.
Answer the question
In order to leave comments, you need to log in
Resolved, MAC address to be stored will be stored in INTEGER Sqlite (long in Java)
made its own conversion "readable MAC address" <=> long , for android:
BluetoothDevice device;
final String mac = device.getAddress();
final long id = Long.parseLong(mac.replaceAll(":", ""), 16);
final String back_mac = idToBD_ADDR(id);
private static String idToBD_ADDR(long id) {
final byte LAST_BYTE = 5;
StringBuilder address = new StringBuilder();
for (byte i = 0; i <= LAST_BYTE; i++) {
byte b = (byte) (id >> 8*i);
address.append(new StringBuilder(String.format("%02x", b)).reverse());
if (i != LAST_BYTE) address.append(":");
}
return address.reverse().toString();
}
Because the MAC address is a 6-byte number, it's easiest to convert it to an 8-byte INTEGER. For example, 12:34:56:78:9A:BC becomes 0x1234'5678'9ABC. It is effective and a question only in manual editing of a DB.
It was not possible to google because the MAC address is an Int, in which the first two bytes are not significant, and the rest are written in hex. 28:d3:44:16:69:54 => 0x000028d344166954.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question