Skip to content

Instantly share code, notes, and snippets.

@robo8080
Created October 23, 2017 11:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robo8080/df411237ac2aca3e5c6b91b842d157e9 to your computer and use it in GitHub Desktop.
Save robo8080/df411237ac2aca3e5c6b91b842d157e9 to your computer and use it in GitHub Desktop.
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
/*
RCBController:
https://itunes.apple.com/us/app/rcbcontroller/id689724127
http://rcbcontroller.micutil.com/
*/
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
BLEUUID SERVICE_UUID = BLEUUID((uint16_t)0xFFF0); // RCBController Service UUID
BLEUUID CHARACTERISTIC_UUID = BLEUUID((uint16_t)0xFFF1); // RCBController Characteristic UUID
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
printf("connectionCallback\n\r");
};
void onDisconnect(BLEServer* pServer) {
printf("disconnectionCallback\n\r");
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() >= 10) {
printf("Data received: length = %d, data = 0x",value.length());
for(int x=0; x < value.length(); x++) {
printf("%02x", value[x]);
}
printf("\n\r");
}
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("MyESP32");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_WRITE_NR
);
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment