Recent projects connected to Bluetooth after receiving the command function issued by Bluetooth devices, after connecting the device, create Rfcommsocket connection when reported Java.io.IOException:read failed, socket might closed or timeout, READ ret:-1 error. Here is my solution, I hope you have a little help.
Private Bluetoothsocket Msocket;<span style= "White-space:pre" ></span>private InputStream mInputSream; <span style= "White-space:pre" ></span>private UUID muuid = Uuid<span style= "White-space:pre" ></ Span>.fromstring ("00001101-0000-1000-8000-00805F9B34FB");
Locate the Bluetooth device and infer if Bluetooth is connected and create the socket
Bluetoothadapter adapter = Bluetoothadapter.getdefaultadapter (); set<bluetoothdevice> sets = Adapter.getbondeddevices ();iterator<bluetoothdevice> Iterator = Sets.iterator (); while (Iterator.hasnext ()) {Bluetoothdevice device = Iterator.next (); if (mutils.isconnected (device)) try {mbluetoothdevice = Device;msocket = Mbluetoothdevice.createrfcommsockettoservicerecord (MUUID);
The next step is the socket connection, which I did in a sub-thread:
public void Run () {try {if (msocket! = null) msocket.connect (); if (msocket! = null) {Minputsream = Msocket.getinputstream () ; misrunning = true;} while (misrunning) {byte[] buffer = new Byte[16];while (minputsream! = null&& minputsream.read (buffer) > 0 & ;& misrunning) {string val = new string (buffer); LOG.I ("SPP", Val), Intent Intent = new Intent (), if (Val.contains ("+ptt=p")) {intent.setaction (" Android.intent.action.PTT.down ");} else if (Val.contains ("+ptt=r")) {intent.setaction ("Android.intent.action.PTT.up");} Mcontext.sendbroadcast (Intent); Arrays.fill (buffer, (byte) 0);}}} catch (IOException e) {try {if (Minputsream! = null) minputsream.close (); if (msocket! = null) {msocket.close (); msocket = Nu ll;}} catch (Exception E2) {//Todo:handle Exception}}}
However, when the socket connection is still reported Java.io.IOException:read failed, socket might closed or timeout, read ret:-1 Error,
All the information was checked and no solution was found. <span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > After many experiments of its own hair now </span>
Msocket.connect () also needs to be processed in a sub-thread to receive instructions on the normal connection. That is, for example, the following code:
<pre name= "code" class= "java" >public void Run () {New Thread (new Runnable () {@Overridepublic void run () {try {if (mSo Cket = null) msocket.connect (); if (msocket! = null) {Minputsream = Msocket.getinputstream (); misrunning = true;} while (misrunning) {byte[] buffer = new Byte[16];while (minputsream! = null&& minputsream.read (buffer) > 0 & ;& misrunning) {string val = new string (buffer); LOG.I ("SPP", Val), Intent Intent = new Intent (), if (Val.contains ("+ptt=p")) {intent.setaction (" Android.intent.action.PTT.down ");} else if (Val.contains ("+ptt=r")) {intent.setaction ("Android.intent.action.PTT.up");} Mcontext.sendbroadcast (Intent); Arrays.fill (buffer, (byte) 0);}}} catch (IOException e) {try {if (Minputsream! = null) minputsream.close (); if (msocket! = null) {msocket.close (); msocket = Nu ll;}} catch (Exception E2) {//Todo:handle Exception}}}). Start ();
Here is only a solution, but not yet know the reason, also looked up all kinds of information, did not get why in the sub-thread, connect the time also need to open a sub-thread.
Java.io.IOException:read failed, socket might closed or timeout, read ret:-1