android - Bluetooth disconnecting -


i'm developing app pair smartphone evaluation board via bluetooth. find board in phone , pair them, when happens, conection gets lost. have tried pair them other app , works well, must in code. these steps app follows make conection.

mainactivity:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     /*gets bluetooth adapter*/     globalvar.mbluetoothadapter = bluetoothadapter.getdefaultadapter();     /*resgisters receiver*/     registerreceiver(bluetoothdeviceselectedreceiver, new intentfilter(             "android.bluetooth.devicepicker.action.device_selected"));     /*calls method make conection , pair devices*/     newconection(); }  public void newconection() {     /*our device not support bt*/     if (globalvar.mbluetoothadapter == null) {         toast.maketext(this, r.string.bluetooth_not_available, toast.length_long).show();         finish();         return;     }     /*enables bt if disabled*/     if (!globalvar.mbluetoothadapter.isenabled()) {         intent enablebtintent = new intent(bluetoothadapter.action_request_enable);         startactivityforresult(enablebtintent, request_enable_bt);         return;     }     /*if enabled, starts session*/     else {         globalvar.mconnections = new manageconnection(mainactivity.this, this);     }     /*opens activity select device*/     openselectdevice(); }   /**starts activity select device*/ public void openselectdevice() {     startactivity(new intent("android.bluetooth.devicepicker.action.launch")     .putextra("android.bluetooth.devicepicker.extra.need_auth", false)     .putextra("android.bluetooth.devicepicker.extra.filter_type", 0)     .setflags(intent.flag_activity_exclude_from_recents)); }   @override public void onactivityresult(int requestcode, int resultcode, intent data) {     switch (requestcode) {         case request_enable_bt:             if (resultcode == result_ok) {                 /*starts session*/                 globalvar.mconnections = new manageconnection(this, this);                 openselectdevice();             }             break;     } }   /**receiver make somethign when device selected pair*/ public broadcastreceiver bluetoothdeviceselectedreceiver = new broadcastreceiver() {     @override     public void onreceive(context context, intent intent) {         /*gets device*/         bluetoothdevice device = (bluetoothdevice) intent.getparcelableextra(bluetoothdevice.extra_device);         globalvar.mconnections.cancelclient();         globalvar.mconnections.startclient(device, mainactivity.this);     } }; 

manageconnections:

/**constructor*/ public manageconnection(context context, connectionlistener connectionlistener) {     this.connectionlistener = connectionlistener; }  public synchronized void startclient(bluetoothdevice device, connectionlistener cnlistener) {     /*cancel thread attempting make connection*/     if (mstate == state_connecting) {         if (globalvar.clientconnectionthread != null) {             globalvar.clientconnectionthread.cancel();             globalvar.clientconnectionthread = null;         }     }     /*cancel thread running connection*/     if (globalvar.connectedthread != null) {         globalvar.connectedthread.cancel();         globalvar.connectedthread = null;     }     /*start thread connect given device*/     globalvar.clientconnectionthread = new clientconnectionthread(device, cnlistener);     globalvar.clientconnectionthread.start(); } 

clientconectionthread:

public clientconnectionthread(bluetoothdevice device, connectionlistener connectionlistener) {     this.connectionlistener = connectionlistener;     bluetoothsocket sockettemp = null;      try {         sockettemp = device.createrfcommsockettoservicerecord(globalvar.my_uuid);     } catch (ioexception e) {     }     socket = sockettemp; }  @override public void run() {     try {         socket.connect();         connectionlistener.onconnected(socket);     } catch (ioexception e) {         try {             socket.close();         } catch (ioexception e1) {}     } } 

mannageconections:

public synchronized void startconnected(bluetoothsocket socket, activity activity, connectionlistener cnlistener) {     /*cancel thread completed connection*/     if (globalvar.clientconnectionthread != null) {         globalvar.clientconnectionthread.cancel();         globalvar.clientconnectionthread = null;     }     /*cancel thread running connection*/     if (globalvar.connectedthread != null) {         globalvar.connectedthread.cancel();         globalvar.connectedthread = null;     }     /*cancel accept thread because want connect 1 device*/     if (globalvar.serverconnectionthread != null) {         globalvar.serverconnectionthread.cancel();         globalvar.serverconnectionthread = null;     }     /*start thread manage connection , perform transmissions*/     globalvar.connectedthread = new connectedthread(socket, activity, cnlistener);     globalvar.connectedthread.start(); } 

connectedthread:

public connectedthread(bluetoothsocket socket, activity activity, connectionlistener connectionlistener) {     this.socket = socket;     this.activity = activity;     this.connectionlistener = connectionlistener;      inputstream intemp = null;     outputstream outtemp = null;      try {         intemp = socket.getinputstream();         outtemp = socket.getoutputstream();     } catch (ioexception e) {     }     inputstream = intemp;     outputstream = outtemp; }  @override public void run() {     byte[] buffer = new byte[1024];     int readed;      while (true) {         try {             readed = inputstream.read(buffer);  //in point jumps catch statement             if (readed > 0) {                 final byte[] temp = new byte [readed];                 system.arraycopy(buffer, 0, temp, 0, readed);                  /*shows on ui*/                 activity.runonuithread(new runnable() {                     @override                     public void run() {                         connectionlistener.msgread(temp);                     }                 });             }         } catch (ioexception e) {             connectionlistener.ondisconnected("error when reading: " +e.getmessage());             break;         }     } } 

so, in line above indicated jumps catch statement , on logcat:

11-27 09:49:24.438: w/bluetoothadapter(5297): getbluetoothservice() called no bluetoothmanagercallback 11-27 09:49:24.438: d/bluetoothsocket(5297): connect(), socketstate: init, mpfd: {parcelfiledescriptor: filedescriptor[46]} 11-27 09:49:24.458: i/adreno200-egl(5297): <qegldrvapi_eglinitialize:265>: egl 1.4 qualcomm build: au_linux_android_jb_2.5.5.04.02.02.092.059_msm8960_jb_2.5.5_cl3896081_release_au (cl3896081) 11-27 09:49:24.458: i/adreno200-egl(5297): build date: 06/25/13 tue 11-27 09:49:24.458: i/adreno200-egl(5297): local branch:  11-27 09:49:24.458: i/adreno200-egl(5297): remote branch: quic/jb_2.5.5 11-27 09:49:24.458: i/adreno200-egl(5297): local patches: none 11-27 09:49:24.458: i/adreno200-egl(5297): reconstruct branch: au_linux_android_jb_2.5.5.04.02.02.092.059 +  nothing 11-27 09:49:24.528: d/openglrenderer(5297): enabling debug mode 0 11-27 09:49:24.658: i/timeline(5297): timeline: activity_idle id: android.os.binderproxy@41d0d7c8 time:2279177 11-27 09:49:25.769: e/bluetoothsocket(5297): ioexception while reading inputstream 11-27 09:49:25.779: d/ondisconnected(5297): error when reading: bt socket closed, read return: -1 

it looks trying read() inputstream before connection done.

the documentation of getinputstream() tells it:

[...] operations on stream throw ioexception until associated socket connected

start connectedthread after socket.connect() returns.


Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -