title: Writing data to Android Bluetooth output stream throws: IllegalMonitorStateException
body: <p>I have an Android Bluetooth application which manages a couple of remote devices(<strong>Capsules</strong>).</p>
  
  <p><strong>Writing</strong> data to the socket output stream of a Capsule worked yesterday, and after medium scale refactoring to the Android application <strong>only</strong>, I get the following error:</p>
  
  <p>java.lang.IllegalMonitorStateException: attempt to unlock read lock, not locked by current thread.</p>
  
  <p><strong>Here is the socket creation code:</strong></p>
  
  <pre><code> 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;
      }
  }
  </code></pre>
  
  <p><strong>and the model which manages the in/out streams:</strong></p>
  
  <pre><code>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;
  }
  }
  </code></pre>
  
  <p><strong>Note:</strong> I do not perform any action with both of the streams, and the first write causes the exception.</p>
  
  <p>First thing that pops to mind is which thread puts the read lock and when? </p>
  
  <p>I've tried to play around with the threads which call the socket creation, and the streams transactions, I've made <strong>sure</strong>, 100% <strong>sure</strong> they have both been accessed by the <strong>same</strong> thread (and also tried accessing with different threads), but this exception persists. </p>
  
  <p>Please enlighten me...</p>