Buetooth connection failed: read failed, socket might closed or timeout, read ret: -1
Try to use ,
createInsecureRfcommSocketToServiceRecord(MY_UUID)
in place of
createRfcommSocketToServiceRecord(MY_UUID)
This should fix the problem. Share your debugging results , if this doesn't fix the issue.
Also , don't generate random UUID(s), try the one below.
UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
How to fix java.io.IOException: read failed, socket might closed or timeout when trying to connect to a paired device?
From the CONNECTION_UUID
that you provided in your code, I assume that you are connecting with a Bluetooth serial board. I am not sure about the problem yet, however, I thought of writing this answer to provide a probable solution that might solve your issue.
I think in case of the paired devices, you need to initiate the connection with a secure channel. Currently, you are using an insecure channel.
From the documentation...
The communication channel will not have an authenticated link key i.e
it will be subject to man-in-the-middle attacks. For Bluetooth 2.1
devices, the link key will be encrypted, as encryption is mandatory.
For legacy devices (pre Bluetooth 2.1 devices) the link key will be
not be encrypted. Use createRfcommSocketToServiceRecord(UUID) if an
encrypted and authenticated communication channel is desired.
Hence you might consider using createRfcommSocketToServiceRecord()
for your case.
Instead of this
BluetoothSocket mBluetoothSocket = mBluetoothDevice.createInsecureRfcommSocketToServiceRecord(CONNECTION_UUID);
Use this...
BluetoothSocket mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(CONNECTION_UUID);
I hope that solves your problem.
From the comment below - The UUID that actually worked here is 00001101-0000-1000-8000-00805f9b34fb
Bluetooth connect failed. java.io.IOException: read failed, socket might closed or timeout, read ret: -1
In my situation,the bluetooth settings that comes with the system works.
So, I look the source code of the bluetooth settings.
And Copy the libframeworks/base/packages/SettingsLib/src/com/android/settingslib/bluetooth
into my project.
Use reflect to invoke the hide Class and function or variable.
Finally, it works.
I wish this could help.
Android bluetooth IOException: read failed, socket might closed or timeout, read ret: -1
Finally, the root cause was because the socket's inputStream couldn't be read. That's because there was no open socket with the same UUID on the server side.
See https://android.googlesource.com/platform/frameworks/base/+/android-8.0.0_r30/core/java/android/bluetooth/BluetoothSocket.java
So to fix the problem we need a listening socket with the same UUID on the other side, for me it was a raspberry in python:
from bluetooth import *
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "YOUR UUID"
advertise_service( server_sock, "SampleServer", service_id = uuid, service_classes = [ uuid, SERIAL_PORT_CLASS ], profiles = [ SERIAL_PORT_PROFILE ])
print("Waiting for connection on RFCOMM channel %d" % port)
client_sock, client_info = server_sock.accept()
print("Accepted connection from ", client_info)
try:
while True:
data = client_sock.recv(1024)
if len(data) == 0: break
print("received [%s]" % data)
except IOError:
pass
print("disconnected")
client_sock.close()
server_sock.close()
print("all done")
from https://github.com/karulis/pybluez/blob/master/examples/simple/rfcomm-server.py
Related Topics
Can't Run Junit 4 Test Case in Eclipse Android Project
How to Include the Spongy Castle Jar in Android
Wrong Version of Keystore on Android Call
Difference Between Google() and Maven { Url 'Https://Maven.Google.Com' }
How to Persist Permission in Android API 19 (Kitkat)
Runtime.Exec():Reboot in Android
Open Gallery App from Android Intent
How to Get String Response from Retrofit2
Android: How to Use Download Manager Class
Transformexception Duplicate Entry for Common.Annotations.Beta
Polygon Touch Detection Google Map API V2
Android Resource Not Found Exception
How to Resume Android Activity Programmatically from Background
Unhandled Exception Type Error
:App:Dexdebug Execexception Finished with Non-Zero Exit Value 2