socket只能在android 6.0上关闭android错误

时间:2017-04-06 21:50:05

标签: android sockets bluetooth

我正在使用我在网上发现的一个名为multibluetooth的蓝牙库来连接一堆客户端。我正在测试2个Android手机,1个运行5.0,另一个运行6.0。在6.0手机上运行服务器,在5.0手机上运行客户端,但是当我反转角色时,我收到此错误:

04-07 00:27:03.507 7499-8393/com.ramimartin.sample.multibluetooth W/BT: Fallback failed. Cancelling.
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:900)
  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:859)
  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:535)
  at com.ramimartin.multibluetooth.bluetooth.client.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:203)
  at com.ramimartin.multibluetooth.bluetooth.client.BluetoothConnector.connect(BluetoothConnector.java:59)
  at com.ramimartin.multibluetooth.bluetooth.client.BluetoothClient.run(BluetoothClient.java:54)
  at java.lang.Thread.run(Thread.java:818)

以下是我收到错误的部分:

public BluetoothSocketWrapper connect() throws IOException {
    boolean success = false;
    while (selectSocket()) {
        adapter.cancelDiscovery();

        try {
            bluetoothSocket.connect();
            success = true;
            break;
        } catch (IOException e) {
            //try the fallback
            try {
                bluetoothSocket = new FallbackBluetoothSocket(bluetoothSocket.getUnderlyingSocket());
                Thread.sleep(500);
                bluetoothSocket.connect();
                success = true;
                break;
            } catch (FallbackException e1) {
                Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
            } catch (InterruptedException e1) {
                Log.w("BT", e1.getMessage(), e1);
            } catch (IOException e1) {
                Log.w("BT", "Fallback failed. Cancelling.", e1);
            }
        }
    }


    if (!success) {
        throw new IOException("===> Could not connect to device: " + device.getAddress());
    }

    return bluetoothSocket;
}

提前谢谢。

1 个答案:

答案 0 :(得分:0)

如果在您让设备处于睡眠状态时发生断开连接,这可能与打盹模式有关。

您可以在https://developer.android.com/training/monitoring-device-state/doze-standby.html找到更多信息。

也可以随时向我询问更多详情。