Open
Description
For example, in following scenario (Nordic UART Service implementation):
#include <CurieBLE.h>
BLEService NUS ("713D0000-503E-4C75-BA94-3148F18D941E");
BLECharacteristic rxChar ("713D0003-503E-4C75-BA94-3148F18D941E", BLEWrite | BLEWriteWithoutResponse, BLE_MAX_ATTR_DATA_LEN);
BLECharacteristic txChar ("713D0002-503E-4C75-BA94-3148F18D941E", BLERead | BLENotify, BLE_MAX_ATTR_DATA_LEN);
void setup() {
Serial.begin(9600);
// begin initialization
BLE.begin();
// set the local name peripheral advertises
BLE.setLocalName("NUS test");
BLE.setAdvertisedService(NUS);
// add the characteristic to the service
NUS.addCharacteristic(rxChar);
NUS.addCharacteristic(txChar);
// add service
BLE.addService(NUS);
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristics
rxChar.setEventHandler(BLEWritten, rxCharWritten);
txChar.setEventHandler(BLESubscribed, txCharSubscribed);
txChar.setEventHandler(BLEUnsubscribed, txCharUnsubscribed);
// set an initial value for characteristics
unsigned char empty[0] = {};
rxChar.setValue(empty, 0);
txChar.setValue(empty, 0);
// start advertising
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// poll for BLE events
BLE.poll();
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void txCharSubscribed(BLEDevice central, BLECharacteristic ch) {
Serial.println("Subscribed");
}
void txCharUnsubscribed(BLEDevice central, BLECharacteristic ch) {
Serial.println("Unsubscribed");
}
void rxCharWritten(BLEDevice central, BLECharacteristic ch) {
const uint8_t* data = ch.value();
uint32_t len = ch.valueLength();
Serial.print("Got data:");
Serial.write(data, len);
txChar.setValue((uint8_t*)data, len);
}