Arduino Function: BLEPeripheral.poll

Name

poll

Description

Poll the radio for events

Syntax

blePeripheral.poll();

Parameters

void

Returns

none 

Note

This function is maintained only for backward compatibility. All the BLE events will be forwarded even if this function is not called.

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.
  The second characteristic shows the notify property. If notifications are enabled when the button is pressed the value is automatically updated on your central and the led will switch accordingly.
  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
  or use another board with led_switch_client example in File->Examples->BLE->Central menu.
*/

#include <BLEPeripheral.h>

// LED and button pin
#define LED_PIN 13

#define BUTTON_PIN  USER1_BUTTON

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

// create service
BLEService            ledService        = BLEService("19b10010e8f2537e4f6cd104768a1214");

// create switch and button characteristic
BLECharCharacteristic switchCharacteristic = BLECharCharacteristic("19b10011e8f2537e4f6cd104768a1214", BLERead | BLEWrite);
BLECharCharacteristic buttonCharacteristic = BLECharCharacteristic("19b10012e8f2537e4f6cd104768a1214", BLERead | BLENotify);

void setup() {

 Serial.begin(115200);

 // set LED pin to output mode, button pin to input mode
 pinMode(LED_PIN, OUTPUT);
 pinMode(BUTTON_PIN, INPUT);

 // set advertised local name and service UUID
 blePeripheral.setLocalName("LED Switch");
 blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

 // add service and characteristics
 blePeripheral.addAttribute(ledService);
 blePeripheral.addAttribute(switchCharacteristic);
 blePeripheral.addAttribute(buttonCharacteristic);

 // begin initialization
 blePeripheral.begin();

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

void loop() {
 // poll peripheral
 blePeripheral.poll();

 // read the current button pin state
 char buttonValue = digitalRead(BUTTON_PIN);

 // has the value changed since the last read
 bool buttonChanged = (buttonCharacteristic.value() != buttonValue);

 if (buttonChanged) {

// button state changed, update characteristics
switchCharacteristic.setValue(buttonValue);
buttonCharacteristic.setValue(buttonValue);
 }

 if (switchCharacteristic.written()) {
// update LED if central has written to characteristic
if (switchCharacteristic.value()) {
  Serial.println(F("LED on"));
  digitalWrite(LED_PIN, HIGH);
} else {
  Serial.println(F("LED off"));
  digitalWrite(LED_PIN, LOW);
}
 }
}




Written by arduino.org