12#include <NimBLEDevice.h>
13#include "../../Defines.h"
14#define pettutorApproach
17static NimBLEServer* _pBLEServer;
31#define MAX_MESSAGE 600
55 void onConnect(NimBLEServer* pServer)
57 SerialInfo.println(
"Client connected");
58 SerialInfo.println(
"Multi-connect support: start advertising");
59 NimBLEDevice::startAdvertising();
64 void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc)
66 SerialInfo.print(
"Client address: ");
67 SerialInfo.println(NimBLEAddress(desc->peer_ota_addr).toString().c_str());
76 pServer->updateConnParams(desc->conn_handle, 24, 48, 0, 60);
82 void onDisconnect(NimBLEServer* pServer)
84 SerialInfo.println(
"Client disconnected - start advertising");
85 NimBLEDevice::startAdvertising();
87 void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc)
89 SerialInfo.printf(
"MTU updated: %u for connection ID: %u\n", MTU, desc->conn_handle);
94 uint32_t onPassKeyRequest()
96 SerialDebug.println(
"Server Passkey Request");
103 bool onConfirmPIN(uint32_t pass_key)
105 SerialDebug.print(
"The passkey YES/NO number: "); SerialDebug.println(pass_key);
110 void onAuthenticationComplete(ble_gap_conn_desc* desc)
113 if (!desc->sec_state.encrypted)
115 NimBLEDevice::getServer()->disconnect(desc->conn_handle);
116 SerialDebug.println(
"Encrypt connection failed - disconnecting client");
119 SerialDebug.println(
"Starting BLE work!");
128 void onRead(NimBLECharacteristic* pCharacteristic)
130 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
131 SerialDebug.print(
": onRead(), value: ");
133 SerialDebug.println(pCharacteristic->getValue().c_str());
137 void onWrite(NimBLECharacteristic* pCharacteristic)
139 SerialDebug.print(pCharacteristic->getUUID().toString().c_str());
140 SerialDebug.print(
": onWrite(), value: ");
147 std::string value = pCharacteristic->getValue();
148 SerialDebug.println(value.c_str());
150 if (value.length() > 0) {
151 SerialDebug.println(
"*********");
152 SerialDebug.printf(
"New value: %d - \n", value.length());
155 for (
int i = 0; i < value.length(); i++)
159 if (i >= value.length())
167 SerialDebug.println(
" **** REBOOT ***");
173 SerialDebug.println(pCharacteristic->getUUID().toString().c_str());
178 {SerialInfo.println(
"pChar->setValue(_ESP_32)");
180 pCharacteristic->setValue((
char*)
"_ESP_32");
188 SerialDebug.println(
"*** performing an M5 specific command from BLE trigger");
200#ifdef NOT_NOW_MQTT_NETWORKING
204 if (processJSONMessage(ascii))
207 pCharacteristic->setValue(0x01);
212 SerialInfo.println(
"Perform FEED");
217 pCharacteristic->setValue(0x01);
232 SerialDebug.println(
"*********");
244 void onNotify(NimBLECharacteristic * pCharacteristic)
246 SerialDebug.println(
"Sending notification to clients");
253 void onStatus(NimBLECharacteristic * pCharacteristic, Status status,
int code)
255#if (SERIAL_DEBUG_DEBUG)
256 String str = (
"Notification/Indication status code: ");
258 str +=
", return code: ";
261 str += NimBLEUtils::returnCodeToString(code);
262 SerialDebug.println(str);
266 void onSubscribe(NimBLECharacteristic * pCharacteristic, ble_gap_conn_desc * desc, uint16_t subValue)
268#if (SERIAL_DEBUG_DEBUG)
270 String str =
"Client ID: ";
271 str += desc->conn_handle;
273 str += std::string(NimBLEAddress(desc->peer_ota_addr)).c_str();
275 str +=
" Unsubscribed to ";
277 else if (subValue == 1) {
278 str +=
" Subscribed to notfications for ";
280 else if (subValue == 2) {
281 str +=
" Subscribed to indications for ";
283 else if (subValue == 3) {
284 str +=
" Subscribed to notifications and indications for ";
286 str += std::string(pCharacteristic->getUUID()).c_str();
288 SerialDebug.println(str);
296 void onWrite(NimBLEDescriptor* pDescriptor)
298#if (SERIAL_DEBUG_DEBUG)
302 SerialDebug.print(
"Descriptor written value:");
303 SerialDebug.println(pDescriptor->getValue());
305 std::string dscVal((
char*)pDescriptor->getValue(), pDescriptor->getLength());
306 SerialDebug.print(
"Descriptor written value:");
307 SerialDebug.println(dscVal.c_str());
313 void onRead(NimBLEDescriptor* pDescriptor)
315 SerialDebug.print(pDescriptor->getUUID().toString().c_str());
316 SerialDebug.println(
"Descriptor read");
336#ifdef originalApproach
337 if (_pBLEServer->getConnectedCount()) {
338 NimBLEService* pSvc = pServer->getServiceByUUID(
"BAAD");
340 NimBLECharacteristic* pChr = pSvc->getCharacteristic(
"F00D");
347#ifdef pettutorApproach
348 if (_pBLEServer->getConnectedCount()) {
349 NimBLEService* pSvc = _pBLEServer->getServiceByUUID(
_SERVICE_UUID);
366 SerialMin.printf(
"setup_BLEServerNetworking(%s,%s,%s,%s)\n", serviceName?serviceName:
"NULL", deviceName?deviceName:
"NULL", serviceUUID?serviceUUID:
"NULL", characteristicUUID?characteristicUUID:
"NULL");
369 SerialTemp.print(
"Stored DeviceName = ");
370 SerialTemp.println(storedDeviceName);
371 if (strcmp(serviceName,
"PTClicker")==0)
389 SerialMin.print(
"Setting BLE serviceName: ");
415 NimBLEDevice::setPower(ESP_PWR_LVL_P9);
430 NimBLEDevice::setSecurityAuth( BLE_SM_PAIR_AUTHREQ_SC);
433 _pBLEServer = NimBLEDevice::createServer();
434 _pBLEServer->setCallbacks(
new BLEServerCallbacks());
436 NimBLEService* pDeadService = _pBLEServer->createService(
"DEAD");
438 NimBLECharacteristic* pBeefCharacteristic = pDeadService->createCharacteristic(
442 NIMBLE_PROPERTY::READ |
443 NIMBLE_PROPERTY::WRITE |
445 NIMBLE_PROPERTY::READ_ENC |
446 NIMBLE_PROPERTY::WRITE_ENC
450#ifdef OLD_BLE_NO_ACK_APPROACH
458 pBeefCharacteristic->setValue(deviceName);
465 pBeefCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
472 NimBLE2904* pBeef2904 = (NimBLE2904*)pBeefCharacteristic->createDescriptor(
"2904");
473 pBeef2904->setFormat(NimBLE2904::FORMAT_UTF8);
474 pBeef2904->setCallbacks(&_descriptorBLEServerCallbacks);
477#ifdef originalApproach
478 NimBLEService* pBaadService = pServer->createService(
"BAAD");
479 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
481 NIMBLE_PROPERTY::READ |
482 NIMBLE_PROPERTY::WRITE |
483 NIMBLE_PROPERTY::NOTIFY
486#ifdef pettutorApproach
487 NimBLEService* pBaadService = _pBLEServer->createService(
_SERVICE_UUID);
488 NimBLECharacteristic* pFoodCharacteristic = pBaadService->createCharacteristic(
490 NIMBLE_PROPERTY::READ |
491 NIMBLE_PROPERTY::WRITE
496#ifdef originalApproach
497 pFoodCharacteristic->setValue(
"Fries");
500 pFoodCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
506#ifdef originalApproach
508 NimBLEDescriptor* pC01Ddsc = pFoodCharacteristic->createDescriptor(
510 NIMBLE_PROPERTY::READ |
511 NIMBLE_PROPERTY::WRITE |
512 NIMBLE_PROPERTY::WRITE_ENC,
515 pC01Ddsc->setValue(
"Send it back!");
516 pC01Ddsc->setCallbacks(&dscCallbacks);
518#ifdef pettutorApproach
520 NimBLEDescriptor* pPetTutordsc = pFoodCharacteristic->createDescriptor(
522 NIMBLE_PROPERTY::READ |
523 NIMBLE_PROPERTY::WRITE |
526 pPetTutordsc->setValue(
"feed s/a/j type u/m ");
527 pPetTutordsc->setCallbacks(&_descriptorBLEServerCallbacks);
530#ifdef CURRENT_BLE_ACK_APPROACH
537#ifdef TRY_THIRD_SERVICE_WITH_NAME
538#define DEVICE_NAME_UUID "53636F74-7479426F79"
539#define DEVICE_NAME_SERVICE "6E616D65"
541 NimBLEService* pNameService = _pBLEServer->createService(DEVICE_NAME_SERVICE);
543 NimBLECharacteristic* pNameCharacteristic = pNameService->createCharacteristic(
547 NIMBLE_PROPERTY::READ |
548 NIMBLE_PROPERTY::WRITE |
550 NIMBLE_PROPERTY::READ_ENC |
551 NIMBLE_PROPERTY::WRITE_ENC
557 pNameCharacteristic->setValue(deviceName);
564 pNameCharacteristic->setCallbacks(&_characteristicBLEServerCallbacks);
571 NimBLE2904* pName2904 = (NimBLE2904*)pNameCharacteristic->createDescriptor(
"2904");
572 pName2904->setFormat(NimBLE2904::FORMAT_UTF8);
573 pName2904->setCallbacks(&_descriptorBLEServerCallbacks);
574 pNameService->start();
579 pDeadService->start();
580 pBaadService->start();
582 NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
584 pAdvertising->addServiceUUID(pDeadService->getUUID());
585 pAdvertising->addServiceUUID(pBaadService->getUUID());
589#ifdef TRY_THIRD_SERVICE_WITH_NAME
590 pAdvertising->addServiceUUID(pNameService->getUUID());
593 pAdvertising->setScanResponse(
true);
594 pAdvertising->start();
603 SerialInfo.println(
"Advertising Started");
610 SerialTemp.printf(
"%d sendBLEMessageACKMessage._pCharacteristic\n",
_pCharacteristic);
void setBLEServerDeviceName(char *deviceName)
sets the device name
void loop_BLEServerNetworking()
the 'loop' for this module BLEServerNetworking.
void sendBLEMessageACKMessage()
send ACK over bluetooth, this right now is 0x01
char _asciiMessage[MAX_MESSAGE]
char * getServiceName_BLEServerNetworking()
retrieve the service name (PTFEEDER, PTFeeder:Name, PTClicker:Name, etc)
char * _CHARACTERISTIC_UUID
NimBLECharacteristic * _pCharacteristic
char * _SERVICE_UUID
need to device way to change these ...
char * _deviceName_BLEServer
device name
void setup_BLEServerNetworking(char *serviceName, char *deviceName, char *serviceUUID, char *characteristicUUID)
the 'setup' for this module BLEServerNetworking. Here the service name is added (and potentially more...
char _serviceName_BLEServer[100]
#define BLE_SERVER_CALLBACK_ONWRITE
void addToTextMessages_displayModule(String text)
void incrementScreenColor_displayModule()
void showText_displayModule(String text)
char * deviceName_mainModule()
gets the device name
#define CALLBACKS_BLE_SERVER
void processClientCommandChar_mainModule(char cmd)
single character version of processClientCommand (since many used that already)
void rebootDevice_mainModule()
void callCallbackMain(int callbacksModuleId, int callbackType, char *message)
performs the indirect callback based on the callbackType
boolean getPreferenceBoolean_mainModule(int preferenceID)
called to set a preference (which will be an identifier and a string, which can be converted to a num...
#define PREFERENCE_BLE_SERVER_USE_DEVICE_NAME_SETTING
if set, the BLE Server (like PTFeeder) will tack on the device name (or none if not defined).
Note: the onRead and onWrite are from the perspective of the client. So onRead is the client calling ...