Reputation: 1
Problem Description:
The android app communicates with the peer microcontroller through Bluetooth, The data of the single chip microcomputer comes from the serial port, and then sends it to the android app through Bluetooth SPP,
ps: this single chip microcomputer contain a bluetooth module.
The current problem is: The peer data received by the Bluetooth app is all garbled characters:
The serial port program settings are as follows: Baud rate: 115200 Data bits: 8 Stop bits: 1 Parity: None Flow control: None
HEX send data like below: FE 01 02 enter image description here
The expectation is: The android app can receive the following string: 0xFE 0x12 0x02
enter image description here But currently the app receives garbled characters, After reading the byte stream of inputStream.read is -1,0,0..., Personally, I feel very weird. I don’t know why the byte stream sent by the board is like this.
try:
Try to process the received byte stream with different encodings, such as UTF8/ASCII/UNICODE, but it is useless;
Try to send in non-HEX, that is, string: FE 01 02, and found the same problem;
Do you feel that the android Bluetooth SPP/RFCOM needs to set the same serial port settings as the peer? But the current problem is that the set interface cannot be found: BluetoothSocket.setBaudRate?/setUartConfig? -- there is no such interface actually
At present, I feel that there may be two problems: i. The data from the peer MCU is not sent through Bluetooth at all, that is, the sensor data of the underlying LoRa-01 MCU is transmitted to its classic Bluetooth module SPP through Uart. There may be a problem, but there is no code for this piece.
I'm not sure if this is the problem. ii. The data from the peer MCU is sent via bluetooth, but its data encoding is completely incompatible with android? What android receives via bluetooth is a byte stream.
Please take a look at the above questions, and wait online anxiously! Thanks you!
in MyActivity.mHandler:
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
in BtClientService.ConnectThread
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created ~ " + e.toString());
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mState = STATE_CONNECTED;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (mState == STATE_CONNECTED) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected ~ " + e.toString());
connectionLost();
break;
}
}
}
And I just use different encoding from bytes to string like: utf8/ASCII/UNICODE, but it doesn't work.
//default encoding
String readMessage = new String(readBuf, 0, msg.arg1);
// convert to string Encode with UTF-8 format
String readMessage = new String(byteArray, StandardCharsets.UTF_8);
// convert to string Encode with ASCII format
String readMessage = new String(byteArray, StandardCharsets.US_ASCII);
// convert to string Encode with Unicode format
String readMessage = new String(byteArray, StandardCharsets.UTF_16);
Upvotes: 0
Views: 216
Reputation: 2662
You are trying to convert a byte array directly to a string. That will not give you the expected result. When you look at the received bytes directly you will most likely see that the send values have been received correctly, maybe just in a different format which entirely depends on how you represent it.
If you want to receive a HEX string from a byte array you would need to use something like this answer suggested:
final protected static char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length*2];
int v;
for(int j=0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j*2] = hexArray[v>>>4];
hexChars[j*2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
See this example for a test implementation
Upvotes: 0