HOW ARDUINO READ THE BLUETOOTH SHIELD WHEN BLUETOOTH CONNECTION WAS LOST

Hi everyone, i’m newbie here :slight_smile:
i really hope you guys would like to help my project.
I want to set up a simple communication with my tablet and my arduino via bluetooth and the digital buzzer for alarm.

My goal is to simply turn on the digital buzzer when the bluetooth connection is lost, and turn off when bluetooth connection is connected to my tablet.
but i have a problem with my digital buzzer. It’s not turning on when the bluetooth connection is lost, how arduino read if bluetooth connection is lost ?
I don’t want to send or receive a character between my tablet and arduino (not using recvChar code), just need to connect each other. Just for tracking a case and bag at bus or train using bluetooth as transmission. And when the case or bag is far from the owner, the bluetooth connection is lost and the buzzer will turn on at the bag and case as a alarm.

Here is my code:

#include <SoftwareSerial.h>   //Software Serial Port
#define RxD 7
#define TxD 6
int buzzPin = 4;
 
SoftwareSerial blueToothSerial(RxD,TxD);
 
void setup() 
{ 
  Serial.begin(9600);
  pinMode(RxD, INPUT);
  pinMode(TxD, OUTPUT);
  pinMode(buzzPin, OUTPUT);
  setupBlueToothConnection();
 
} 
 
void loop() 
{ 
  while(1){
    if(blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
    digitalWrite(buzzPin, LOW);
    delay(1);
    }
   if(!blueToothSerial.available()){//check if there's any data sent from the remote bluetooth shield
    digitalWrite(buzzPin, HIGH);
    delay(1);
    } 
    }
}


 
void setupBlueToothConnection()
{
  blueToothSerial.begin(38400); //Set BluetoothBee BaudRate to default baud rate 38400
  blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
  blueToothSerial.print("\r\n+STNA=Receiverone\r\n"); //set the bluetooth name as "SeeedBTSlave"
  blueToothSerial.print("\r\n+STPIN=0000\r\n"); 
  blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
  blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
  delay(2000); // This delay is required.
  blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable 
  Serial.println("The slave bluetooth is inquirable!");
  delay(2000); // This delay is required.
  Serial.println("2 seconds passed");
  blueToothSerial.flush();
}

i really happy and appreciate for a suggestion, thank you so much guys :slight_smile:

i really happy and appreciate for a suggestion

OK. Get rid of the stupid while loop in the loop() function. The loop() function IS an infinite loop.

The only way that the Arduino will know that the bluetooth connection is lost is if it sends data, and does not get a response.

Your code never sends anything. If some data did arrive from the device, you never read it, so the buzzer pin would be set to LOW permanently.

Okay i get it now, so what's the right Arduino's code for this problem ? Am i should send and receive a character between my tablet and Arduino ? please, help :) thank you so much :)

Okay i get it now, so what's the right Arduino's code for this problem ?

What is the problem?

In order to verify that a connection exists, whatever is on the other end of the bluetooth device needs to send a steady (not continuous) stream of data. The Arduino needs to note when each character is read. If the interval between reads (significantly) exceeds the normal interval, the connection has been lost.

my problem is : 1. i can't turn on the buzzer when my bluetooth connection is lost. would you like to help me for the code ? what is the right arduino's code for this project ? my project : - my device monitoring a bag with bluetooth connection. i put my device(bluetooth shield + arduino UNO and digital buzzer) at the bag, then i connect the bluetooth on my android tablet to my device. When my bluetooth connection is lost because my bag is out of range, buzzer at my device will turn on as alarm. i'm very appreciate for any help about this :D pardon my english.. XD

sir, did get the correct coding for this? can you shared it with me ? i'll really appreciate it