java - How to call write() function of ConnectedThread class of android bluetooth from main activity -


i want know how call write function of connectedthread class main activity java file

public static class connectedthread extends thread {     public final bluetoothsocket mmsocket;     private final inputstream mminstream;     private final outputstream mmoutstream;      public connectedthread(bluetoothsocket socket) {         mmsocket = socket;         inputstream tmpin = null;         outputstream tmpout = null;          // input , output streams, using temp objects because         // member streams final         try {             tmpin = socket.getinputstream();             tmpout = socket.getoutputstream();         } catch (ioexception e) { }          mminstream = tmpin;         mmoutstream = tmpout;     }      public void run() {         byte[] buffer = new byte[1024];  // buffer store stream         int bytes; // bytes returned read()          // keep listening inputstream until exception occurs         while (true) {             try {                 // read inputstream                 bytes = mminstream.read(buffer);                 // send obtained bytes ui activity                // mhandler.obtainmessage(2, bytes, -1,                                     buffer).sendtotarget();             } catch (ioexception e) {                 break;             }         }     }      // call main activity send data remote device      public void write(string m) {         try {             string msg = m;             mmoutstream.write(msg.getbytes());         } catch (exception e)             {                 e.printstacktrace();             }      }      /* call main activity shutdown connection */     public void cancel() {         try {             mmsocket.close();         } catch (ioexception e) { }     }  } 

the short answer cannot (and shouldn't) call directly activity callback or handler tied main thread. cause main thread of app block since making socket calls. defer background thread. there many options can use this, such handlerthread, intentservice, asynctask or rxjava framework.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -