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

时间:2017-05-16 07:46:30

标签: java android sockets bluetooth android-bluetooth

我尝试通过蓝牙连接条形码扫描仪和我的应用程序。但是,bluetoothSocket.connect()会返回此错误 -

java.io.IOException: read failed, socket might closed or timeout, read ret: -1.

我搜索过solution并关注它,但它不能用于我的应用。请帮忙!这是我的代码:

public class BluetoothConnect {

private static final String TAG = BluetoothBarcode.class.getSimpleName();
private ConnectBluetooth bluetoothSocket;
private BluetoothDevice device;
private boolean secure;
private BluetoothAdapter adapter;
private List<UUID> uuidCandidates;
private int candidate;

public BluetoothConnect(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
                          List<UUID> uuidCandidates) {
    this.device = device;
    this.secure = secure;
    this.adapter = adapter;
    this.uuidCandidates = uuidCandidates;
    if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
        this.uuidCandidates = new ArrayList<UUID>();
        this.uuidCandidates.add(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
    }
}

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

        try {
            bluetoothSocket.connect();
            success = true;
            break;
        } catch (IOException e) {
            try {
                bluetoothSocket = new ConnectBluetoothFallback(bluetoothSocket.getUnderlyingSocket());
                Thread.sleep(500);
                bluetoothSocket.connect();
                success = true;
                break;
            } catch (InterruptedException e1) {
                Log.e(TAG, e1.getMessage(), e1);
            } catch (IOException e1) {
                Log.e(TAG, "Fallback failed. Cancelling.", e1);
            }
        }
    }

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

    return bluetoothSocket;
}

private boolean selectSocket() throws IOException {
    if (candidate >= uuidCandidates.size()) {
        return false;
    }

    BluetoothSocket tmp;
    UUID uuid = uuidCandidates.get(candidate++);
    Log.d(TAG, "Attempting to connect to Protocol: "+ uuid);
    if (secure) {
        tmp = device.createRfcommSocketToServiceRecord(uuid);
    } else {
        tmp = device.createInsecureRfcommSocketToServiceRecord(uuid);
    }
    bluetoothSocket = new ConnectBluetoothFacade(tmp);

    return true;
}
}

...

public class ConnectBluetoothFacade implements ConnectBluetooth {

private BluetoothSocket socket;

public ConnectBluetoothFacade(BluetoothSocket tmp) {
    this.socket = tmp;
}

@Override
public InputStream getInputStream() throws IOException {
    return socket.getInputStream();
}

@Override
public OutputStream getOutputStream() throws IOException {
    return socket.getOutputStream();
}

@Override
public String getRemoteDeviceName() {
    return socket.getRemoteDevice().getName();
}

@Override
public void connect() throws IOException {
    socket.connect();
}

@Override
public String getRemoteDeviceAddress() {
    return socket.getRemoteDevice().getAddress();
}

@Override
public void close() throws IOException {
    socket.close();
}

@Override
public BluetoothSocket getUnderlyingSocket() {
    return socket;
}

}

...

public class ConnectBluetoothFallback extends ConnectBluetoothFacade {

private static final String TAG = BluetoothBarcode.class.getSimpleName();
private static final String KEY = "createRfcommSocket";
private BluetoothSocket fallbackSocket;

public ConnectBluetoothFallback(BluetoothSocket socket) {
    super(socket);
    try {
        Class<?> socketClass = socket.getRemoteDevice().getClass();
        Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
        Method method = socketClass.getMethod(KEY, paramTypes);
        Object[] params = new Object[] {Integer.valueOf(1)};
            fallbackSocket = (BluetoothSocket) method.invoke(socket.getRemoteDevice(), params);

    }
    catch (Exception e) {
        Log.e(TAG, "Error");
    }
}

@Override
public InputStream getInputStream() throws IOException {
    return fallbackSocket.getInputStream();
}

@Override
public OutputStream getOutputStream() throws IOException {
    return fallbackSocket.getOutputStream();
}


@Override
public void connect() throws IOException {
    fallbackSocket.connect();
}


@Override
public void close() throws IOException {
    fallbackSocket.close();
}
}

0 个答案:

没有答案