Bluetooth

Enable energy-efficient wireless communication with your Echo using Bluetooth Low Energy (BLE).

BLE Server Library Documentation

Overview

The BLE Server Library provides a simplified interface for creating Bluetooth Low Energy servers on the Echo. BLE is optimized for low power consumption and ideal for wireless control and data exchange in robotics applications.

Method
Description

BLEServerWrapper(name)

Creates a BLE server with the specified device name

begin()

Initializes the BLE subsystem (call once in setup)

connect()

Starts advertising and makes device discoverable

disconnect()

Stops connection and advertising

getStatus()

Returns true if a client is connected, false otherwise

autoReconnect(enable)

Enables or disables automatic reconnection after disconnect

sendData(data)

Sends string data to the connected client

readData()

Retrieves received data from client (clears after reading)

loop()

Handles reconnection and BLE management (call in main loop)


Quick Start Example

#include <EchoLib.h>

BLEServerWrapper ble("EchoBot");

void setup() {
  Serial.begin(115200);
  ble.begin();
  ble.connect();
}

void loop() {
  ble.loop();
  
  if (ble.getStatus()) {
    String cmd = ble.readData();
    if (cmd == "PING") {
      ble.sendData("PONG");
    }
  }
  
  delay(100);
}

Method Details

Constructor

BLEServerWrapper(const String& name)

Creates a BLE server with the specified advertised name. This name will be visible when scanning for BLE devices.


Initialization

void begin()

Initializes the BLE communication stack. Must be called once in setup() before any other BLE operations.


Connection Management

void connect()

Starts BLE advertising, making the device discoverable to nearby BLE clients.

void disconnect()

Stops the current connection and advertising. Useful for low-power modes or resetting connections.

bool getStatus()

Returns the current connection state.


Auto-Reconnection

void autoReconnect(bool enable)

Enables or disables automatic reconnection. When enabled (default), the server automatically resumes advertising after disconnection.


Data Communication

void sendData(const String& data)

Transmits string data to the connected client. Only works when a client is connected.

String readData()

Retrieves the most recent data received from the client. Returns an empty string if no new data. Data is automatically cleared after reading.


Loop Management

void loop()

Handles automatic reconnection and internal BLE state management. Must be called regularly in the main loop.


Complete Example

This example demonstrates LED control via BLE with status reporting.


Testing with nRF Connect

Connection Steps

  1. Scan and Connect

    • Open nRF Connect mobile app

    • Scan for devices

    • Find your device name (e.g., "EchoBot")

    • Tap CONNECT

  2. Enable Notifications

    • Expand service: 4fafc201-1fb5-459e-8fcc-c5c9c331914b

    • Find TX Characteristic: beb5483e...

    • Tap the notification icon (down arrow)

    • You'll now receive data from the Echo

  3. Send Commands

    • Find RX Characteristic: a4488e9d...

    • Tap the write icon

    • Select "Text" format

    • Type your command (e.g., "ON")

    • Tap "Send"


Important Notes

Connection Management

Always check connection status before sending data:


Data Size

Keep transmitted data concise for optimal performance. BLE has a limited packet size (20-244 bytes depending on MTU negotiation).

Good: "battery:85,temp:42" Avoid: Large multi-line messages or rapid sequential sends


Loop Delays

Always include delays in your main loop to maintain BLE stability:


Initialization Order

Always call begin() before connect():


Data Clearing

readData() clears data after reading. Store the value if needed later:


Power Consumption

BLE is low-power, but consider these typical current draws:

  • Advertising: ~15-20 mA

  • Connected (idle): ~8-12 mA

  • Connected (active): ~12-18 mA

For extended range, consider using an external IPEX-1 antenna.


Troubleshooting

Device Not Discoverable

  • Verify both begin() and connect() are called

  • Check Serial Monitor for initialization messages

  • Restart the scanning app

  • Ensure no other device is using the same name


Connection Drops

  • Enable auto-reconnect: ble.autoReconnect(true)

  • Verify stable power supply

  • Reduce distance between devices

  • Check for motor-induced electrical interference


No Data Received

  • Confirm connection with getStatus()

  • Enable notifications in your BLE client app

  • Verify correct characteristic (TX for receiving)

  • Check data format


Unexpected Resets

  • Add delay(1000) after Serial.begin()

  • Monitor power during motor operation

  • Verify adequate power supply


Technical Specifications

UUIDs

  • Service: 4fafc201-1fb5-459e-8fcc-c5c9c331914b

  • TX Characteristic: beb5483e-36e1-4688-b7f5-ea07361b26a8 (Notify)

  • RX Characteristic: a4488e9d-3c11-42fd-89c8-3ae3a57395e6 (Write)

Configuration

  • Data Encoding: UTF-8 strings

  • MTU Range: 20-244 bytes (negotiated)

  • I2C Pins: 17 (SDA), 18 (SCL) at 400kHz

Last updated