【问题标题】:Getting IOException when connecting socket in Android Bluetooth在Android蓝牙中连接套接字时出现IOException
【发布时间】:2017-05-25 22:15:14
【问题描述】:

我正在为蓝牙连接功能创建一个类,一切似乎都可以正常工作,直到我在设备配对后到达连接设备的位置。当我尝试调用 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)

【问题讨论】:

  • 你能解决这个问题吗,@philip?

标签: java android bluetooth android-bluetooth


【解决方案1】:

我已经检查了您的粘贴箱(顺便说一句,代码比我的更好!)。我对 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!");
        }
    }
}

【讨论】:

    【解决方案2】:

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

    connect() 函数中的 IOException 只是表示您的套接字在其他设备中没有找到具有相同 UUID 的侦听套接字。

    UUID 很重要,因为它是设备中套接字的唯一标识符(例如连接到 tcp 的端口)。

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

    【讨论】:

      猜你喜欢
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2012-12-12
      • 2021-06-29
      相关资源
      最近更新 更多