sockets - My bluetooth connection unexpectedly closed in Android -


java.io.ioexception: bt socket closed, read return: -1 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ android.bluetooth.bluetoothsocket.read(bluetoothsocket.java:434) 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ android.bluetooth.bluetoothinputstream.read(bluetoothinputstream.java:96) 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ java.io.inputstreamreader.read(inputstreamreader.java:231) 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ java.io.bufferedreader.fillbuf(bufferedreader.java:145) 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ java.io.bufferedreader.readline(bufferedreader.java:397) 08-14 20:30:11.519 30608-1676/com.example.lg.scoreboardapp w/system.err: @ com.example.lg.scoreboardapp.mainactivity$connectthread.run(mainactivity.java:336) 

sometimes in app, client unexpectedly closed. don't know why...... have 3 diveces. 1 devices server. others client. connected well, 1 client closed. other client still open. please give me solution...!! it's mainactivitity.. 336lineenter code here

while(socket != null){     i++;     log.d("mainactivity","-----------------"+i+"-----------------------------");     bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream()));     final string str1 = in.readline(); //<<---line 336     log.d("mainactivity","--------------------------------------"+str1+i);     json1 = str1;     parse(); } 

it's connectthread client

private class connectthread extends thread {     private bluetoothsocket socket;     private final bluetoothdevice mmdevice;      public connectthread(bluetoothdevice device) {         mmdevice = device;         bluetoothsocket tmp = null;          // bluetoothsocket connection         // given bluetoothdevice         try {             tmp = device.createrfcommsockettoservicerecord(my_uuid);         } catch (ioexception e) {             toast.maketext(mainactivity.this, "연결에 실패하였습니다.\n다시 시도하여 주세요", toast.length_short).show();             e.printstacktrace();             //mkmsg("client connection failed: "+e.getmessage().tostring()+"\n");         }         socket = tmp;      }      public void run() {         // mkmsg("client running\n");         // cancel discovery because slow down connection         mbluetoothadapter.canceldiscovery();          // make connection bluetoothsocket         try {             // blocking call , return on             // successful connection or exception             socket.connect();         } catch (ioexception e) {             //mkmsg("connect failed\n");             e.printstacktrace();             try {                 socket.close();                 socket = null;             } catch (ioexception e2) {                 //mkmsg("unable close() socket during connection failure: "+e2.getmessage().tostring()+"\n");                 socket = null;                 e2.printstacktrace();             }             // start service on restart listening mode         }         // if connection accepted         if (socket != null) {             //mkmsg("connection made\n");             //mkmsg("remote device address: "+socket.getremotedevice().getaddress().tostring()+"\n");             //note copied tcpdemo code.             try {                 int i=0;                 while(socket != null){                     i++;                     log.d("mainactivity","-----------------"+i+"-----------------------------");                     bufferedreader in = new bufferedreader(new inputstreamreader(socket.getinputstream()));                     final string str1 = in.readline();                     log.d("mainactivity","--------------------------------------"+str1+i);                     json1 = str1;                     parse();                 }                 log.d("mainactivity_341line","socket null.......... check this");                 handler.post(new runnable() {                     @override                     public void run() {     toast.maketext(getapplicationcontext(),"mainactivity_341line   socket null.......... check this",toast.length_long);                      }                 });               } catch(exception e) {                 //mkmsg("error happened sending/receiving\n");                 e.printstacktrace();                  handler.post(new runnable() {                     @override                     public void run() {                         toast.maketext(getapplicationcontext(),"error happened sending/receiving\\n",toast.length_long);                         log.d("mainactivity_341line","error happened sending/receiving");                     }                 });                  try {                     socket.close();                     socket = null;                 } catch (ioexception e2) {                     //mkmsg("unable close() socket during connection failure: "+e2.getmessage().tostring()+"\n");                     socket = null;                     log.d("mainactivity_341line","error happened "+e2);                     e2.printstacktrace();                 }               }         } else {             //mkmsg("made connection, socket null\n");             handler.post(new runnable() {                 @override                 public void run() {                     toast.maketext(getapplicationcontext(),"made connection, socket null\\n",toast.length_long);                 }             });         }       }      public void cancel() {         try {             socket.close();             toast.maketext(mainactivity.this, "채점기기와의 연결이 끝났습니다", toast.length_short).show();         } catch (ioexception e) {             toast.maketext(mainactivity.this, "채점기기와의 연결이 끝났습니다", toast.length_short).show();             e.printstacktrace();         }     } } 

try replace run() method in connectthread this:

    public void run()     {         log.e(tag, "begin mconnectthread");         setname("connectthread");          madapter.canceldiscovery();          try         {             mmsocket.connect();         }         catch (ioexception e)         {             try             {                 log.e(tag,"trying fallback...");                 mmsocket = (bluetoothsocket)                         mmdevice.getclass()                                 .getmethod("createrfcommsocket", new class[] {int.class}).invoke(mmdevice, 2);                 mmsocket.connect();                 log.e(tag,"connected");             }             catch (exception e2)             {                 log.e(tag, "couldn't establish bluetooth connection!");                 try                 {                     mmsocket.close();                 }                 catch (ioexception e3)                 {                     log.e(tag, "unable close() " + " socket during connection failure", e3);                 }                 connectionfailed();                 return;             }         }          synchronized (bluetoothhelper.this)         {             mconnectthread = null;         }          connected(mmsocket, mmdevice);     } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -