From 5e5b5b78f0aeca611edb52ff4e885334b6fe46a9 Mon Sep 17 00:00:00 2001 From: kolban Date: Sun, 10 Sep 2017 13:36:10 -0500 Subject: 0.1.0 release --- .../examples/Arduino/BLE_write/BLE_write.ino | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino (limited to 'ESP32_BLE_Arduino/examples/Arduino/BLE_write') diff --git a/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino b/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino new file mode 100644 index 0000000..ed5ebc6 --- /dev/null +++ b/ESP32_BLE_Arduino/examples/Arduino/BLE_write/BLE_write.ino @@ -0,0 +1,68 @@ +/* + 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 +*/ + +#include +#include +#include + +BLEDevice ble; + +// See the following for generating UUIDs: +// https://www.uuidgenerator.net/ + +#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" +#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" + + +class MyCallbacks: public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic *pCharacteristic) { + std::string value = pCharacteristic->getValue(); + + if (value.length() > 0) { + Serial.println("*********"); + Serial.print("New value: "); + for (int i = 0; i < value.length(); i++) + Serial.print(value[i]); + + Serial.println(); + Serial.println("*********"); + } + } +}; + +void setup() { + Serial.begin(115200); + + Serial.println("1- Download and install an BLE scanner app in your phone"); + Serial.println("2- Scan for BLE devices in the app"); + Serial.println("3- Connect to MyESP32"); + Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something"); + Serial.println("5- See the magic =)"); + + //ble.begin("MyESP32"); + ble.init("MyESP32"); + BLEServer *pServer = new BLEServer(); + + BLEService *pService = pServer->createService(SERVICE_UUID); + + BLECharacteristic *pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID, + BLECharacteristic::PROPERTY_READ | + BLECharacteristic::PROPERTY_WRITE + ); + + pCharacteristic->setCallbacks(new MyCallbacks()); + + pCharacteristic->setValue("Hello World"); + pService->start(); + + BLEAdvertising *pAdvertising = pServer->getAdvertising(); + pAdvertising->start(); +} + +void loop() { + // put your main code here, to run repeatedly: + delay(2000); +} \ No newline at end of file -- cgit v1.2.3