Listing 1

public void messageReceived(String s) {
  if (!isBluetoothLink) {
    return; //Ignore input from the box, it's very dull!
              }
  System.out.println("Message :" + s);
  int switch1, switch2;
  String upperCaseVersion = s.toUpperCase();
  if (upperCaseVersion.startsWith("BEDROOM")) {
    switch1 = 2;
    switch2 = 4;
  } else if (upperCaseVersion.startsWith("BATHROOM")) {
    switch1 = 5;
    switch2 = 6;
  } else {
    switch1 = 1;
    switch2 = 7;
  }
  if (upperCaseVersion.indexOf("ON") > 0) {
    outputConnection.switchOnDevice(switch1, switch2);
  } else {
    outputConnection.switchOffDevice(switch1, switch2);
  }
}

Listing 2

public void messageReceived(String s) {
  nextMessage = s;
  return;
}


public String getMessage() {
  String tempMessage;
  while (nextMessage.equals("") || (nextMessage.charAt(1) == '#')) {
    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
      System.out.println("Insomnia");
    }
    tempMessage = nextMessage;
    nextMessage = "";
    return tempMessage;
  }
}

Listing 3

public void run() {
  while (notDead) {
    if (!tryingToChangeSpeakers) {
      isDoorbellPolling = true;
      actualSendMessage("I1\r\n");
      if (getMessage().endsWith("0")) {
        doorbellPressed = false;
      } else {
        if (!doorbellPressed) {
          doorbellPressed = true;
          soundDoorbell();
        }
      }
    } else {
      System.out.println("Missed Poll, speaker control in
	  progress");
    }
    isDoorbellPolling = false;
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      System.out.println("Insomina!");
    }
  }
}

Additional Code for this Article zip file ~9 KB