将数据写入Android蓝牙输出流抛出:IllegalMonitorStateException

时间:2011-11-12 18:57:54

标签: android multithreading bluetooth

我有一个Android蓝牙应用程序,可管理几个远程设备(胶囊)。

将数据写入Capsule的套接字输出流昨天工作,并且在中等规模重构到Android应用程序 后,我收到以下错误:

java.lang.IllegalMonitorStateException:尝试解锁读锁定,而不是被当前线程锁定。

这是套接字创建代码:

 public final void connectWithCapsule(Capsule capsule)
        throws Exception {
    BluetoothSocket socket = capsulesSockets.get(capsule);

    if (socket == null) {
        try {
            // Method m = capsule.getBT_Device().getClass().getMethod("createRfcommSocket", new Class[]{int.class});
            // socket = (BluetoothSocket) m.invoke(capsule.getBT_Device(), Integer.valueOf(17));
            socket = capsule.getBT_Device().createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        } catch (Exception e) {
            logError("Error creating RFcomm socket", e);
            throw e;
        }
        capsulesSockets.put(capsule, socket);
    }

    try {
        socket.connect();
    } catch (IOException e) {
        logError("Error connecting socket", e);
        try {
            socket.close();
        } catch (IOException e1) {
            logError("Error closing socket", e1);
        }
        capsulesSockets.remove(capsule);
        throw e;
    }
}

以及管理输入/输出流的模型:

public final class KitBT_ConnectionModel {

private final OutputStream[] outputStreams;

private final InputStream[] inputStreams;

public KitBT_ConnectionModel(OutputStream[] outputStreams, InputStream[] inputStreams) {
    super();
    this.outputStreams = outputStreams;
    this.inputStreams = inputStreams;
}

public void transmitData(byte[] bs)
        throws IOException {
    for (OutputStream outputStream : outputStreams) {
        outputStream.write(bs); // THIS LINE THROWS THE EXCEPTION
        outputStream.flush();
    }
}

public InputStream[] getInputStreams() {
    return inputStreams;
}
}

注意:我不会对这两个流执行任何操作,并且第一次写入会导致异常。

首先想到的是哪个线程放入读锁定以及什么时候?

我尝试使用调用套接字创建的线程和流事务,我已经确定,100%确定他们都有已被相同的线程访问(并尝试使用不同的线程访问),但此异常仍然存在。

请赐教......

1 个答案:

答案 0 :(得分:1)

HAAAAAAAAAAAAAAAAAAa .........

认识这款LG手机!!!!

我给手机重新启动,取出电池并重新启动它,然后再次工作......

关闭和打开蓝牙并没有成功!过去一天左右我一直这样做。

上帝该死了将近24小时的废物......

这些产品如何搞砸了!

至少它现在有效!

相关问题