Library for bidirectional serial communication between devices.
Don't forget to add the native RXTX libraries to classpath.
Import and initialize:
import de.voidplus.twowayserialcomm.*;
// ...
public TwoWaySerialComm com = new TwoWaySerialComm();
public HashMap<Long, String> msgs = new HashMap<Long, String>();Connect:
try {
com.connect("/dev/tty.RNBT-33AA-RNI-SPP", 9600);
} catch (Exception e) {
e.printStackTrace();
}Disconnect:
if(com.isConnected()){
com.disconnect();
}Read / Receive:
if(com.isConnected()){
this.msgs = com.read();
if(!this.msgs.isEmpty()){
for(String str : this.msgs.values()){
System.out.println(str);
}
}
}Write / Send:
if(com.isConnected()){
com.write("bar");
}#include <SoftwareSerial.h>
int bluetoothTx = 4;
int bluetoothRx = 3;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
#define LED_PIN 13
bool flag = false;
// simple json builder, b/c aJson needs to much storage
String buildKeyValueJson(String key, String value)
{
String result = "";
result = "{\""+key+"\":\""+value+"\"}";
return result;
}
void setup()
{
Serial.begin(9600);
bluetooth.begin(115200);
bluetooth.print("$");
bluetooth.print("$");
bluetooth.print("$");
delay(350);
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
bluetooth.println("foo");
String content = "";
while(bluetooth.available() > 0) {
content.concat((char)bluetooth.read());
}
if (content != "") {
Serial.println(content);
flag = !flag;
}
if(flag){
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}Arduino Breakboard with the Bluetooth BlueSMiRF Silver module.
Don't be shy and feel free to contact me via Twitter.
The library is Open Source Software released under the License.