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.
What is BLE?
Section titled “What is BLE?”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
Key Features
Section titled “Key Features”✅ What tinyCore BLE Can Do
Section titled “✅ What tinyCore BLE Can Do”- 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
📱 Common Use Cases
Section titled “📱 Common Use Cases”- 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
Getting Started
Section titled “Getting Started”Prerequisites
Section titled “Prerequisites”- tinyCore Board - Any tinyCore variant
- Arduino IDE - With ESP32 board support
- Mobile Device - iOS or Android for testing
Basic BLE Server Example
Section titled “Basic BLE Server Example”#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 callbackclass 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); }}Advanced Features
Section titled “Advanced Features”Custom Services and Characteristics
Section titled “Custom Services and Characteristics”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;Data Formats
Section titled “Data Formats”Send different types of data:
// Send sensor readingsfloat temperature = 23.5;uint8_t tempBytes[4];memcpy(tempBytes, &temperature, 4);pTemperatureChar->setValue(tempBytes, 4);pTemperatureChar->notify();Security Features
Section titled “Security Features”Implement pairing and encryption:
// Enable securityBLESecurity *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);Project Examples
Section titled “Project Examples”1. RGB Mood Light Controller
Section titled “1. RGB Mood Light Controller”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 valuesBLECharacteristic* 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}2. Sensor Data Logger
Section titled “2. Sensor Data Logger”Send sensor data to your phone:
// Send IMU datavoid 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(); }}Mobile App Development
Section titled “Mobile App Development”Using BLE Scanner Apps
Section titled “Using BLE Scanner Apps”For testing, use these free apps:
- nRF Connect (Android/iOS)
- BLE Scanner (Android)
- LightBlue (iOS)
Creating Custom Apps
Section titled “Creating Custom Apps”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
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”-
Device Not Found
- Check if BLE is enabled on your phone
- Ensure tinyCore is advertising
- Try restarting the BLE stack
-
Connection Drops
- Check signal strength
- Reduce data transmission frequency
- Implement reconnection logic
-
Data Corruption
- Use proper data formatting
- Implement checksums
- Check characteristic properties
Debug Tips
Section titled “Debug Tips”// Enable BLE debugging#define CONFIG_BT_NIMBLE_ROLE_PERIPHERAL 1#define CONFIG_BT_NIMBLE_ROLE_CENTRAL 1
// Monitor connection statusvoid printConnectionStatus() { Serial.print("Connected devices: "); Serial.println(deviceConnected ? "1" : "0");}Performance Optimization
Section titled “Performance Optimization”Power Management
Section titled “Power Management”// Reduce power consumptionBLEDevice::setMTU(23); // Use smaller packetspAdvertising->setMinInterval(0x20); // Slower advertisingpAdvertising->setMaxInterval(0x40);Data Throughput
Section titled “Data Throughput”// Optimize for high data rates#define BLE_MTU_SIZE 512BLEDevice::setMTU(BLE_MTU_SIZE);Next Steps
Section titled “Next Steps”- 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!