Android蓝牙:java.io.IOException:读取失败,套接字可能关闭或超时,读取ret:-1

时间:2018-08-07 20:27:16

标签: java android bluetooth

我在标题中出现了以下代码错误:

protected void onHandleIntent(Intent intent){
        startForeground(1337,buildForegroundNotification());
        String deviceAddress = intent.getStringExtra("address");
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice bd = ba.getRemoteDevice(deviceAddress);
        Log.w("BTService",bd.getName());
        try {
            BluetoothSocket socket = bd.createRfcommSocketToServiceRecord(bd.getUuids()[0].getUuid());
            socket.connect();
        } catch (Exception e) {
            Log.e("BTService", "failed to connect to joycon", e);
            stopSelf();
        }
        /*other stuff*/
        return;
    }

我正在尝试从前台IntentService连接到蓝牙设备(Nintendo Switch Joy-Con),并且错误发生在行socket.connect()上。我已经尝试了IOException: read failed, socket might closed - Bluetooth on Android 4.3的大部分建议,但它们根本没有做任何事情。我正在测试的手机是配备Android oreo(8.0.0)的OnePlus 3t。在运行android 6.0.1的Samsung Galaxy S5上发生相同的错误。该错误的完整堆栈跟踪为:

java.io.IOException: read failed, socket might closed or timeout, read ret: -1
        at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:686)
        at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:645)
        at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:379)
        at com.example.vchid.myapplication.BTService.onHandleIntent(BTService.java:49)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:68)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.os.HandlerThread.run(HandlerThread.java:65)

我在设置连接时做错了什么,还是有其他解决方法?感谢您的帮助。

0 个答案:

没有答案