I am trying to send data over bluetooth spp from an Android app. The code below takes a string, which works perfectly. Whatever I put in I get on hyperterminal at the other end of the connection.
public void write(String message) {
byte[] msgBuffer = message.getBytes();
try {
mmOutStream.write(msgBuffer);
} catch (IOException e) {
Log.d(TAG, "Error data send");
}
}
However, I want to be able to send a hex value such as 0x41 for an 'A' in ASCII. How would I go about implementing this so that I could convert a string of say "41 7E" to the values 0x41 and 0x7E and then send them as is, so that on hyperterminal they come up as "A~"? I have tried using an example below but no success.
public void writeHex(String str){
char[] buffer = str.toCharArray();
byte[] b = new byte[buffer.length];
for (int k=0; k < b.length; k++)
{
b[k] = (byte) buffer[k];
try{
mmOutStream.write(b);
}catch (IOException e){
Log.d(TAG, "Error data send");
}
}
}
The values to be sent are read in from and editText when a button 'send' with an onClickListener is triggered. I have already formatted the editText so that only characters 0-9 and A-F are valid.
hexSend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String hex = hexInput.getText().toString();
mConnectedThread.writeHex(hex);
Toast.makeText(getBaseContext(), "Hex Data Sent", Toast.LENGTH_SHORT).show();
}
});