Java 在Android Bluetooth中连接套接字时获取IOException

Java 在Android Bluetooth中连接套接字时获取IOException,java,android,bluetooth,android-bluetooth,Java,Android,Bluetooth,Android Bluetooth,我正在为蓝牙连接功能创建一个类,在设备配对后连接到该设备之前,每件事情似乎都可以正常工作。我尝试调用socket.connect时遇到IOException 调用时出错 mSocket.connect(); 这是我得到的stacktrace 我已经看过这些问题了 但还是没能解决我的问题。你可以查看我的全部代码,我可能遗漏了什么 顺便说一下,这是stacktrace 01-11 18:38:37.399 14502-14502/adc.com.samplebluetooth W/Bluet

我正在为蓝牙连接功能创建一个类,在设备配对后连接到该设备之前,每件事情似乎都可以正常工作。我尝试调用socket.connect时遇到IOException

调用时出错

mSocket.connect();
这是我得到的stacktrace

我已经看过这些问题了

但还是没能解决我的问题。你可以查看我的全部代码,我可能遗漏了什么

顺便说一下,这是stacktrace

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)

我已经检查了你的粘贴箱(顺便说一句,代码比我的好!)。我在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!");
        }
    }
}
connect()
函数将尝试读取套接字的输入流。为此,它将尝试在您提供的设备中找到具有相同UUID的侦听套接字

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

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


请看帖子:

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