Aura Aura - 7 months ago 24
Java Question

Java bluetooth program not working

I'm using this program to send some data from Java to Arduino:

JAVA

public class Arduino extends PApplet{

public void connect(){
String[] serials = Serial.list();
Serial port = new Serial(this, Serial.list()[0], 9600);
port.write('H');
port.dispose();
}


ARDUINO

void setup() {
Serial.begin(9600);
Serial.println("Start");
}

void loop() {
if(Serial.available()){
int a = Serial.read();
Serial.print(a);
}
}


The problem is that every time I send data, I lose the bluetooth connection. Can someone explain what's wrong with it?

Thank you.

Answer

every time I send data, I lose the bluetooth connection.

for sure, you are calling the dispose method after sending the char, then is the connection gone.... try removing the dispose method instead..

public void connect(){
    String[] serials = Serial.list();
    Serial port = new Serial(this, Serial.list()[0], 9600);
    port.write('H');
    //port.dispose();
}