Skip to content

Commit

Permalink
Renamed newData to btSerialEvent and switched listen() and connect() …
Browse files Browse the repository at this point in the history
…to only use the SPP UUID. Also updated the examples to reflect this.
  • Loading branch information
joshuaalbers committed Aug 23, 2012
1 parent 2d41243 commit 9c38ad0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 87 deletions.
2 changes: 1 addition & 1 deletion examples/SerialDuplex/SerialDuplex.pde
Expand Up @@ -42,7 +42,7 @@ void draw() {
text("Last Received: " + inByte, 10, 200);
}

void newData(BtSerial bt){
void btSerialEvent(BtSerial bt){
inByte = bt.read();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/SerialDuplexServer/SerialDuplexServer.pde
Expand Up @@ -47,7 +47,7 @@ void draw() {
}
}

void newData(BtSerial bt) {
void btSerialEvent(BtSerial bt) {
inByte = bt.read();
}

Expand Down
2 changes: 1 addition & 1 deletion examples/SimpleRead/SimpleRead.pde
Expand Up @@ -39,7 +39,7 @@ void draw() {
rect(width/2, height/2, width/3, width/3); // draw a square in the center of the screen
}

void newData(BtSerial bt) {
void btSerialEvent(BtSerial bt) {
val = bt.read(); //update val whenever new data is received
}

Expand Down
65 changes: 34 additions & 31 deletions examples/SimpleWrite/SimpleWrite.pde
Expand Up @@ -22,7 +22,7 @@ void setup() {

bt = new BtSerial(this); //create the BtSerial object that will handle the connection
println(bt.list(true)); //get list of paired devices (with extended information)
remoteAddress = bt.list()[2]; //get only the hardware address for the specific entry
remoteAddress = bt.list()[3]; //get only the hardware address for the specific entry

bt.connect(remoteAddress);
}
Expand All @@ -37,42 +37,45 @@ void draw() {
fill(0); // change color and
bt.write('L'); // send an L otherwise
}
rect(width/2, height/2, 100, 100); // Draw a square
rect(width/2, height/2, 200, 200); // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= (width/2-50)) && (mouseX <= (width/2-50)) && (mouseY >= (width/2+50)) && (mouseY <= (width/2+50)));
return ((mouseX >= (width/2-100)) && (mouseX <= (width/2+100)) && (mouseY >= (height/2-100)) && (mouseY <= (height/2+100)));
}


/*
// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value
// Adapted for BtSerial with Processing Android by Joshua Albers
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(8,9); // Serial Bluetooth Modem connected with TX to pin 8 and RX to pin 9
char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
bluetooth.begin(115200); // Start serial communication at 115200 bps
}
void loop() {
if (bluetooth.available()) { // If data is available to read,
val = bluetooth.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
}
else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}
// Read data from the serial and turn ON or OFF a light depending on the value
// Adapted for BtSerial with Processing Android by Joshua Albers
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(8,9); // Serial Bluetooth Modem connected with TX to pin 8 and RX to pin 9
char val; // Data received from the serial port
int ledPin = 13; // Set the pin to digital I/O 13
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
bluetooth.begin(115200); // Start serial communication at 115200 bps
}
void loop() {
if (bluetooth.available()) { // If data is available to read,
val = bluetooth.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
bluetooth.flush();
}
else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
bluetooth.flush();
}
delay(100); // Wait 100 milliseconds for next reading
}
*/
80 changes: 31 additions & 49 deletions src/cc/arduino/btserial/BtSerial.java
Expand Up @@ -63,10 +63,10 @@ public class BtSerial {
private BluetoothDevice mDevice;
private UUID uuidSpp = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
private UUID uuidSecure = UUID
.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
private UUID uuidInecure = UUID
.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
// private UUID uuidSecure = UUID
// .fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
// private UUID uuidInecure = UUID
// .fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");

/* Socket & streams for BT communication */
private BluetoothSocket mSocket;
Expand All @@ -81,7 +81,7 @@ public class BtSerial {
private int bufferIndex;
private int bufferLast;

Method newDataMethod;
Method btSerialEventMethod;

/* Debug variables */
public static boolean DEBUG = true;
Expand All @@ -104,34 +104,34 @@ public BtSerial(Context ctx) {
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
//Log.i(TAG, "BluetoothAdapter started");
// Log.i(TAG, "BluetoothAdapter started");

// reflection to check whether host applet has a call for
// public void serialEvent(processing.serial.Serial)
// which would be called each time an event comes in
try {
newDataMethod = ctx.getClass().getMethod("newData",
btSerialEventMethod = ctx.getClass().getMethod("btSerialEvent",
new Class[] { BtSerial.class });
} catch (Exception e) {
// no such method, or an error.. which is fine, just ignore
}
}

/*
* Callback triggered whenever there is data in the buffer.
*/

public void newData() {
if (newDataMethod != null) {
public void btSerialEvent() {
if (btSerialEventMethod != null) {
try {
newDataMethod.invoke(ctx, new Object[] { this });
// Log.i(TAG, "newData called from BtSerial");
btSerialEventMethod.invoke(ctx, new Object[] { this });
// Log.i(TAG, "btSerialEvent called from BtSerial");
} catch (Exception e) {
String msg = "error, disabling newData() for "
String msg = "error, disabling btSerialEvent() for "
+ mDevice.getName();
Log.e(TAG, msg);
e.printStackTrace();
newDataMethod = null;
btSerialEventMethod = null;
}
}
}
Expand Down Expand Up @@ -210,35 +210,34 @@ public String[] list(boolean info) {
public String[] list() {
return list(false);
}

/**
* Returns the name of the connected remote device
* It not connected, returns "-1"
* Returns the name of the connected remote device It not connected, returns
* "-1"
*/

public String getRemoteName() {
if (connected) {
String info = mDevice.getName();
return(info);
return (info);
} else {
return("-1");
return ("-1");
}
}

/**
* Returns the name of the connected remote device
* It not connected, returns "-1"
* Returns the name of the connected remote device It not connected, returns
* "-1"
*/

public String getRemoteAddress() {
if (connected) {
String info = mDevice.getAddress();
return(info);
return (info);
} else {
return("-1");
return ("-1");
}
}


/*
* Some stubs for future implementation:
Expand Down Expand Up @@ -299,26 +298,9 @@ public synchronized boolean connect(String mac) {
mDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
/* Create the RFCOMM sockets */
try {
int deviceMajorClass = mDevice.getBluetoothClass()
.getMajorDeviceClass();

if (deviceMajorClass == 512) { // if the device is a phone
mSocket = mDevice
.createRfcommSocketToServiceRecord(uuidSecure);
//Log.i(TAG, "connecting to phone");

} else if (deviceMajorClass == 256) { // if the device is a
// computer
mSocket = mDevice
.createRfcommSocketToServiceRecord(uuidSpp);
//Log.i(TAG, "connecting to computer");
} else if (deviceMajorClass == 7936) {
// if the device is uncategorized (like a BtMate modem for
// Arduino)
mSocket = mDevice
.createRfcommSocketToServiceRecord(uuidSpp);
//Log.i(TAG, "connecting to uncategorized");
}

mSocket = mDevice.createRfcommSocketToServiceRecord(uuidSpp);
// Log.i(TAG, "connecting to uncategorized");

mSocket.connect();

Expand Down Expand Up @@ -375,7 +357,7 @@ public AcceptThread() {
// Create a new listening server socket
try {
tmp = mAdapter.listenUsingRfcommWithServiceRecord(
"SecureSerial", uuidSecure);
"SerialPortProfile", uuidSpp);
} catch (IOException e) {
Log.e(TAG, "Socket listen() failed", e);
}
Expand Down Expand Up @@ -660,7 +642,7 @@ public synchronized void disconnect() {
connected = false;
/* If it successfully closes I guess we just return a success? */
// return 0;
//Log.i(TAG, "disconnected.");
// Log.i(TAG, "disconnected.");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i(TAG, "whoops! disconnect() encountred an error.");
Expand Down
8 changes: 4 additions & 4 deletions src/cc/arduino/btserial/ConnectedThread.java
Expand Up @@ -73,7 +73,7 @@ public void run() {
}
buffer[bufferLast++] = (byte) mmInStream.read();
}
newData();
btSerialEvent();
}
} catch (IOException e) {
Log.e(TAG, e.getMessage());
Expand All @@ -82,9 +82,9 @@ public void run() {
}
}

public void newData() {
mBtSerial.newData();
// Log.i(TAG, "newData called from ConnectedThread");
public void btSerialEvent() {
mBtSerial.btSerialEvent();
// Log.i(TAG, "btSerialEvent called from ConnectedThread");
}

/* Call this from the main Activity to send data to the remote device */
Expand Down

0 comments on commit 9c38ad0

Please sign in to comment.