android - Save Bytes from Seral Transfer to String -


i have following code , i'm trying save received bytes string until receive \n.

 byte[] buffer = new byte[1024];                 int bytes;                 string receivedmessage = null;                 while (true) {                      try   {                         // read inputstream                         bytes = mminstream.read(buffer);                          receivedmessage = receivedmessage + getstring(bytes);                         // send obtained bytes ui activity                         if(receivedmessage.endswith("\\n")) {                             string stringtoreturn = receivedmessage.replace("\\n","");                              message msg = mhandler.obtainmessage(abstractactivity.message_read);                             bundle bundle = new bundle();                             bundle.putstring("message", stringtoreturn);                             msg.setdata(bundle);                             mhandler.sendmessage(msg);                              //mhandler.obtainmessage(abstractactivity.message_read, bytes, -1, buffer)                             //        .sendtotarget();                         }                  } catch (ioexception e) {                     e.printstacktrace();                     connectionlost();                     bluetoothservice.this.stop();                     break;                 } 

the problem is crashing on receivedmessage = receivedmessage + getstring(bytes);, more on getstring(bytes)

can me fix it?

thanks!

string tempstring = new string(buffer,0,bytes); receivedmessage = receivedmessage + tempstring; 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -