I have written a program that constantly reads from the Bluetooth via SPP and prints the contents in the stream to a edittext box. I have the following thread:
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!");
}
Now the msg’s incoming can be of any size and I want to keep reading until there isn’t anything remaining to be read. I tried an implementation where i did something like this:
byte[] msgIn = new byte[15];
inStream.read(msgIn, 0, 15);
int len = msgIn.length;
for (int i=0; i<len; i++){
out = new Character ((char) msgIn[i]).toString();
_txtArea.append(out);
But that limits the read to 15 bytes and the code doesn’t seem very effecient. If anyone is wondering why i have the following line out = new Character ((char) msgIn[i]).toString(); it’s because the data coming in is in ASCII i am converting it to a char. Also using this method after reading all of the contents when there is nothing else to read the program hangs. Does anyone know a way i can keep reading until all of the data has been read?
I figured it out, for those who are interested it is because the stream should be closed before a read so that inputStream.read() will be able to reach -1 after all data has been sent