I just received my ESP32C3 micro controller and only have one smart bulb to learn with. I wrote BLE scan code first and found the bulb. printed both UUIDs and address needed to connect. It wont connect but Im starting to think the service UUIDs are wrong. BLE scanner on my phone show 7 or so services. Only 2 of which allow write. Here is my code. Any help would be much appreciated.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEClient.h>
#include <BLEAddress.h>
#include <BLEHIDDevice.h>
// The BLE scan duration in seconds
#define BLE_SCAN_DURATION 5
// The MAC address of the Sylvania Smart+ bulb
#define SYLVANIA_SMART_BULB_ADDRESS "CHANGE ME"
// The service UUID and characteristic UUID of the Sylvania Smart+ bulb
#define SYLVANIA_SMART_BULB_SERVICE_UUID "CHANGE ME"
#define SYLVANIA_SMART_BULB_CHARACTERISTIC_UUID "CHANGE ME"
// Function to handle the BLE scan results
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
public:
void onResult(BLEAdvertisedDevice advertisedDevice) {
// Print the MAC address, service UUID, and characteristic UUID of the smart bulb
Serial.printf("Found smart bulb: %s\n", advertisedDevice.getAddress().toString().c_str());
Serial.printf("Service UUID: %s\n", advertisedDevice.getServiceUUID().toString().c_str());
Serial.printf("Characteristic UUID: %s\n", advertisedDevice.getServiceDataUUID().toString().c_str());
// Check if the smart bulb is the Sylvania Smart+ bulb
if (advertisedDevice.getAddress().toString() == SYLVANIA_SMART_BULB_ADDRESS) {
// Connect to the Sylvania Smart+ bulb
BLEClient* pClient = BLEDevice::createClient();
BLEAddress bulbAddress(SYLVANIA_SMART_BULB_ADDRESS);
pClient->connect(bulbAddress);
// Get the service and characteristic of the Sylvania Smart+ bulb
BLERemoteService* pService = pClient->getService(SYLVANIA_SMART_BULB_SERVICE_UUID);
BLERemoteCharacteristic* pCharacteristic = pService->getCharacteristic(SYLVANIA_SMART_BULB_CHARACTERISTIC_UUID);
// Turn off the Sylvania Smart+ bulb
pCharacteristic->writeValue("00 00 00 00 00 00");
// Disconnect from the Sylvania Smart+ bulb
pClient->disconnect();
}
}
};
void setup() {
Serial.begin(115200);
// Initialize the BLE device
BLEDevice::init("");
// Start the BLE scan
BLEScan* pScan = BLEDevice::getScan();
pScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pScan->setActiveScan(true);
pScan->start(BLE_SCAN_DURATION);
}
void loop() {
// Do nothing in the loop
}
I tried writing \Off \OFF \off 00 00 00 00 00 00 etc.. with my BLE scanner app, still no joy.
I still cant connect anyway so its a 2 step issue.