我编写了一个程序,它不断通过SPP从蓝牙读取数据,并将数据流中的内容打印到编辑文本框中。我有以下线程:
myTimer = new Timer();
myTimer.schedule(new TimerTask(){
@Override
public void run(){
TimerMethod();
}
},0,1000);
private void TimerMethod(){this.runOnUiThread(startReading);}
private Runnable startReading = new Runnable(){
public void run(){
EditText _txtArea = (EditText) findViewById(R.id._txtArea);
try{
inStream = btSocket.getInputStream();
}catch (IOException e3) {
_txtArea.append("inStream establishment Failed!");
}现在,消息的传入可以是任何大小,我想继续阅读,直到没有剩下要读的东西。我尝试了一个实现,我做了这样的事情:
byte[] msgIn = new byte[15];
inStream.read(msgIn, 0, 15);
int len = msgIn.length;
for (int i=0; i
out = new Character ((char) msgIn[i]).toString();
_txtArea.append(out);但是这限制了读取到15个字节,并且代码看起来不太有效。如果有人想知道为什么我有以下行=新字符((char)msgIn [i])。toString();这是因为进入的数据是ASCII,我将它转换为字符。在没有别的东西要读取程序时,在读完所有内容后也使用这种方法。有没有人知道一种方式,我可以继续阅读,直到所有的数据被读取?