How to know Programmatically whether BlueSmirf BT is connected to a device

Hi All,

I am trying to know from code to whether our BT module i connected to any device or Not. I know the commands but still cant get the right .

#include <HID.h>

#include <Keyboard.h>


char dataFromBT = 0;
int alreadyLock = 11;
int alreadyUnLock = 111;
int ledPin = 9;
static const char status[] = "$$GK\r";
//static const char status[] = "$$SY,FFF4\r";
static const char *initVector[] = {status};

void setup()
{
  // initialize digital pin 13 as an output.
  pinMode(ledPin, OUTPUT);

    Serial1.begin(115200);  // The Bluetooth Mate defaults to 115200bps
}

void loop()
{
  if(Serial1.available())  {
 
    // Send any characters the bluetooth prints to the serial monitor 
    
         // Serial1.print(initVector[0]);
          dataFromBT = (char)Serial1.read();
         /// delay(7000);
          Serial1.print(dataFromBT);
          

if( alreadyLock == 11)
{
 
  if (dataFromBT == 0) {
      alreadyLock = 22;
      alreadyUnLock = 111;
      Keyboard.press(KEY_LEFT_CTRL);
      Keyboard.press(KEY_LEFT_ALT);
      Keyboard.press(KEY_DELETE);
      Keyboard.releaseAll();    
      delay(1000);
      Keyboard.write(KEY_RETURN); 
      delay(8000); 
      
      // waits for a second
      digitalWrite(ledPin, LOW);    
       // sets the LED off

  } 
  }else if (alreadyUnLock == 111){
  if(dataFromBT == '1') {

      alreadyLock = 11;
      alreadyUnLock = 222;
  
      digitalWrite(ledPin, HIGH);   // sets the LED on
      delay(8000);  
      //Turn on LEFD
      //Keyboard.press(32);
      Keyboard.write(' ');
      delay(1000); 

      Keyboard.print("up14y7434");
      Keyboard.write(KEY_RETURN); 
  }
 }
}

  // and loop forever and ever!
}

I don't know anything about BlueSmirf, but bluetooth devices typically have a state pin, which nobody uses but reflects the onboard status LED. I imagine you could connect that to a pin on Arduino and poll its condition.

Hey Thanks for the reply, I don't think there is any on BlueSmirf. I have read the Documentation. Anyways Thanks . Cheers !!

You have to send an AT command and see if you get something that makes sense in return.

Hi,

Yeah I tried that, here is the issue : I am trying to repeatedly send GK command that returns
1.0,0,0 no device connected
2.1,0,0 Device connected

I am trying to continuously ping the GK command to see if any device gets connected.

Problem: Initially Console prints 0,0,0 continuously but as soon s i connect a device the O/P is 4,0,0 and that to gets stuck after a while then nothing comes up.

Expected : It should continuously print the status weather connected or not.

Adding code and logs :

Code

static const char status = "GK\n";
static const char status_ = "$$$";
static const char *initVector = {status};

void setup()
{
Serial.begin(9600); // Begin the serial monitor at 9600bps

Serial1.begin(115200); // The Bluetooth Mate defaults to 115200bps

Serial1.print(status_);

}

void loop()
{
if(Serial1.available()) // If the bluetooth sent any characters
{
Serial1.print(initVector[0]);
// Send any characters the bluetooth prints to the serial monitor
Serial.print((char)Serial1.read());
}

// and loop forever and ever!
}

Logs:

0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
0,0,0
ERR
0,0,0
0,0,0
4,0,0
?
4,0,0
4,0,0
4,0,0
4,0,0
4,0,0
4,0,0
4,0,0

Now here it gets stuck.As per my logic inside the loop it should keep on printing the state.