0
我有一個Android藍牙應用程序,它管理幾個遠程設備(膠囊)。將數據寫入Android藍牙輸出流拋出:IllegalMonitorStateException
寫數據到膠囊的插座輸出流昨天工作,並經過中等規模重構的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;
}
}
注:我不執行任何操作與兩個流,並且第一次寫入導致異常。
想到的第一件事是哪個線程放置讀鎖,何時?
我試圖玩弄它調用Socket創建線程,流交易,我做了肯定,100%肯定它們都被由相同線程訪問(並嘗試使用不同的線程訪問),但是這種異常仍然存在。
請賜教...
嘿!我也遇到類似LG電話的問題,在哪種模式下你有這個問題? – YasuDevil
該設備是:LG-P925 – TacB0sS
此解決方案是否解決了您的問題? – TacB0sS