Skip to content

Instantly share code, notes, and snippets.

@appuchias
Last active August 25, 2024 01:19
Show Gist options
  • Select an option

  • Save appuchias/5470fe298eb756f9ba8f8c41cb3cf6a9 to your computer and use it in GitHub Desktop.

Select an option

Save appuchias/5470fe298eb756f9ba8f8c41cb3cf6a9 to your computer and use it in GitHub Desktop.
ESP32 BLE Media Player controls from serial

ESP32 BLE Media Player controls from serial

This is made mainly to test if your board supports sending them.

This code uses the ESP32-BLE-Keyboard library.

To avoid the ESP not sending the commands after a reconnection, please use at least version 0.3.1-beta and uncomment #define USE_NIMBLE on the first line of BleKeyboard.h. You will also need to install the NimBLE-Arduino library by h2zero inside the Arduino IDE.

In order to receive the commands, add the ESP32 as a Bluetooth keyboard of your computer or mobile phone:

  1. Go to your computers/phones settings
  2. Ensure Bluetooth is turned on
  3. Scan for Bluetooth devices
  4. Connect to the device called "ESP32 BLE Media Player"
  5. Connect to the serial of the ESP32
  6. Send one of the supported keys:
  • t: Toggle play/pause
  • p: Previous track
  • n: Next track
  • u: Volume up
  • d: VOlume down
#include <BleKeyboard.h>
BleKeyboard bleKeyboard("ESP32 BLE Media Player", "Appu", 33);
void setup() {
Serial.begin(115200);
Serial.setTimeout(100); // Read keys faster
Serial.println("Starting BLE work!");
bleKeyboard.begin();
}
void loop() {
if (bleKeyboard.isConnected()) {
String in = Serial.readString().substring(0,1);
if (in != "") {
if (in == "t") {
Serial.println("Toggle");
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
} else if (in == "p") {
Serial.println("Prev");
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
} else if (in == "n") {
Serial.println("Next");
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
} else if (in == "u") {
Serial.println("Vol.Up");
bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
} else if (in == "d") {
Serial.println("Vol.Dwn");
bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
} else {
Serial.printf("Read: %s\n", in);
}
}
bleKeyboard.releaseAll(); // Maybe not needed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment