Arduino Function: BLEPeripheral.addAttribute

Name

addAttribute

Description

Add an attribute to the peripheral. The attribute can be a BLEService, a BLECharacteristic or a BLEDescriptor object.

Syntax

blePeripheral.addAttribute(service);

Parameters

service: BLELocalAttribute&. Service, characteristic or descriptor object.

Returns

none 

Note

Services, characteristics and descriptors have to be added following the BLE standard that requires a precise order. Hierarchical BLE data structure has to be followed:



Once a service is added, all its characteristics have to be added before to add the successive service. In the same way once a characteristic is added, all its descriptors have to be added before to add the successive characteristic.

For example if you have two services that has a certain number of characteristics with a certain number of descriptors the relative code will be:

// add first service with three characteristic
blePeripheral.addAttribute(serviceNoOne);
blePeripheral.addAttribute(characteristicOneOfSerivceOne);
blePeripheral.addAttribute(descriptorOne);
blePeripheral.addAttribute(characteristicTwoOfServiceOne);
blePeripheral.addAttribute(descriptorTwo);
blePeripheral.addAttribute(CharacteristicThreeOfSerivceOne);

// add second service with one characteristic
blePeripheral.addAttribute(serviceNoTwo);
blePeripheral.addAttribute(characteristicOfServiceTwo);

Note that a characteristic can also have no descriptors, as well as a service can have no characteristics.





Examples

/* Copyright (c) Sandeep Mistry. All rights reserved.
  Licensed under the MIT license. See LICENSE file in the project root for full license information.
  Modified by Chiara Ruggeri <chiara@arduino.org>;

  This example shows how to use the BLEPeripheral library.
*/

#include <BLEPeripheral.h>

// create peripheral instance
BLEPeripheral blePeripheral = BLEPeripheral();

// uuid's can be:
//   16-bit: "ffff"
//  128-bit: "19b10010e8f2537e4f6cd104768a1214" (dashed format also supported)
// create one or more services
BLEService service = BLEService("fff0");

// create one or more characteristics
BLECharCharacteristic characteristic = BLECharCharacteristic("fff1", BLERead | BLEWrite);

// create one or more descriptors (optional)
BLEDescriptor descriptor = BLEDescriptor("2901", "value");

void setup() {
 Serial.begin(115200);
 blePeripheral.setLocalName("local-name"); // optional
 blePeripheral.setAdvertisedServiceUuid(service.uuid()); // optional

 // add attributes (services, characteristics, descriptors) to peripheral
 blePeripheral.addAttribute(service);
 blePeripheral.addAttribute(characteristic);
 blePeripheral.addAttribute(descriptor);

 // set initial value
 characteristic.setValue(0);

 // begin initialization
 blePeripheral.begin();
}

void loop() {
 BLECentral central = blePeripheral.central();
 
if (central) {

// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());

while (central.connected()) {

  // central still connected to peripheral
  if (characteristic.written()) {

    // central wrote new value to characteristic
    Serial.println(characteristic.value(), DEC);

    // set value on characteristic
    characteristic.setValue(5);
  }
}

// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
 }
}




Written by arduino.org