Skip to content

Bluetooth Low Energy (BLE) 📱


title: Bluetooth (BLE) publish: true date: 2025-01-27 12:00:00 update: 2025-01-27 12:00:00 description: Learn how to use Bluetooth Low Energy with tinyCore categories:

  • advanced
  • connectivity

tinyCore includes built-in Bluetooth Low Energy (BLE) support, allowing you to create wireless projects that connect to phones, computers, and other devices.

Bluetooth Low Energy is a wireless communication protocol designed for:

  • Low power consumption - Perfect for battery-powered projects
  • Short-range communication - Typically 10-100 meters
  • Simple data exchange - Ideal for sensors, controllers, and IoT devices
  • Phone Connectivity - Connect to iOS and Android apps
  • Data Transfer - Send sensor data to your phone
  • Remote Control - Control tinyCore from your phone
  • Notifications - Send alerts and status updates
  • Custom Apps - Create your own mobile applications
  • Smart Home Devices - Control lights, sensors, and appliances
  • Wearable Projects - Fitness trackers, smart watches
  • Remote Sensors - Environmental monitoring
  • Game Controllers - Custom input devices
  • IoT Devices - Internet of Things applications
  1. tinyCore Board - Any tinyCore variant
  2. Arduino IDE - With ESP32 board support
  3. Mobile Device - iOS or Android for testing
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// BLE Service and Characteristic UUIDs
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
// Connection callback
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("Device connected!");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Device disconnected!");
// Restart advertising
pServer->getAdvertising()->start();
}
};
void setup() {
Serial.begin(115200);
// Initialize BLE
BLEDevice::init("tinyCore BLE");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
// Add descriptor for notifications
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("BLE Server ready! Waiting for connections...");
}
void loop() {
if (deviceConnected) {
// Send data every second
String data = "Hello from tinyCore!";
pCharacteristic->setValue((uint8_t*)data.c_str(), data.length());
pCharacteristic->notify();
delay(1000);
}
}

Create your own BLE services for specific applications:

// Custom service for sensor data
#define SENSOR_SERVICE_UUID "12345678-1234-1234-1234-123456789abc"
#define TEMPERATURE_CHAR_UUID "87654321-4321-4321-4321-cba987654321"
BLEService* pSensorService;
BLECharacteristic* pTemperatureChar;

Send different types of data:

// Send sensor readings
float temperature = 23.5;
uint8_t tempBytes[4];
memcpy(tempBytes, &temperature, 4);
pTemperatureChar->setValue(tempBytes, 4);
pTemperatureChar->notify();

Implement pairing and encryption:

// Enable security
BLESecurity *pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_BOND);
pSecurity->setCapability(ESP_IO_CAP_OUT);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);

Control RGB LEDs from your phone:

// RGB LED pins
#define RED_PIN 25
#define GREEN_PIN 26
#define BLUE_PIN 27
// BLE characteristic for RGB values
BLECharacteristic* pRGBChar;
void setup() {
// Initialize RGB pins
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Setup BLE (similar to basic example)
// ...
// Create RGB characteristic
pRGBChar = pService->createCharacteristic(
RGB_CHAR_UUID,
BLECharacteristic::PROPERTY_WRITE
);
}
void loop() {
// Handle RGB updates in BLE callback
}

Send sensor data to your phone:

// Send IMU data
void sendIMUData() {
if (deviceConnected) {
// Read IMU
float accelX, accelY, accelZ;
// ... read sensor data ...
// Create JSON-like string
String data = "{\"accel\":{\"x\":" + String(accelX) +
",\"y\":" + String(accelY) +
",\"z\":" + String(accelZ) + "}}";
pCharacteristic->setValue((uint8_t*)data.c_str(), data.length());
pCharacteristic->notify();
}
}

For testing, use these free apps:

  • nRF Connect (Android/iOS)
  • BLE Scanner (Android)
  • LightBlue (iOS)

Use these frameworks for custom mobile apps:

  • React Native with react-native-ble-plx
  • Flutter with flutter_blue_plus
  • Native iOS with CoreBluetooth
  • Native Android with BluetoothLe
  1. Device Not Found

    • Check if BLE is enabled on your phone
    • Ensure tinyCore is advertising
    • Try restarting the BLE stack
  2. Connection Drops

    • Check signal strength
    • Reduce data transmission frequency
    • Implement reconnection logic
  3. Data Corruption

    • Use proper data formatting
    • Implement checksums
    • Check characteristic properties
// Enable BLE debugging
#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL 1
#define CONFIG_BT_NIMBLE_ROLE_CENTRAL 1
// Monitor connection status
void printConnectionStatus() {
Serial.print("Connected devices: ");
Serial.println(deviceConnected ? "1" : "0");
}
// Reduce power consumption
BLEDevice::setMTU(23); // Use smaller packets
pAdvertising->setMinInterval(0x20); // Slower advertising
pAdvertising->setMaxInterval(0x40);
// Optimize for high data rates
#define BLE_MTU_SIZE 512
BLEDevice::setMTU(BLE_MTU_SIZE);
  • Explore ESP-NOW - For device-to-device communication
  • Learn WiFi - For internet connectivity
  • Check out User Projects - See what others have built
  • Join the Community - Get help on Discord

Ready to build wireless projects? Start with the RGB Mood Light Tutorial or explore other connectivity options!