How do I successfully write value from Arduino BLE to Hue Lamp?

I’m trying to write to a Hue Light Bulb from the Arduino Nano BLE Sense.

So far I can connect to the bulb and verify that the bulb can be written to however when I call the ‘writeValue’ method it always returns 0 (indicating failure) https://www.arduino.cc/en/Reference/ArduinoBLEBLECharacteristicwriteValue

I’m not sure where I’m going wrong or where the failure derives.

#include <ArduinoBLE.h>

void setup() {
  Serial.begin(9600);
  while(!Serial);

  if(!BLE.begin()){
    Serial.println("starting BLE failed!");
    while (1);
  }

  Serial.println("Starting to scan peripherals");
  BLE.scan();
}

void loop() {
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
     Serial.print("Found ");
     if (peripheral.address() == "<Bulb MAC>") {
      // stop scanning
      BLE.stopScan();

     turnOff(peripheral);
    }
  }
}

void turnOff(BLEDevice peripheral){
   // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");

    // discover peripheral attributes
    Serial.println("Discovering attributes ...");
    if (peripheral.discoverAttributes()) {
      Serial.println("Attributes discovered");
    } else {
      Serial.println("Attribute discovery failed!");
      peripheral.disconnect();
      return;
    }

    BLECharacteristic ledCharacteristic = peripheral.characteristic("932c32bd-0002-47a2-835a-a8d455b859dd"); //On/Off Characteristic of the bulb
    
    if (!ledCharacteristic) {
      Serial.println("Peripheral does not have LED characteristic!");
      peripheral.disconnect();
      return;
    } else if (!ledCharacteristic.canWrite()) {
      Serial.println("Peripheral does not have a writable LED characteristic!");
      peripheral.disconnect();
      return;
    }
    Serial.print("value = ");
    if(ledCharacteristic.value()) {
      Serial.println("on");
      } else {
      Serial.println("off");
      }
      
    int hasSent = ledCharacteristic.writeValue((byte)0x00);
    Serial.println(hasSent);
   
  } else {
    Serial.println("Failed to connect!");
    return;
  }
  
}

If anyone has any ideas or resources I’d greatly appreciate it.

Source: Windows Questions C++

LEAVE A COMMENT