该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
单片机和安卓手机进行蓝牙通信,发送电压数据到手机,恰好每次8位数据(1字节)传给上位机。采用以下代码获得16进制字符输出,以下是蓝牙通信接收并显示数据部分代码
case Constants.MESSAGE_READ:
// construct a string from the valid bytes in the buffer
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
break;
我想将字节数组数据转换为十进制,做进一步计算,计算结果以十进制打印输出。代码如下:
case Constants.MESSAGE_READ:
if (!msg.obj.equals(null)) {
byte[] readBuf = (byte[]) msg.obj;
for (int i = 0; i < readBuf.length; i++) {
int[] a = new int[readBuf.length];
a[i] = readBuf[i] * 5000 / 255;
mConversationArrayAdapter.add(mConnectedDeviceName + ": ");
System.out.print(a[i]);
}
}
break;
上面的第二段代码编译无错误,但在手机上执行后只显示mConnectedDeviceName 和": ",a输出为空白!
第一次接触JAVA编程,还请多多指教!