Aurasphere Aurasphere - 7 months ago 55
Java Question

Arduino HC-05 Bluetooth module disconnects when sending data

I'm new to Arduino. I'm trying to build a program to control a breadboard through Arduino using the bluetooth module HC-05. At the moment I don't have anything on the breadboard and I'm just trying to test the connection. This is what I've done so far:

I put the module on the breadboard and I've paired it with the computer. When it's paired, the red led starts blinking slowly. I've connected the bluetooth Rx to Arduino Tx and Arduino Rx to bluetooth Tx following this tutorial: http://playground.arduino.cc/Learning/Tutorial01

I've also implemented both Java and Arduino programs following that tutorial. Here they are:

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 Java part should send the letter H to Arduino and Arduino should detect that and print it on the Serial monitor. But what actually happens is that I send data, and the led on the HC-05 starts blinking faster (which means the connection is lost). Why does that happen? I'm pretty confused. For the communication I'm using the port COM6 and COM7. It depends on how it connects. To see what port to use I just run the Java program: if the port is not correct, it will just get stuck and send nothing.

Any help is appreciated. Thanks!

Answer

In the end, I've solved this problem by removing this line of code:

port.dispose();

It would never work with it because I was getting rid of the connection. I don't know what I was thinking at the time I did that. That was very silly of me.