Stopping and Starting Bluetooth on ESP32

Hi everyone, I am using the BLEkeyboard library with an ESP32, and have a momentary button disable or enable Bluetooth on an ESP32. This is working well with a boolean toggle function.

Currently, btStop(); does a fine job in starting Bluetooth, but unfortunately, neither btStart(); or bleKeyboard.begin(); seems to reconnect Bluetooth to my device (iOS).

I currently have it so that my entire program resets which accomplishes the result I am looking for, but is a bit messy. Any ideas on what command or set of commands will reconnect Bluetooth after a btStop(); command is issued?

Please post a complete sketch illustrating the problem

/*
  BLE keyboard,BLE keyboard, ESP32
*/

#include <BleKeyboard.h>

const int buttonPinA = 2;  //13
const int buttonPinB = 21;  //32

boolean oldPinStateA = HIGH;
boolean oldPinStateB = HIGH;

boolean toggle = !toggle;

void setup() {
  Serial.begin(115200);
  Serial.println("Starting Bluetooth Service");

  bleKeyboard.begin();

  pinMode(buttonPinA, INPUT_PULLUP);
  pinMode(buttonPinB, INPUT_PULLUP);

}


void loop() {

  if (bleKeyboard.isConnected()) {
    digitalWrite(LED, HIGH);
    delay (ON_TIME);
    digitalWrite(LED, LOW);
    delay (OFF_TIME);


    // Button Press A and B (Stop BT)
    if (digitalRead(buttonPinA) == LOW && digitalRead(buttonPinB) == LOW && toggle == true) {
      Serial.println("button A and B are pressed");
      Serial.println("Pausing Bluetooth");
      oldPinStateA = LOW;
      oldPinStateB = LOW;

      btStop();
      Serial.println("(inverting toggle variable)");
      toggle = !toggle;

    }
    else {
      oldPinStateA = HIGH;
      oldPinStateB = HIGH;
    }

    // Button Press A and B (Start BT)
    if (digitalRead(buttonPinA) == LOW && digitalRead(buttonPinB) == LOW && toggle == false) {
      Serial.println("button A and B are pressed");
      Serial.println("Resuming Bluetooth");
      oldPinStateA = LOW;
      oldPinStateB = LOW;

      bleKeyboard.begin();
      btStart();
      Serial.println("(inverting toggle variable)");
      toggle = !toggle;

    }
  }
}

Here is is.

  • Two buttons pressed - disables Bluetooth.
  • Toggle flipped so that next time two buttons pressed runs the second function.
  • Attempted to start Bluetooth, but it never reconnects on my iOS device.
    (I have confirmed the toggle is working via Serial prints.)

Your first if() statement checks for both buttons pressed and toggle == true.

If that is true, you change toggle.

You then immediately test if both buttons are pressed and toggle == false.

I'm betting that is a true statement since humans don't react on MCU time and you just changed toggle.

You should have nested if() statements.

    if (digitalRead(buttonPinA) == LOW && digitalRead(buttonPinB) == LOW) {
      if ( toggle == false) {
        // do stuff
      } else {
      // do other stuff
      }
      toggle = !toggle;
}
1 Like

Good catch, here's the updated code, (I had it working before, but had to scrap it, so I re-wrote it incorrectly for the example) -- should be correct now.

  void loop() {

    if (bleKeyboard.isConnected()) {

      if (digitalRead(buttonPinA) == LOW && digitalRead(buttonPinB) == LOW) {
        if ( toggle == false) {
          btStop();
          Serial.println( F ( "BT switched off" ) );

        } else {

          btStart();
          Serial.println( F ( "BT switched on" ) );

          oldPinStateA = HIGH;
          oldPinStateB = HIGH;

        }
        toggle = !toggle;
      }

I will note that even if all there was in the loop section was the following (below), it would still not start up the Bluetooth again, which makes me think the ESP32 needs more instructions to restart the Bluetooth service once stopped...

void loop() {
  
  if (bleKeyboard.isConnected()) {
    
    btStop():
    delay(10000);
    btStart();
    delay(10000);

    }
  }

It seems like these are out of order. Don't you want to start up Bluetooth and then initialize your keyboard?

1 Like

I have tried them in both orders. :confused:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.