0
0
Drone-programmingConceptBeginner · 3 min read

BLE Service and Characteristic: What They Are and How They Work

In Bluetooth Low Energy (BLE), a service is a collection of data and behaviors that represent a feature of a device, while a characteristic is a single piece of data or value within that service. Services group related characteristics to organize device capabilities for communication.
⚙️

How It Works

Think of a BLE device like a smart toolbox. The service is like a specific drawer in the toolbox that holds tools for a particular job, such as measuring temperature or controlling lights. Each characteristic inside that drawer is a single tool, like a thermometer or a switch.

When a phone or another device connects to the BLE device, it looks inside these drawers (services) to find the tools (characteristics) it needs. Each characteristic holds data or allows control commands, such as reading the current temperature or turning a light on or off.

This organization helps devices communicate clearly and efficiently by grouping related data and functions together.

💻

Example

This example shows how to define a BLE service with one characteristic using Python and the bleak library to simulate a BLE peripheral.

python
from bleak import BleakGATTCharacteristic, BleakGATTService, BleakServer

# Define a UUID for the service and characteristic
SERVICE_UUID = "12345678-1234-5678-1234-56789abcdef0"
CHARACTERISTIC_UUID = "12345678-1234-5678-1234-56789abcdef1"

async def run_server():
    server = BleakServer()

    # Add a service
    service = BleakGATTService(SERVICE_UUID)
    server.add_service(service)

    # Add a characteristic to the service
    characteristic = BleakGATTCharacteristic(CHARACTERISTIC_UUID, ['read', 'write'])
    service.add_characteristic(characteristic)

    # Set initial value
    characteristic.value = bytearray(b"Hello BLE")

    print(f"Service {SERVICE_UUID} with characteristic {CHARACTERISTIC_UUID} started.")

    await server.start()

import asyncio
asyncio.run(run_server())
Output
Service 12345678-1234-5678-1234-56789abcdef0 with characteristic 12345678-1234-5678-1234-56789abcdef1 started.
🎯

When to Use

Use BLE services and characteristics when you want to organize and share data or control commands between a small wireless device and a phone or computer. For example:

  • Fitness trackers use services to group heart rate data and step counts.
  • Smart home devices use services to control lights, locks, or sensors.
  • Medical devices share patient data like temperature or blood pressure through characteristics.

This structure makes it easy to find and use specific device features without confusion.

Key Points

  • A service groups related data and functions on a BLE device.
  • A characteristic is a single data value or control point within a service.
  • Services and characteristics use unique IDs called UUIDs to identify them.
  • Clients read, write, or get notified about characteristic values to interact with the device.

Key Takeaways

BLE services organize related data and functions on a device.
Characteristics are individual data points or controls inside services.
Each service and characteristic has a unique UUID for identification.
Clients use characteristics to read or control device features.
This structure simplifies communication between devices and apps.