edufolly / flutter_bluetooth_serial

A basic Flutter Bluetooth Serial
MIT License
478 stars 460 forks source link

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

Open HajerWael opened 2 years ago

HajerWael commented 2 years ago

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,

vaibhiarora03 commented 2 years ago

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

martesabt commented 2 years ago

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

vaibhiarora03 commented 2 years ago

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

martesabt commented 2 years ago

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.

vaibhiarora03 commented 2 years ago

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

martesabt commented 2 years ago

Thanks @vaibhiarora03 I will take a look at it.

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

This is frustrating :(

vaibhiarora03 commented 2 years ago

@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

martesabt commented 2 years ago

@vaibhiarora03 Oh, ok!

I hope we can solve this!

RRohitM commented 2 years ago

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()); }

guigzp commented 2 years ago

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

MrAnderson2001 commented 1 year ago

@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?

mohsinnaqvi0606 commented 1 year ago

I tried @RRohitM solution but still same issue

ugifractal commented 9 months ago

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);
leverkusen188 commented 1 month ago

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);

Saved my life man!