0

I'm creating a class for a Bluetooth connectivity feature every thing seems to work fine until I reached where I am connecting the devices after they have paired. I'm getting an IOException when I try to call socket.connect.

the error occurs when calling

mSocket.connect();

Here is the stacktrace I am getting.

I've looked at these stackoverflow questions

Bluetooth Connection failed "java.io.IOException: read failed, socket might closed or timeout, read ret: -1"

java.io.IOException: read failed, socket might closed or timeout, read ret: -1 on Android 5.0.1 Lollipop version

Getting java.io.IOException: read failed, socket might closed or timeout, read ret: -1 while printing via bluetooth printer

But still failed to fix my issue. You can check out my entire code HERE I might have missed something.

Here is the stacktrace by the way.

01-11 18:38:37.399 14502-14502/adc.com.samplebluetooth W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
01-11 18:38:37.409 14502-14502/adc.com.samplebluetooth D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[60]}
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:     at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516)
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at adc.com.samplebluetooth.Bluetooth.connectToPaired(Bluetooth.java:248)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at adc.com.samplebluetooth.MainActivity$OnClick.onClick(MainActivity.java:90)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.view.View.performClick(View.java:4443)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.view.View$PerformClick.run(View.java:18443)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.os.Handler.handleCallback(Handler.java:733)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.os.Looper.loop(Looper.java:136)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:     at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
philip
  • 1,292
  • 3
  • 24
  • 44

2 Answers2

0

I have checked your paste bin (nicer code than mine btw!). I had the same problem with socket.connect(), I managed to solve it with the following code. Good luck.

public void connect_bt(String deviceAddress, String deviceName) {

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    device = btAdapter.getRemoteDevice(deviceAddress);
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    try {
        socket = device.createRfcommSocketToServiceRecord(uuid);
    } catch (Exception e) {
        Log.d(TAG,"Error creating socket");
    }

    try {
        socket.connect();

        Log.d(TAG,"1st Attempt, Connected");

        connect_obd2();

    } catch (IOException e) {
        Log.d(TAG, "1st Attempt: failed: " + e.getMessage());

        try {
            Log.d(TAG,"2nd Attempt: trying fallback...");

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

            Log.d(TAG,"2nd Attempt: Connected");

            connect_obd2();
        }
        catch (Exception e) {
            Log.d(TAG, "2nd Attempt: Couldn't establish Bluetooth connection!");
        }
    }
}
Mr L
  • 470
  • 6
  • 16
0

The connect() function will try to read the inputStream of the socket. To do that, it will try to find a listening socket with the same UUID in the device you provided.

The IOException in the connect() function just says that your socket didn't find a listening socket with the same UUID in the other device.

The UUID is important cause it's the Unique IDentifier of your socket (like ports to tcp) in your device.

Please see that post: Android bluetooth IOException: read failed, socket might closed or timeout, read ret: -1

tbraun89
  • 2,246
  • 3
  • 25
  • 44
Liam
  • 469
  • 1
  • 6
  • 16