Last active
February 21, 2025 00:03
-
-
Save swordfish6975/93c2257520f0be2a1e95c78163273ff3 to your computer and use it in GitHub Desktop.
JBL Flip 5 Stereo Mode FIX using ESP32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "BLEDevice.h" | |
| //#include "BLEScan.h" | |
| //Operating procedure | |
| //1. Power on only the JBL Flip 5 you want to connect to the "master of the two speakers" | |
| //2. It is ok to have it connected to your (phone/laptop/TV etc) or not at this stage | |
| //3. Plug in or reset your ESP32 with this code in it | |
| //4. Built in LED should be solid from startup, if it shuts off for two seconds the master Flip 5 was found and set to RIGHT channel mode | |
| //5. Press the ♾️ on the master speaker | |
| //6. Power on the slave speaker | |
| //7. Press ♾️ on the slave speaker (might need to wait a bit before it lets you) (the master speaker will put this one to LEFT channel) | |
| //8. Play some LEFT RIGHT test music on youtube or spotify | |
| //9. 🥳 | |
| //If something didnt work reset both speakers to default (Press the Volume Up and Play buttons together. Hold them down together for about 10-20 seconds or until the status light turns off.) | |
| //NOTE: Wont work with ESP8266, as it has no bluetooth... | |
| static BLEUUID serviceUUID("65786365-6c70-6f69-6e74-2e636f6d0000"); | |
| static BLEUUID charUUID("65786365-6c70-6f69-6e74-2e636f6d0002"); | |
| static boolean doConnect = false; | |
| static boolean connected = false; | |
| static boolean doScan = false; | |
| static BLERemoteCharacteristic *pRemoteCharacteristic; | |
| static BLEAdvertisedDevice *myDevice; | |
| //I am using a LoLin32, for some other esp32 boards it seems this is pin 2 and the LOW and HIGH need to be fliped | |
| //Built in Light should be on the whole time and turn off for about 2 seconds when its written the RIGHT RemoteCharacteristic | |
| //https://github.com/espressif/arduino-esp32/issues/359 | |
| int LED_BUILTIN = 22; | |
| static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) { | |
| Serial.print("Notify callback for characteristic "); | |
| Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str()); | |
| Serial.print(" of data length "); | |
| Serial.println(length); | |
| Serial.print("data: "); | |
| Serial.write(pData, length); | |
| Serial.println(); | |
| } | |
| class MyClientCallback : public BLEClientCallbacks { | |
| void onConnect(BLEClient *pclient) {} | |
| void onDisconnect(BLEClient *pclient) { | |
| connected = false; | |
| Serial.println("onDisconnect"); | |
| } | |
| }; | |
| bool connectToServer() { | |
| Serial.print("Forming a connection to "); | |
| Serial.println(myDevice->getAddress().toString().c_str()); | |
| BLEClient *pClient = BLEDevice::createClient(); | |
| Serial.println(" - Created client"); | |
| pClient->setClientCallbacks(new MyClientCallback()); | |
| // Connect to the remove BLE Server. | |
| pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private) | |
| Serial.println(" - Connected to server"); | |
| pClient->setMTU(517); //set client to request maximum MTU from server (default is 23 otherwise) | |
| // Obtain a reference to the service we are after in the remote BLE server. | |
| BLERemoteService *pRemoteService = pClient->getService(serviceUUID); | |
| if (pRemoteService == nullptr) { | |
| Serial.print("Failed to find our service UUID: "); | |
| Serial.println(serviceUUID.toString().c_str()); | |
| pClient->disconnect(); | |
| return false; | |
| } | |
| Serial.println(" - Found our service"); | |
| // Obtain a reference to the characteristic in the service of the remote BLE server. | |
| pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); | |
| if (pRemoteCharacteristic == nullptr) { | |
| Serial.print("Failed to find our characteristic UUID: "); | |
| Serial.println(charUUID.toString().c_str()); | |
| pClient->disconnect(); | |
| return false; | |
| } | |
| Serial.println(" - Found our characteristic"); | |
| // Read the value of the characteristic. | |
| if (pRemoteCharacteristic->canRead()) { | |
| String value = pRemoteCharacteristic->readValue(); | |
| Serial.print("The characteristic value was: "); | |
| Serial.println(value.c_str()); | |
| } | |
| if (pRemoteCharacteristic->canNotify()) { | |
| pRemoteCharacteristic->registerForNotify(notifyCallback); | |
| } | |
| connected = true; | |
| return true; | |
| } | |
| /** | |
| * Scan for BLE servers and find the first one that advertises the service we are looking for. | |
| */ | |
| class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { | |
| /** | |
| * Called for each advertising BLE server. | |
| */ | |
| void onResult(BLEAdvertisedDevice advertisedDevice) { | |
| Serial.print("BLE Advertised Device found: "); | |
| Serial.println(advertisedDevice.toString().c_str()); | |
| String FlipName = advertisedDevice.getName(); | |
| String NameTarget = "JBL Flip 5"; | |
| //String macAdd = advertisedDevice.getAddress().toString(); | |
| // We have found a device, let us now see if it contains the service we are looking for. | |
| if (NameTarget == FlipName) { | |
| BLEDevice::getScan()->stop(); | |
| myDevice = new BLEAdvertisedDevice(advertisedDevice); | |
| doConnect = true; | |
| doScan = true; | |
| } // Found our server | |
| } // onResult | |
| }; // MyAdvertisedDeviceCallbacks | |
| void setup() { | |
| pinMode(LED_BUILTIN, OUTPUT); | |
| digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level) | |
| Serial.begin(115200); | |
| Serial.println("Starting Arduino BLE Client application..."); | |
| BLEDevice::init(""); | |
| // Retrieve a Scanner and set the callback we want to use to be informed when we | |
| // have detected a new device. Specify that we want active scanning and start the | |
| // scan to run for 5 seconds. | |
| BLEScan *pBLEScan = BLEDevice::getScan(); | |
| pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); | |
| pBLEScan->setInterval(1349); | |
| pBLEScan->setWindow(449); | |
| pBLEScan->setActiveScan(true); | |
| pBLEScan->start(5, false); | |
| } // End of setup. | |
| // This is the Arduino main loop function. | |
| void loop() { | |
| // If the flag "doConnect" is true then we have scanned for and found the desired | |
| // BLE Server with which we wish to connect. Now we connect to it. Once we are | |
| // connected we set the connected flag to be true. | |
| if (doConnect == true) { | |
| if (connectToServer()) { | |
| Serial.println("We are now connected to the BLE Server."); | |
| } else { | |
| Serial.println("We have failed to connect to the server; there is nothing more we will do."); | |
| } | |
| doConnect = false; | |
| } | |
| // If we are connected to a peer BLE Server, update the characteristic each time we are reached | |
| // with the current time since boot. | |
| if (connected) { | |
| //byte leftCommand[6] = { 0xaa, 0x15, 0x03, 0x00, 0x46, 0x01 }; | |
| //SET_SPEAKER_INFO(0x15), | |
| //TOKEN_AUDIO_CHANNEL(0x46), | |
| //LEFT(1), | |
| //RIGHT(2), | |
| //STEREO(0); | |
| byte rightCommand[6] = { 0xaa, 0x15, 0x03, 0x00, 0x46, 0x02 }; | |
| pRemoteCharacteristic->writeValue(rightCommand, sizeof(rightCommand)); | |
| Serial.println("Flip Done..."); | |
| connected = false; | |
| doConnect = false; | |
| doScan = false; | |
| digitalWrite(LED_BUILTIN, HIGH); | |
| delay(2000); | |
| digitalWrite(LED_BUILTIN, LOW); | |
| delay(2000); | |
| } else if (doScan) { | |
| BLEDevice::getScan()->start(0); // this is just example to start scan after disconnect, most likely there is better way to do it in arduino | |
| } | |
| digitalWrite(LED_BUILTIN, LOW); | |
| delay(1000); // Delay a second between loops. | |
| Serial.println("loop"); | |
| } // End of loop | |
Author
Author
tested with changes motioned here
https://gist.github.com/swordfish6975/93c2257520f0be2a1e95c78163273ff3#file-jbl-flip-5-stereo-ino-L27
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

LoLin32