在Android蓝牙中连接套接字时获取IOException

时间:2017-01-11 07:58:57

标签: java android bluetooth android-bluetooth

我正在为蓝牙连接功能创建一个类似乎每件事情都能正常工作,直到我到达配对后连接设备的位置。当我尝试调用socket.connect时,我收到了IOException。

调用

时发生错误
mSocket.connect();

这是我得到的堆栈跟踪。

我看过这些stackoverflow问题

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

但仍未能解决我的问题。您可以查看我的整个代码HERE我可能错过了一些内容。

顺便说一句,这是堆栈跟踪。

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)

2 个答案:

答案 0 :(得分:0)

我已经检查了你的粘贴箱(比我的btw更好的代码!)。我有socket.connect()的同样问题,我设法用下面的代码解决它。祝你好运。

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!");
        }
    }
}

答案 1 :(得分:0)

connect()函数将尝试读取套接字的inputStream。为此,它会尝试在您提供的设备中找到具有相同UUID 的侦听套接字

IOException函数中的connect()只是说你的套接字在另一个设备中找不到具有相同UUID的侦听套接字。

UUID非常重要,因为它是您设备中套接字的唯一标识符(如port to tcp)。

请查看该帖子:Android bluetooth IOException: read failed, socket might closed or timeout, read ret: -1