Error when trying to send data to Thunkable app through Bluetooth

Hello,
I am trying to send multiple data values to my Thunkable app through Bluetooth. I am using an Arduino MKR WIFI 1010 to send data. I have successfully connected to my app via Bluetooth but whenever I try to send data, I get an error. I will attach below my Thunkable blocks, Arduino code, and picture of my error message. Any help and suggestions are greatly appreciated.

#include <ArduinoBLE.h>

BLEService batteryService("180F"); // Custom service UUID
BLECharacteristic batteryPercentageChar("2A19", BLERead | BLENotify, 20);
BLECharacteristic batteryVoltageChar("2A1C", BLERead | BLENotify, 20);

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

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

  // Set advertised local name and service UUID
  BLE.setLocalName("BatteryMonitor");
  BLE.setAdvertisedService(batteryService);

  // Add characteristics to the service
  batteryService.addCharacteristic(batteryPercentageChar);
  batteryService.addCharacteristic(batteryVoltageChar);

  // Add the service
  BLE.addService(batteryService);

  // Start advertising
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected()) {
      // Simulate battery percentage and voltage
      int batteryPercentage = 75; // Example percentage
      float batteryVoltage = 3.7; // Example voltage

      // Convert to strings
      String percentageStr = String(batteryPercentage);
      String voltageStr = String(batteryVoltage, 2); // Two decimal places

      // Write data to characteristics
      batteryPercentageChar.setValue(percentageStr.c_str());
      batteryVoltageChar.setValue(voltageStr.c_str());

      delay(1000);
    }

    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

Hi @chrismdisalvoohxko , welcome to Thunkable! :tada:
Is your hardware using the NOTIFY method?

Another creator had the same issue and was able to resolve it using this method: iOS Bluetooth App doesn’t receives any data - Questions about Thunkable / BLE / BlueTooth - Community

Hi,
I figured it out! In order to send the data, I needed to use a 32-bit characteristic UUID instead of a 4-bit characteristic UUID. I was then able to read data via BLE on an Android tablet. I still have not tested this on IOS but will be soon. Here is an updated code for anyone in the future.

#include <ArduinoBLE.h>


BLEService batteryService("180F"); // Custom service UUID

// Create characteristics using 32-bit UUIDs
BLECharacteristic batteryPercentageChar("00002A19-0000-1000-8000-00805F9B34FB", BLERead | BLENotify, 20);
BLECharacteristic batteryVoltageChar("00002A1C-0000-1000-8000-00805F9B34FB", BLERead | BLENotify, 20);

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

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

  // Set advertised local name and service UUID
  BLE.setLocalName("BatteryMonitor");
  BLE.setAdvertisedService(batteryService);

  // Add characteristics to the service
  batteryService.addCharacteristic(batteryPercentageChar);
  batteryService.addCharacteristic(batteryVoltageChar);

  // Add the service
  BLE.addService(batteryService);

  // Start advertising
  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    Serial.print("Connected to central: ");
    Serial.println(central.address());

    while (central.connected()) {
      // Simulate battery percentage and voltage
      int batteryPercentage = 75; // Example percentage
      float batteryVoltage = 3.7; // Example voltage

      // Convert to strings
      String percentageStr = String(batteryPercentage);
      String voltageStr = String(batteryVoltage, 2); // Two decimal places

      // Write data to characteristics
      batteryPercentageChar.writeValue(percentageStr.c_str());
      batteryVoltageChar.writeValue(voltageStr.c_str());

      delay(1000);
    }

    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

1 Like

Hi @chrismdisalvoohxko
Thank you for sharing an update and the code
Glad that is working now.