Skip to content

Bluetooth LE

Bluetooth LE Interface.

The Bluetooth LE interface provides a means of communicating with Bluetooth LE devices, acting as Central

Sysprops

The Bluetooth LE driver does not have any sysprops defined.

Example

This is a basic example of how to set up a BLE central.

cpp
#include "ble.h"

void my_ble_mgr_on_connect_cb (uint16_t hnd, void *ctx)
{
  printf("peripheral connected.\n");
}

void my_ble_mgr_on_disconnect_cb (uint16_t hnd, void *ctx)
{
   printf("peripheral disconnected.\n");
}

void
my_ble_mgr_on_write_cb (uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, bool done, void *ctx)
{
  // Add code to be executed when a peripheral device writes data to us
}

int my_ble_mgr_on_read_cb (uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, void *ctx)
{
  // not needed, since we use `notify` on the characteristic
}
static const ble_uuid128_t serv_uuid = {
        {0x1c, 0xc5, 0xd5, 0xa5},
        {0xff, 0x99},
        {0xa1, 0x85},
        {0xe4, 0x11, 0x24, 0xd2, 0x01, 0x00, 0x8c, 0xfb}
};

 static const ble_uuid128_t read_char_uuid = {
    {0x1c, 0xc5, 0xd5, 0xa5},
    {0x04, 0x00},
    {0x89, 0x86},
    {0xe4, 0x11, 0x29, 0xd2, 0x01, 0x00, 0x88, 0x77}
};

static const ble_uuid128_t write_char_uuid = {
    {0x1c, 0xc5, 0xd5, 0xa5},
    {0x05, 0x00},
    {0x89, 0x86},
    {0xe4, 0x11, 0x29, 0xd2, 0x01, 0x00, 0x88, 0x77}
};

void demo() {
    int deviceHandle = open("ble_device_name");
    struct ble_gatt *gattInterface = NULL;

    if (ble_get_gatt(deviceHandle, &gattInterface) == 0) {  // success
        gattInterface->init(my_ble_mgr_on_connect_cb,
        my_ble_mgr_on_disconnect_cb,
        my_ble_mgr_on_write_cb,
        my_ble_mgr_on_read_cb,
        NULL, 0);
    }

    gatt->begin_service(serv_uuid);

    h_read_char = gatt->add_characteristic(read_char_uuid,
                                           BLE_ATT_PROPERTY_WRITE, 20);

    h_write_char = gatt->add_characteristic(write_char_uuid,
                                           BLE_ATT_PROPERTY_READ | BLE_ATT_PROPERTY_NOTIFY
                                           | BLE_ATT_PROPERTY_INDICATE, 20);

    gatt->end_service();

    gatt->set_local_name((char *) "my_device_name");

    gatt->set_advertise_data(adv, 18);
}

Types

Name
structble_uuid128_t
Structure to convey information about UUIDs in BLE.
structble_gatt
Structure returned by the BLE driver to configure and interact with the peripheral.
typedef void(*)(uint16_t hnd, void *ctx)ble_manager_on_connect
typedef void(*)(uint16_t hnd, void *ctx)ble_manager_on_disconnect
typedef int(*)(uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, void *ctx)ble_manager_on_read
typedef void(*)(uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, bool done, void *ctx)ble_manager_on_write

Functions Overview

Name
CW_DRIVER_FUNCble_get_gatt(int hDev, struct ble_gatt ** gatt_if)

Defines

Name
BLE_IOCTL_SET_CONNECT_CALLBACKioctl to register a function to be called on an incoming connection from a BLE peripheral
BLE_IOCTL_SET_DISCONNECT_CALLBACKioctl to register a function to be called on disconection of a BLE peripheral
BLE_IOCTL_SET_LOCAL_NAMEioctl set a local name for the central to be advertised as
BLE_IOCTL_DISCONNECTioctl to disconnect any connected peripheral
BLE_IOCTL_START_ADVERTISINGioctl to start advertising
BLE_IOCTL_STOP_ADVERTISINGioctl to stop advertising
BLE_IOCTL_AUTO_RECONNECTioctl to enable/disable auto-reconnect, i.e. if advertising should be auto re-started when a connection ends
BLE_IOCTL_START_CW_TESTioctl to start compliance measurements (have the RF send a CW signal)
BLE_IOCTL_STOP_CW_TESTioctl to stop CW mode

Types Documentation

typedef ble_manager_on_connect

cpp
typedef void(* ble_manager_on_connect) (uint16_t hnd, void *ctx);

Parameters:

callback-prototype for peripheral-connection events

typedef ble_manager_on_disconnect

cpp
typedef void(* ble_manager_on_disconnect) (uint16_t hnd, void *ctx);

Parameters:

callback-prototype for peripheral-disconnection events

typedef ble_manager_on_read

cpp
typedef int(* ble_manager_on_read) (uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, void *ctx);

Parameters:

  • characteristic id of the characteristic being read
  • offset read offset
  • data data-buffer returning the data being read
  • len length of the data-buffer
  • ctx user defined context, set via ble_gatt::init()

callback-prototype for peripheral-read events

typedef ble_manager_on_write

cpp
typedef void(* ble_manager_on_write) (uint16_t characteristic, uint16_t offset, uint8_t *data, uint16_t len, bool done, void *ctx);

Parameters:

  • characteristic id of the characteristic being read
  • offset write offset
  • data data-buffer containng the data being written
  • len length of the data-buffer
  • done true, if this is the end of the ongoing write operation
  • ctx user defined context, set via ble_gatt::init()

callback-prototype for peripheral-write events

Function Details

function ble_get_gatt

cpp
CW_DRIVER_FUNC ble_get_gatt(
    int hDev,
    struct ble_gatt ** gatt_if
)

Parameters:

Return: 0 if successful, error code otherwise.

Macros Documentation

define BLE_IOCTL_SET_CONNECT_CALLBACK

cpp
#define BLE_IOCTL_SET_CONNECT_CALLBACK (0x0001)

ioctl to register a function to be called on an incoming connection from a BLE peripheral

define BLE_IOCTL_SET_DISCONNECT_CALLBACK

cpp
#define BLE_IOCTL_SET_DISCONNECT_CALLBACK (0x0002)

ioctl to register a function to be called on disconection of a BLE peripheral

define BLE_IOCTL_SET_LOCAL_NAME

cpp
#define BLE_IOCTL_SET_LOCAL_NAME (0x0003)

ioctl set a local name for the central to be advertised as

define BLE_IOCTL_DISCONNECT

cpp
#define BLE_IOCTL_DISCONNECT (0x0004)

ioctl to disconnect any connected peripheral

define BLE_IOCTL_START_ADVERTISING

cpp
#define BLE_IOCTL_START_ADVERTISING (0x0005)

ioctl to start advertising

define BLE_IOCTL_STOP_ADVERTISING

cpp
#define BLE_IOCTL_STOP_ADVERTISING (0x0006)

ioctl to stop advertising

define BLE_IOCTL_AUTO_RECONNECT

cpp
#define BLE_IOCTL_AUTO_RECONNECT (0x0007)

ioctl to enable/disable auto-reconnect, i.e. if advertising should be auto re-started when a connection ends

define BLE_IOCTL_START_CW_TEST

cpp
#define BLE_IOCTL_START_CW_TEST (0x0F01)

ioctl to start compliance measurements (have the RF send a CW signal)

define BLE_IOCTL_STOP_CW_TEST

cpp
#define BLE_IOCTL_STOP_CW_TEST (0x0F02)

ioctl to stop CW mode