Bluetooth HC-06 disconnects after a few minutes.

I can connect to the bluetooth module (HC-06) with any bluetooth device (phone, pc etc).
It works. I can send commands etc both ways.

But after a few minutes the connection breaks (and the bluetooth blinks rapidly again)
This happens even if I send commands or just let it sit idle.

I have 3.3v on the bluetooths RX pin with resistors.

here is the test code I have used.
I also have tried different baud rates.
What else should I test? I have tested both of my bluetooth modules. same thing happens on both.

#include <SoftwareSerial.h>

#define rxPin 10
#define txPin 11 //for software serial
SoftwareSerial mySerial(rxPin, txPin);
int state = 0;
const int led = 13;


void setup() {
  // put your setup code here, to run once:
  mySerial.begin(38400);
  mySerial.listen();
  Serial.begin(9600);
  Serial.println("Serial ok");
}

void loop() {
  // put your main code here, to run repeatedly:
  if (mySerial.available() > 0) {
    state = mySerial.read();
    Serial.println(state);
  }
  if (state == '0') {
    digitalWrite(led, LOW);
    Serial.println("led: OFF");
    state = 0;
  }
  else if (state == '1') {
    digitalWrite(led, HIGH);
    Serial.println("LED: ON");
    state = 0;
  }

}

Suggestions? I have been googling for answers and suggestions for days now ...

IF your project works at all, it suggests your code is kosher, but maybe you are taking a risk with software serial at 38400, and might either change to hardware serial or reduce speed to 9600. I bet you don't have a good reason to run it at 38400. IF it works initially AND then dies, it suggests inadequate power, about which you say nothing. Please don't tell us you are running it on a 9v PP3.

ah sorry. Should have mentioned that I have tried most of the usual baud rates. 9600, 115200, 38400 etc.

and yes, I have tried with power from the arduino 5V usb AND a separate PSU @5v 4A. no difference.

Seems like the Bluetooth timeouts after x seconds of inactivity.
When I was actively using it, it didn't disconnect.

So how can I keep the connection alive?

tiboine:
Seems like the Bluetooth timeouts after x seconds of inactivity.

No, that does not happen, and 5v 4A should be more than enough. Don't even think about 115200 with software serial, but that is not the cause of your problem. I have no idea what the problem is, but I would suspect it is mechanical, and the fact that it stays going when you use it is just a coincidence.

Don't think it's a coincidence. every time I send signals to the BT, it keeps the connection alive. And the moment I don't do anything for ~20 seconds, it disconnects.

Also, I forgot to mention that I have been unsuccessful at sending any AT commands. Following the instructions on the big internet.

Another observation:

I made the Arduino send a text string every 10 sec to a connected BT device (my phone).
It held the connection for several minutes, but only when I had the terminal tab active on my phone.

whatever

HC-06 is in AT mode by default. About the only way you can get this wrong on the big internet is by following the wrong instructions. Reply #6 sounds normal. Any problem there is not Arduino.

You might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Since you have mentioned a specific time, 20 sec, when this runs, perhaps it is time to see the code.