Java.io.IOException:read failed, socket might closed or timeout, read ret:-1

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.