我正在做一个 android 应用程序,它可以从蓝牙接收数据并将其显示为波形。现在的问题是我想在文本视图中显示数据以确认数据是否正确,但我显示的内容无法识别(如@&zA ...)。任何人都可以帮助将数据转换为 8 位值?谢谢你 !
相关编码如下图:
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what==READ) {
String str = (String)msg.obj;
textView1.setText(str);
}
super.handleMessage(msg);
}
};
private class ConnectedThread extends Thread {
private final InputStream mmInStream;
public ConnectedThread(BluetoothSocket socket) {
InputStream tmpIn = null;
try {
tmpIn = socket.getInputStream();
}catch (IOException e) { }
mmInStream = tmpIn;
}
public void run() {
byte[] buffer = new byte[5];
int bytes; // bytes returned from read()
// Keep listening to the InputStream until an exception occurs
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI activity
String str = new String(buffer);
temp = byteToInt(buffer); //Convert byte to int
handler.obtainMessage(READ, bytes, -1, str).sendToTarget();
}catch (Exception e) {
System.out.print("read error");
break;
}
}
}
}