import processing.bluetooth.*; final int STATE_FIND = 0; final int STATE_PLAY = 2; int state; char pressed; boolean changed = false; //// bluetooth library Bluetooth bt; //// discovered services Service[] services; //// status message String msg; //// connection to other player Client c; PFont font; void setup() { //// set up font font = loadFont(); textFont(font); //// initialize Bluetooth library c=null; services=null; bt = new Bluetooth(this, Bluetooth.UUID_SERIALPORT); bt.find(); msg = "Looking for devices..."; } void destroy() { bt.stop(); } void draw() { if (state == STATE_FIND) { background(255); fill(0); textAlign(LEFT); if (services == null) { text("Looking for MIDIPoet...\n\n" + msg, 2, 2, width - 4, height - 4); } else { String list = "Found:\n\n"; for (int i = 0, length = length(services); i < length; i++) { list += i + ". " + services[i].device.name + "\n"; } text(list, 2, 2, width - 4, height - 4); } } else { if (changed) { background(255); text("MIDIPoet ... " + pressed, 2, 2, width - 4, height - 4); c.writeChar('a'); c.flush(); int m = millis(); while ((millis()-m)<500) { } c.writeChar(pressed); c.flush(); changed=false; } } } void libraryEvent(Object library, int event, Object data) { if (library == bt) { switch (event) { case Bluetooth.EVENT_DISCOVER_DEVICE: msg = "Found device at: " + ((Device) data).address + "..."; break; case Bluetooth.EVENT_DISCOVER_DEVICE_COMPLETED: msg = "Found " + length((Device[]) data) + " devices, looking for MIDIPoet..."; break; case Bluetooth.EVENT_DISCOVER_SERVICE: msg = "Found MIDIPoet on " + ((Service[]) data)[0].device.address + "..."; break; case Bluetooth.EVENT_DISCOVER_SERVICE_COMPLETED: services = (Service[]) data; msg = "Search complete."; break; case Bluetooth.EVENT_CLIENT_CONNECTED: c = (Client) data; state = STATE_PLAY; break; } } } void keyPressed() { if (state == STATE_FIND) { if (services != null) { if ((key >= '0') && (key <= '9')) { int i = key - '0'; if (i < length(services)) { c = services[i].connect(); background(255); state = STATE_PLAY; } } } } else { pressed = key; changed = true; } }