|
/** NimBLE_Server Demo:
|
*
|
* Demonstrates many of the available features of the NimBLE server library.
|
*
|
* Created: on March 22 2020
|
* Author: H2zero
|
*
|
*/
|
|
#include <NimBLEDevice.h>
|
|
static NimBLEServer* pServer;
|
NimBLECharacteristic* pBeefCharacteristic;
|
int counter;
|
|
/** Handler class for characteristic actions */
|
class CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
|
void onRead(NimBLECharacteristic* pCharacteristic){
|
counter++;
|
pCharacteristic->setValue(counter);
|
Serial.print(pCharacteristic->getUUID().toString().c_str());
|
Serial.print(": onRead(), value: ");
|
Serial.println(pCharacteristic->getValue().c_str());
|
|
};
|
|
|
};
|
|
|
static CharacteristicCallbacks chrCallbacks;
|
|
|
void setup() {
|
Serial.begin(115200);
|
Serial.println("Starting NimBLE Server");
|
counter = 0;
|
/** sets device name */
|
NimBLEDevice::init("Acell_Miha_1");
|
|
/** Optional: set the transmit power, default is 3db */
|
//NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
|
|
/** Set the IO capabilities of the device, each option will trigger a different pairing method.
|
* BLE_HS_IO_DISPLAY_ONLY - Passkey pairing
|
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing
|
* BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing
|
*/
|
//NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); // use passkey
|
//NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO); //use numeric comparison
|
|
/** 2 different ways to set security - both calls achieve the same result.
|
* no bonding, no man in the middle protection, secure connections.
|
*
|
* These are the default values, only shown here for demonstration.
|
*/
|
//NimBLEDevice::setSecurityAuth(false, false, true);
|
NimBLEDevice::setSecurityAuth(/*BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC);
|
|
pServer = NimBLEDevice::createServer();
|
// pServer->setCallbacks(new ServerCallbacks());
|
|
NimBLEService* pDeadService = pServer->createService("DEAD");
|
pBeefCharacteristic = pDeadService->createCharacteristic(
|
"BEEF",
|
NIMBLE_PROPERTY::READ |
|
//NIMBLE_PROPERTY::WRITE |
|
/** Require a secure connection for read and write access */
|
NIMBLE_PROPERTY::READ_ENC // only allow reading if paired / encrypted
|
//NIMBLE_PROPERTY::WRITE_ENC // only allow writing if paired / encrypted
|
);
|
|
pBeefCharacteristic->setValue(counter);
|
pBeefCharacteristic->setCallbacks(&chrCallbacks);
|
|
/** 2904 descriptors are a special case, when createDescriptor is called with
|
* 0x2904 a NimBLE2904 class is created with the correct properties and sizes.
|
* However we must cast the returned reference to the correct type as the method
|
* only returns a pointer to the base NimBLEDescriptor class.
|
*/
|
// NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor("2904");
|
// pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
|
|
/** Note a 0x2902 descriptor MUST NOT be created as NimBLE will create one automatically
|
* if notification or indication properties are assigned to a characteristic.
|
*/
|
|
/** Custom descriptor: Arguments are UUID, Properties, max length in bytes of the value */
|
NimBLEDescriptor* pBeef2904 = pBeefCharacteristic->createDescriptor(
|
"C01D",
|
NIMBLE_PROPERTY::READ |
|
NIMBLE_PROPERTY::WRITE|
|
NIMBLE_PROPERTY::WRITE_ENC, // only allow writing if paired / encrypted
|
20
|
);
|
|
/** Start the services when finished creating all Characteristics and Descriptors */
|
pDeadService->start();
|
|
|
NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
|
/** Add the services to the advertisment data **/
|
pAdvertising->addServiceUUID(pDeadService->getUUID());
|
|
/** If your device is battery powered you may consider setting scan response
|
* to false as it will extend battery life at the expense of less data sent.
|
*/
|
pAdvertising->setScanResponse(true);
|
pAdvertising->start();
|
|
Serial.println("Advertising Started");
|
}
|
|
|
void loop() {
|
}
|