ble.addService()

Description

Initializes a service and its characteristics and registers them with the NP. Services and characteristics should not be directly modified after they are added.

Syntax

ble.addService(&service);

Parameters

service: Pointer to an instance of BLE_Service. This and the contained BLE_Char’s should just be statically declared and initialized by the Energia user as shown in the example.

Returns

Example

    #include <BLE.h>

    int heartRateMeasurement = 0;

    BLE_Char heartRateChar =
    {
      {0x37, 0x2A}, // UUID for heart rate measurement
      BLE_READABLE, // The BLE client is able to only read this characterisitic value
      "Heart rate measurement" // Description shown to the client
    };

    /* This syntax lets services have any number of characteristics. */
    BLE_Char *heartRateChars[] = {&heartRateChar};

    BLE_Service heartRateService =
    {
      {0x0D, 0x18}, // UUID for heart rate service
      1, heartRateChars
    };

    void setup() {
      /* Start the BLE layer and connect to the network processor. */
      ble.begin();

      /* Add the heart rate service. */
      ble.addService(&heartRateService);

      /* Sets the characteristic value's type. Note that this can be changed by
      calling writeValue with a differently typed variable. */
      ble.writeValue(&heartRateChar, heartRateMeasurement);
    }

    void loop() {
      ble.handleEvents();
    }
Guide Home