java - Unable to connect to external Bluetooth barcode scanner -


i trying connect app external bluetooth barcode scanner (yhd-3100) keep getting java.io.ioexception: read failed, socket might closed or timeout, read ret: -1 when try call .connect() bluetoothsocket.

this code works fine when connect wireless printer (able print out no problem) fails barcode scanner. might cause here?

here code:

public class bluetoothconnect implements runnable {      // code eliminated       public bluetoothconnect(context context, int device) {         // code eliminated      }      public string retrievebarcode() {         bluetoothadapter = bluetoothadapter.getdefaultadapter();         if(bluetoothadapter.isenabled()) {             log.d(tag, "bluetooth enabled");             getbluetoothlist(device, true);         } else {             log.d(tag, "bluetooth disabled");             alertdialog(title_bluetooth_off, message_bluetooth_off);         }          return barcode;     }      private void getbarcode() {         log.d(tag, "attempt retrieve barcode");         thread thread = new thread() {             public void run() {                 try {                     inputstream inputstream = bluetoothsocket.getinputstream();                     bufferedreader r = new bufferedreader(new inputstreamreader(inputstream));                     stringbuilder sb = new stringbuilder();                     string line;                     while ((line = r.readline()) != null) {                         sb.append(line).append('\n');                     }                     barcode = sb.tostring();                 } catch (exception e) {                     log.e(tag, "unable retrieve barcode", e);                 }             }         };          thread.start();     }      private void getbluetoothlist(int device, boolean isexecute) {         log.d(tag, "retrieve list of bluetooth");         set<bluetoothdevice> pairedset = bluetoothadapter.getbondeddevices();         int size = pairedset.size();          if(size > 0) {             arraylist<string> bluetootharray = new arraylist<>();             for(bluetoothdevice bluetoothdevice : pairedset) {                 // display in list wireless printer devices                 if(bluetoothdevice != null && bluetoothdevice.getbluetoothclass().getdeviceclass() == device) {                     paireddevices.add(bluetoothdevice);                     bluetootharray.add(bluetoothdevice.getname());                     log.d(tag, "device: " + bluetoothdevice.getname());                 }             }              bluetoothlist = bluetootharray.toarray(new charsequence[bluetootharray.size()]);             alertbluetoothlistdialog();         }      }      private void alertbluetoothlistdialog() {         log.d(tag, "open dialog bluetooth list");         alertdialog.builder builder = new alertdialog.builder(context);         builder.seticon(r.drawable.ic_attach_money_black_24dp);         if(device == type_printer) {             builder.settitle(title_bluetooth_printer);         } else if(device == type_barcode_scanner) {             builder.settitle(title_bluetooth_scanner);         }         builder.setitems(bluetoothlist, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 bluetoothdevice = paireddevices.get(which);                 setbluetoothitems();                 dialog.dismiss();             }         });          builder.show();     }      private void setbluetoothitems() {         bluetoothadapter.canceldiscovery();         devicemacaddress = bluetoothdevice.getaddress();          // retrieve device's first uuid         parceluuid[] list = bluetoothdevice.getuuids();         if(list[0] != null) {             deviceuuid = list[0].tostring();         }          connectapptodevice();     }      private void connectapptodevice() {         log.d(tag, "attempt connect app device");         bluetoothdevice = bluetoothadapter.getremotedevice(devicemacaddress);         bluetoothconnectprogressdialog = progressdialog.show(context,                 "connecting...", bluetoothdevice.getname(), true, false);         thread bluetoothconnectthread = new thread(this);         bluetoothconnectthread.start();     }      private void alertdialog(string title, string message) {         alertdialog.builder builder = new alertdialog.builder(context);         builder.seticon(r.drawable.ic_warning_24dp);         builder.settitle(title);         builder.setmessage(message);          builder.setnegativebutton(message_ok, new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int which) {                 dialog.cancel();             }         });          builder.show();     }      public void run() {         try {             uuid appuuid = uuid.fromstring(deviceuuid);             bluetoothsocket = bluetoothdevice.createrfcommsockettoservicerecord(appuuid);             bluetoothadapter.canceldiscovery();             bluetoothsocket.connect();             handler.sendemptymessage(0);         } catch (ioexception econnectexception) {             closesocket(bluetoothsocket, true);         }     }      private void closesocket(bluetoothsocket opensocket, boolean iserror) {         try {             opensocket.close();             bluetoothconnectprogressdialog.dismiss();         } catch (ioexception ex) {             log.d(tag, "could not close socket");         }     }      private handler handler = new handler() {         @override         public void handlemessage(message msg) {             bluetoothconnectprogressdialog.dismiss();             getbarcode();             closesocket(bluetoothsocket, false);         }     }; } 


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -