hello, this is my first question, first of all thank you for your great book! i’m following it and it is great!
now, following your examples i wrote the following code, it creates an ESP32 BLE server with two characteristics, as starting values, and for me to understand witch i’m writing from the android bluetooth software, i set the values pwd and ssid. how you can imagine, i would like to sent ssid and password to the esp32 via bluetooth, and then it will connect. now, using one callback function fired at the write event from the client, how can i understand witch characteristic has changed, so to set the correct ssid and password?, and then, is this a right way, or is there(sure) a better way to achieve this? now tht i’m writing i’m thinking about wifi scanner for ssid, and so i can send only the password….but this is another story.
where can i find the documentation of the libraries that you use (BLEDevice,BLEUtils,BLEServer,BLE2902)?
here is my code for explanation,
#include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> #include <BLE2902.h> #define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define SSID_UUID "f8c2ade6-e7a1-4a4b-bb27-47b485063083" #define PASSWORD_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8" #define SERVER_NAME "MYSERVERBT" BLECharacteristic passwordCharacteristics(PASSWORD_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); BLEDescriptor passwordDescriptor(BLEUUID((uint16_t)0x2901)); BLECharacteristic ssidCharacteristics(SSID_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); BLEDescriptor ssidDescriptor(BLEUUID((uint16_t)0x2902)); class MyCallbacks: public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value.length() > 0) { Serial.println("*********"); Serial.println("New value: "); Serial.println(value.length()); Serial.println(); for (int i = 0; i < value.length(); i++) Serial.print(value[i]); Serial.println(); Serial.println("*********"); } } }; void setup() { Serial.begin(115200); BLEDevice::init(SERVER_NAME); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(SERVICE_UUID); pService->addCharacteristic(&passwordCharacteristics); passwordCharacteristics.setValue("PWD"); passwordCharacteristics.setCallbacks(new MyCallbacks()); pService->addCharacteristic(&ssidCharacteristics); ssidCharacteristics.setValue("SSID"); ssidCharacteristics.setCallbacks(new MyCallbacks()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void loop() { // put your main code here, to run repeatedly: delay(2000); }
thank you in advance!
Hi.
I’m sorry for taking so long to get back to you.
So, you want to be able to distinguish which characteristic is being written right? You can identify them by its UUID. Maybe if you get the characteristic UUID you are able to do that. See the methods here: http://www.neilkolban.com/esp32/docs/cpp_utils/html/class_b_l_e_characteristic.html#a706761c2593535beddd0311a57b54d6e
There is not much information about BLE libraries, but here is a good compilation: http://www.neilkolban.com/esp32/docs/cpp_utils/html/class_b_l_e_characteristic.html
I hope this helps.
Regards,
Sara