Reputation: 405
I have a byte array, I need to convert it to hex values and send them through bluetooth. Everything I found is to convert bytes to hex string, but I can't do it because I can write only byte arrays or int.
public static byte[][] commandsArray = new byte[5][5];
...
connectedThr.write(ConstantsVariables.commandsArray[i]);
...
public void write(byte[] bytes) {
try {
mmOutStream.write(bytes);
} catch (IOException e) {
Log.d("CONNECTTHREAD","Could not write: " + e.toString());
}
}
Everything with sending is ok, I can write simple byte array. Problem is with hex values and how to send them.
UPD: I tried this code, but I have such logs:
09-26 20:39:57.324 26848-26848/shkatovl.btandroid I/TEST: 28
09-26 20:39:57.324 26848-26848/shkatovl.btandroid I/TEST___2: [B@3ae4f930
Formatter formatter = new Formatter();
formatter.format("%02x", ConstantsVariables.commandsArray[i][j]);
String hex = formatter.toString();
Log.i("TEST", hex);
Log.i("TEST___2", hex.getBytes().toString());
Upvotes: 0
Views: 438
Reputation: 3284
Have you tried the String class's getBytes()
method? There are also Similar methods on the Primitive Object Wrappers (Integer
etc.) if that is what you need.
Upvotes: 1