背景:我现在在做手机端与单片机通过蓝牙进行数据传递。
问题:在我通过蓝牙接收单片机通过串口发来的数据的时候,出现了:
1:大量为0的无用数据;
2:缺失部分数据;
接收代码如下。
while(true){
try {
num = inputStream.read(buffer_z);
n=0;
for(i=0;i<num;i++){
buffer_z_new[n] = buffer_z[i];
n++;
}
String s =bytesToHexString(buffer_z_new);
smsg+=s.trim();
if(inputStream.available()==0)break;
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuffer stringBuffer = new StringBuffer();
String a = smsg.replace("FB0000",",");
String[] b = a.split(",");
String c = b[1].replace("1000",",");
String[] d = c.split(",");
stringBuffer.append(d[1]);
String ends = stringBuffer.toString();
Message msg = new Message();
Bundle data = new Bundle();
data.putByteArray("bytes",ends.getBytes());
msg.what = MainActivity.ACCEPT_MESSAGE;
msg.setData(data);
handler.sendMessage(msg);
我该如何解决呢?