Arduino Function: BLEService Constructor

Name

BLESerivce

Description

Constructor for BLEService class

Syntax

BLEService yourService = BLEService(uuid);

Parameters

uuid: const char*. Service’s UUID.

Returns

none 

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 read/write a characteristic to turn a LED on or off

  You can use nRFConnect app to read/write the characteristic

  https://www.nordicsemi.com/eng/Products/Nordic-mobile-Apps/nRF-Connect-for-mobile-previously-called-nRF-Master-Control-Panel


  In this example BLE_LED shows the status of the board. It will blink every 200 ms when the board is advertising.

  It will be on when the board is connected to a central. It will be off when the board is disconnected.

*/



#include <BLEPeripheral.h>


// LED pin

#define LED_PIN   LED_BUILTIN


// create peripheral instance

BLEPeripheralRole         blePeripheral     = BLEPeripheralRole();


// create service

BLEService            ledService        = BLEService("19b10000e8f2537e4f6cd104768a1214");


// create switch characteristic

BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);


void setup() {

 Serial.begin(9600);


 //initialize BLE led

 pinMode(BLE_LED, OUTPUT);


 // set LED pin to output mode

 pinMode(LED_PIN, OUTPUT);


 // set advertised local name and service UUID

 blePeripheral.setLocalName("LED");

 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());


 // add service and characteristic

 blePeripheral.addAttribute(ledService);

 blePeripheral.addAttribute(switchCharacteristic);


 // begin initialization

 blePeripheral.begin();


 Serial.println(F("BLE LED Peripheral"));

}


void loop() {

 BLECentralPeer central = blePeripheral.central();


 if (central) {

// central connected to peripheral

Serial.print(F("Connected to central: "));

Serial.println(central.address());


   // turn on BLE_LED when connected

   digitalWrite(BLE_LED, HIGH);


while (central.connected()) {

  // central still connected to peripheral

  if (switchCharacteristic.written()) {

    // central wrote new value to characteristic, update LED

    if (switchCharacteristic.value()) {

      Serial.println(F("LED on"));

      digitalWrite(LED_PIN, HIGH);

    } else {

      Serial.println(F("LED off"));

      digitalWrite(LED_PIN, LOW);

    }

  }

}


// central disconnected

Serial.print(F("Disconnected from central: "));

Serial.println(central.address());

 }



 // here we are not connected. This means we are advertising

 digitalWrite(BLE_LED, HIGH);

 delay(200);

 digitalWrite(BLE_LED, LOW);

 delay(200);

}

Written by arduino.org