android - How to clear the bluetooth InputStream buffer -


in bluetoothchat example app, sent , received data added arrayadapter called mconversationarrayadapter. there, each character added array.

in case, have string instead of array because don't need send , receive several data, need send 1 string, , receive 1 string each time.

the problem i'm getting if first receive string hello world, , receive shorter one, first overwrited second, instead of deleting first , writing new.

so, if first receive hello world, , supposse have receive bye, receive byelo world.

so, how can clear buffer each time receive want?

code snipets

send data:

    byte[] send1 = message_full1.getbytes();     globalvar.mtransmission.write(send1); 

write call:

public void write(byte[] out) {     /**create temporary object*/     connectedthread r;     /**synchronize copy of connectedthread*/     synchronized (this) {         if (globalvar.mstate != globalvar.state_connected) return;         r = globalvar.mconnectedthread;     }     /**perform write unsynchronized*/     r.write(out); } 

write thread:

    public void write(byte[] buffer) {     try {         globalvar.mmoutstream.write(buffer);          /**share sent message ui activity*/         globalvar.mhandler.obtainmessage(globalvar.message_write, -1, -1, buffer).sendtotarget();      } catch (ioexception e) {} } 

finally, read thread:

    public void run() {     byte[] buffer = new byte[12];  // buffer store stream     int bytes; // bytes returned read()      /**keep listening inputstream until exception occurs*/     while (true) {         try {             /**read inputstream*/             bytes = globalvar.mminstream.read(buffer);              /**send obtained bytes ui activity*/             globalvar.mhandler.obtainmessage(globalvar.message_read, bytes, -1, buffer).sendtotarget();         } catch (ioexception e) {             globalvar.mtransmission.connectionlost();             /**start service on restart listening mode*/             //globalvar.mtransmission.start();             break;         }     } } 

try this

bytes = inputstream.read(buffer); buffer[bytes] = '\0'; 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -