
the LED. In the box that opens up, try entering either
00
or
01
, which should turn the LED either off or
on, respectively.
After tapping “Send” you should see the LED change state.
BLE Button Example
This example demonstrates how to use the BLE read and notify features. It monitors the button on
pin 6 of the nRF52832 Breakout. When the button state changes a BLE notification is sent.
The Code
Using the BLEPeripheral library, upload this code to your Breakout:
CopyCode
//Importlibraries(BLEPeripheraldependsonSPI)#include<SPI.h>#include<BLEPeripheral.h>
////////////////Hardware////////////////#defineBTN_PIN6//BTNpinon6#defineBTN_ACT
IVELOW/////////////////////////BLEAdvertisments/////////////////////////constchar*l
ocalName="nRF52832Button";BLEPeripheralblePeriph;BLEServicebleServ("1234");BLECharChar
acteristicbtnChar("1234",BLERead|BLENotify);voidsetup(){Serial.begin(115200);//Setu
pserialat115200baudpinMode(BTN_PIN,INPUT_PULLUP);digitalWrite(7,HIGH);setupBLE();}v
oidloop(){blePeriph.poll();//readthecurrentbuttonpinstatecharbuttonValue=digital
Read(BTN_PIN);//hasthevaluechangedsincethelastreadboolbuttonChanged=(btnChar.valu
e()!=buttonValue);if(buttonChanged){//buttonstatechanged,updatecharacteristicsbtnC
har.setValue(buttonValue);}}voidsetupBLE(){//Advertisenameandservice:blePeriph.setD
eviceName(localName);blePeriph.setLocalName(localName);blePeriph.setAdvertisedServiceUuid(bl
eServ.uuid());//AddserviceblePeriph.addAttribute(bleServ);//AddcharacteristicblePeriph
.addAttribute(btnChar);//Nowthatdevice,service,characteristicaresetup,//initialize
BLE:blePeriph.begin();}
// Import libraries (BLEPeripheral depends on SPI) #include <SPI.h> #include <BLEPeripheral.h>
////////////// // Hardware // ////////////// #define BTN_PIN 6 // BTN pin on 6 #define BTN_ACTIVE LOW
/////////////////////// // BLE Advertisments // /////////////////////// const char * localName = "nRF52832 Button";
BLEPeripheral blePeriph; BLEService bleServ("1234"); BLECharCharacteristic btnChar("1234",
BLERead | BLENotify); void setup() { Serial.begin(115200); // Set up serial at 115200 baud
pinMode(BTN_PIN, INPUT_PULLUP); digitalWrite(7, HIGH); setupBLE(); } void loop() {
blePeriph.poll(); // read the current button pin state char buttonValue = digitalRead(BTN_PIN); // has
the value changed since the last read bool buttonChanged = (btnChar.value() != buttonValue); if
(buttonChanged) { // button state changed, update characteristics btnChar.setValue(buttonValue); } }
void setupBLE() { // Advertise name and service: blePeriph.setDeviceName(localName);
blePeriph.setLocalName(localName); blePeriph.setAdvertisedServiceUuid(bleServ.uuid()); // Add
service blePeriph.addAttribute(bleServ); // Add characteristic blePeriph.addAttribute(btnChar); // Now
that device, service, characteristic are set up, // initialize BLE: blePeriph.begin(); }