Skip to content

Instantly share code, notes, and snippets.

@francisli
Created December 7, 2011 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save francisli/1445243 to your computer and use it in GitHub Desktop.
Save francisli/1445243 to your computer and use it in GitHub Desktop.
Example sending an image file with the Processing Bluetooth library and Object Push Protocol
import com.francisli.processing.bluetooth.*;
PFont font;
PImage photo;
Bluetooth bt;
String statusMsg = "inactive";
Service[] services = new Service[0];
void setup() {
size(600,300);
photo = loadImage("urchins.jpg");
font = createFont("Courier", 15);
textFont(font);
try {
bt = new Bluetooth(this, Bluetooth.UUID_OPP);
bt.find();
statusMsg = "starting search";
}
catch (RuntimeException e) {
statusMsg = "bluetooth off?";
println(e);
}
}
void draw() {
background(0);
image(photo, 0, 0);
fill(255);
text("Status: " + statusMsg, 10, 30);
translate(20, 60);
text("Devices:", 0, 0);
if (services!=null) {
for (int i=0; i<services.length; i++) {
text(i + ": " + services[i].device.name + " @ " + services[i].device.address, 0, 30+i*20);
}
}
translate(300, 0);
text("Services:", 0, 0);
if (services!=null) {
for (int i=0; i<services.length; i++) {
text(services[i].name, 0,30+ i*20);
}
}
}
void serviceDiscoverEvent(Service[] s) {
statusMsg = "Found Service " + s[0].name + "...";
services = (Service[])append(services,s[0]);
}
void serviceDiscoveryCompleteEvent(Service[] s) {
services = (Service[])s;
statusMsg = "Search complete.";
}
void serviceSendProgressEvent(int bytesRead, int totalBytesRead, int totalBytes) {
println(totalBytesRead + "/" + totalBytes);
}
void keyPressed() {
int i = int("" + key);
println(i);
if ((i >= 0) && (i < services.length)) {
Service service = services[i];
service.send("urchins.jpg", "image/jpeg");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment