Bluetooth-to-Bluetooth connection using blueSMiRF

This Processing code connects one BlueSMiRF radio from Sparkfun to another blueSMiRF.

 Language: Processing
 Connects to a blueSMiRF radio via a serial port, searches for 
 available bluetooth devices to connect to, looks for another
 blueSMiRF in the list, and connects to it.
 The program connects when you press any key.
 by Tom Igoe
 created 17 Oct. 2006

import processing.serial.*;

Serial myPort;                        // the serial connection
String responseString = "";           // response from the blueSMiRF
String[] responses = new String[20];  // array of strings in case you get a multi-line response
int nextLine = 0;                     // counter for the multi-line response
String targetAddress;                 // BT address of another device to connect to
boolean connected = false;            // whether you're connected to another BT device or not

void setup() {
  // set the window size:
  // print the list of serial ports, to find the one you need:

  // open the appropriate serial port. Change the port number 
  // to match the one that your BT radio is attached to:
  myPort = new Serial(this, Serial.list()[0], 9600);
  // tell the serial object to hold serial events until it gets a carriage return:

void draw() {
  // set the background color:
  // if you're connected and you get a message to exit, disconnect:
  if (connected) {
    if (responseString.equals("exit")) {

void keyReleased() {
  // if you're not connected, attempt to connect:
  if (!connected) {
  else {
    // if you're connected, the radio should be in data mode.
    // send any keystrokes through to the remote radio:
  btConnect method runs through all the commands necessary to make a connection:
void btConnect() {
  // send an AT command, wait for an OK:
  sendCommand("AT", "OK");
  // send a clear command, wait for an OK:
  sendCommand("ATUCL", "OK");
  // sent an inquiry command for up to 3 devices, wait for a DONE:
  sendCommand("ATDI,3,00000000", "DONE"); 
  // search the array of returned strings for a target address and connect:

  sendCommand method sends a command and waits for a response:
void sendCommand(String command, String whatResponse) {
  // send the command:
  myPort.write(command+ "\r");
  // wait for the appropriate response:
  while (!responseString.equals(whatResponse)) {

  findTarget looks through the array of received responses for a
 BT device type of 00000000. If it finds one, it connects to the
 associated address. 
void findTarget() {
  // go through all of the received strings before the OK
  // and look for comma-separated pairs:
  for (int i = 0; i < nextLine; i++) {
    String[] addressElements = split(responses[i], ",");
    // if you get a comma-separated pair where the last element
    // is 00000000, you have an address that's a blueSMiRF:
    if (addressElements.length > 1) {
      if (addressElements[1].equals("00000000")) {
        // save the first element of the address as the target address:
        targetAddress = addressElements[0];
  // reset the response array counter:
  nextLine = 0;
  // if you got a valid address, formulate a connect string with it:
  if (targetAddress !=null) {
    String thisResponse = "CONNECT,"+targetAddress;
    // send a connect command:
    sendCommand("ATDM,"+targetAddress+",1101", thisResponse); 
  else {
    println("no one to connect to!");
  disconnect method sends all commands necessary to disconnect:

void disconnect() {
  // send the data mode escape string:
  // send the hang up command:
  serialEvent method processes all the serial bytes as they come in:
void serialEvent(Serial myPort) {
  // read until you get a carriage return:
  String inString = myPort.readStringUntil('\r');
  // if the string isn't empty:
  if (inString != null) {
    // eliminate all whitespace:
    responseString = trim(inString);
    // if the line you got isn't an OK, add it to the responses array:
    if (!responseString.equals("OK")) {
      responses[nextLine] = responseString;
      // increment the response array counter:

    // if you got a CONNECT message, set the connected status to true:
    if (responseString.equals("CONNECT,"+targetAddress)) {
      connected = true;

    // if you get a DISCONNECT or a NO CARRIER message, set the connected status to false:
    if (responseString.equals("DISCONNECT") || responseString.equals("NO CARRIER")) {
      connected = false;
    // print out anything you get from the radio:
    println("response " +responseString);