Hello! I'm a student, and I've a project about
the interconnectionofa mobile deviceand aBluetooth module. I use a Processing (2.0b7) to program an application on my mobile (LG L7 Optimus under android 4.0.3) I must receivea signal from abluetooth module(FB155BC)which emitsin continuous. Mydevice ispaired(testedthroughapplications such asBluetoothSPP) and I see "0" or "1". I found alibrarythat perfectly answermyneeds:Btserial (
https://github.com/arduino/BtSerial) But whenI compilemyprogram,there is 1 luck on 20 that it work!
And when the program run I have a square but when I change the button status, the application
doesn't react, filling the square doesn't change color ... Did I make a mistake? Please
My program:
import cc.arduino.btserial.*;
BtSerial bt; // Create object from BtSerial class String remoteAddress; // MAC address of the device to which the Android will connect int val; // Data received from the Bluetooth serial port
void setup() { size(displayWidth, displayHeight); rectMode(CENTER); bt = new BtSerial(this); //create the BtSerial object that will handle the connection remoteAddress = "00:19:01:21:66:F1"; //my bluetooth Mac Address bt.connect(remoteAddress); }
void draw() { background(255); // Set background to white if (val == 0) { // If the serial value is 0, fill(0); // set fill to black } else { // If the serial value is not 0, fill(204); // set fill to light gray }
rect(width/2, height/2, width/3, width/3); // draw a square in the center of the screen }
void btSerialEvent(BtSerial bt) { val = bt.read(); //update val whenever new data is received }
void resume() { if (bt != null) { while (!bt.isConnected ()) { bt.connect(remoteAddress); } println("Bluetooth reconnected"); } }
My console view:
debug: btserial 0.1.6 by Andreas Göransson & David Cuartielles & Tom Igoe http://www.arduino.cc FATAL EXCEPTION: Animation Thread java.lang.NullPointerException at cc.arduino.btserial.BtSerial.connect(Unknown Source) at processing.test.simplereadbtserial_processing_.SimpleReadbtserial_processing_.setup(SimpleReadbtserial_processing_.java:34) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:856) Bluetooth disconnected
And my Arduino program:
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(8,9); // Modem Bluetooth série connecté à la broche 8 TX et 9 RX int switchPin = 2; // Interrupteur connecté à la broche 2
void setup() { pinMode(switchPin, INPUT); // Mettre la broche 0 en entrée bluetooth.begin(9600); // commencer la conversion série à 9600 bps }
void loop() { if (digitalRead(switchPin) == HIGH) // Si l'interrupteur est à "1", { bluetooth.write(byte(1)); // Envoie d'un 1 à Processing Serial.write("1"); } else // Sinon { bluetooth.write(byte(0)); // Envoie d'un 0 à Processing Serial.write("0"); } delay(100); // temporisation de 100 milliseconds }