Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HajerWael opened this issue Nov 22, 2021 · 14 comments
Open

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

HajerWael opened this issue Nov 22, 2021 · 14 comments

Comments

@HajerWael
Copy link

HajerWael commented Nov 22, 2021

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
Copy link

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

@martesabt
Copy link

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

@vaibhiarora03
Copy link

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

@martesabt
Copy link

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
Copy link

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
Copy link

Thanks @vaibhiarora03 I will take a look at it.

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

This is frustrating :(

@vaibhiarora03
Copy link

@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
Copy link

@vaibhiarora03 Oh, ok!

I hope we can solve this!

@RRohitM
Copy link

RRohitM commented Jun 17, 2022

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
Copy link

guigzp commented Sep 27, 2022

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
Copy link

@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
Copy link

I tried @RRohitM solution but still same issue

@ugifractal
Copy link

ugifractal commented Jan 14, 2024

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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants