flutter_bluetooth_serial icon indicating copy to clipboard operation
flutter_bluetooth_serial copied to clipboard

read failed, socket might closed or timeout, read ret: -1

Open HajerWael opened this issue 4 years ago • 15 comments

Tried to use the package to add chat feature ,always getting this error when try to connect to a paired device (android mobile) as below .

read failed, socket might closed or timeout, read ret: -1

What I tried:

  1. Remote device(android smart phone) already paired and isn't busy.
  2. UUID is The same"00001101-0000-1000-8000-00805F9B34FB", tried to generate random one or get the remote device's still not working .
  3. Tried to test it with deferent types of android mobiles(galaxy and oppo) amd still the same .

I'm stucked at this point ,really need to implement this feature asap , soo any help ??

Thanks in advanced,

HajerWael avatar Nov 22 '21 10:11 HajerWael

Hi @HajerWael. Were you able to solve this error?

vaibhiarora03 avatar Feb 04 '22 09:02 vaibhiarora03

Same here, in my case i'm tying to connect to an Intel NUC.

martesabt avatar Feb 09 '22 10:02 martesabt

Hi @martesabt. What's the occurance rate of this error for you? Is it random or you are stuck with it?

vaibhiarora03 avatar Feb 09 '22 15:02 vaibhiarora03

Hey @vaibhiarora03, unfortunately I'm stuck with it.

I also tried to connect the device to the Intel NUC through BT system settings, and it worked. Then when I request BT devices with the package the Intel NUC has the status connected.

martesabt avatar Feb 09 '22 15:02 martesabt

Hi @martesabt .

I did some searching for this issue and had implemented it from those learnings. You can have a look at my implementation. Though the code I tried didn't work for me but worked for many others. Maybe it can help you out. https://stackoverflow.com/questions/71038323/ioexception-read-failed-socket-might-closed-or-timeout-read-ret-1

My source:- https://stackoverflow.com/questions/18657427/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3

vaibhiarora03 avatar Feb 09 '22 15:02 vaibhiarora03

Thanks @vaibhiarora03 I will take a look at it.

I noticed you closed #160 did you found an alternative?

This is frustrating :(

martesabt avatar Feb 09 '22 15:02 martesabt

@martesabt The logs which i mentioned in the post had my custom logs in it making it useless to point to the exact issue. Thats why closed it and opened a new issue with proper information.

#164

vaibhiarora03 avatar Feb 09 '22 15:02 vaibhiarora03

@vaibhiarora03 Oh, ok!

I hope we can solve this!

martesabt avatar Feb 09 '22 15:02 martesabt

hello Everyone I have read failed, socket might be closed or timeout read ret: -1 this issues you all are using static UUID for the connection so that you are facing this issue, You can use your Bluetooth device UUID for the connection and you can resolve this issue.

You can change BluetoothConnection.java class you can replace connect method

/// Connects to given device by hardware address (default UUID used) public void connect(String address) throws IOException { BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = btAdapter.getRemoteDevice(address); ParcelUuid[] uuids = (ParcelUuid[]) device.getUuids(); connect(address, uuids[0].getUuid()); }

RRohitM avatar Jun 17 '22 05:06 RRohitM

hello Everyone I have read failed, socket might be closed or timeout read ret: -1 this issues you all are using static UUID for the connection so that you are facing this issue, You can use your Bluetooth device UUID for the connection and you can resolve this issue.

You can change BluetoothConnection.java class you can replace connect method

/// Connects to given device by hardware address (default UUID used) public void connect(String address) throws IOException { BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothDevice device = btAdapter.getRemoteDevice(address); ParcelUuid[] uuids = (ParcelUuid[]) device.getUuids(); connect(address, uuids[0].getUuid()); }

this saved me, thx man

guigzp avatar Sep 27 '22 20:09 guigzp

@RRohitM I tried this method, but it caused my code to compile to my device. Were there any other changes that you made to the base example codes?

MrAnderson2001 avatar Jan 29 '23 23:01 MrAnderson2001

I tried @RRohitM solution but still same issue

mohsinnaqvi0606 avatar Sep 18 '23 08:09 mohsinnaqvi0606

This should help. https://stackoverflow.com/a/25647197/5552022

So change from

device.createRfcommSocketToServiceRecord(uuid); // or device.createInsecureRfcommSocketToServiceRecord(uuid);

to

socket = (BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device, 1);

ugifractal avatar Jan 14 '24 13:01 ugifractal