Cannot connect to non-advertise esp32 BLE server

Llamachine
Posts: 2
Joined: Wed Mar 24, 2021 10:04 pm

Cannot connect to non-advertise esp32 BLE server

Postby Llamachine » Wed Mar 24, 2021 10:25 pm

I have built an android app hooked up to my esp32 ble server settings. When the ESP32 is advertising, I can connect/pair/and then issue commands via android fine. However, if I close the android app and then reopen it, select the ESP32 from my paired list in the app to connect to it, the ESP32 will not register anything, not even in verbose. Instead I have to put the ESP32 back into advertising mode to initiate the connection again even though the Android app/esp32 are paired. I thought BLE servers could be connected to without having them advertise all the time if you already have saved/paired device info on the app side.

Is there a way, with known connection info, to connect to a non-advertising ESP32 over BLE?

My arduino code.
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5.  
  6. //UUIDs
  7. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
  8. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  9. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  10.  
  11. int currentCommand = 0;
  12. int newCommand = 0;
  13.  
  14. /*====== BLE VARIABLES ======*/
  15. bool deviceConnected = false;
  16. const char* DEVICE_NAME = "ESP32 TEST";
  17. BLECharacteristic *pCharacteristic;
  18.  
  19. /*====== Incoming Message Handler ======*/
  20. void ParseMessage(std::string receiveValue)
  21. {
  22.   int newCommand = (int)atoi(receiveValue.c_str());
  23.   if (newCommand != currentCommand)
  24.   {
  25.     currentCommand = newCommand;
  26.   }
  27.   Serial.println("New Command");Serial.println(newCommand);
  28.  
  29.   pCharacteristic->setValue(receiveValue);
  30.   pCharacteristic->notify();
  31.   delay(10);
  32. }
  33.  
  34.  
  35. /* ====== On Connect/Disconnect Callbacks ======*/
  36. class MyServerCallbacks: public BLEServerCallbacks
  37. {
  38.   void onConnect(BLEServer* pServer)
  39.   {
  40.     deviceConnected = true;
  41.     Serial.println("A device has connected");
  42.   };
  43.  
  44.   void onDisconnect(BLEServer* pServer)
  45.   {
  46.     deviceConnected = false;
  47.     Serial.println("A device has disconnected");
  48.   }
  49. };
  50.  
  51. /*====== NEW BLE DATA CALLBACK ======*/
  52. class MyCallbacks: public BLECharacteristicCallbacks
  53. {
  54.   //On receive message call back
  55.   void onWrite(BLECharacteristic *pCharacteristic)
  56.   {
  57.     std::string receiveValue = pCharacteristic->getValue();
  58.     if (receiveValue.length() > 0)
  59.     {
  60.       ParseMessage(receiveValue);
  61.       delay(10);
  62.     }
  63.   }
  64. };
  65.  
  66.  
  67. void setup() {
  68.   Serial.begin(115200); Serial.println("Serial Started");
  69.  
  70.   /*====== Setup the server ======*/
  71.   BLEDevice::init(DEVICE_NAME);
  72.   BLEServer *pServer = BLEDevice::createServer();
  73.   pServer->setCallbacks(new MyServerCallbacks());
  74.  
  75.   //Create a service to advertise
  76.   BLEService *pService = pServer->createService(SERVICE_UUID);
  77.  
  78.  
  79.   /*======= Create TX Characteristic ======*/
  80.   pCharacteristic = pService -> createCharacteristic
  81.   (
  82.     CHARACTERISTIC_UUID_TX,
  83.     BLECharacteristic::PROPERTY_NOTIFY
  84.   );
  85.   // Add descriptor
  86.    pCharacteristic -> addDescriptor(new BLE2902());
  87.  
  88.  
  89.   /*======= Create RX Characteristic ======*/
  90.   BLECharacteristic* pCharacteristic = pService -> createCharacteristic
  91.   (
  92.     CHARACTERISTIC_UUID_RX,
  93.     BLECharacteristic::PROPERTY_WRITE
  94.   );
  95.  
  96.   pCharacteristic -> setCallbacks(new MyCallbacks());
  97.   Serial.println("BLE Server ready");
  98.  
  99.   /*====== Start Server ======*/
  100.   pService->start();
  101.   Serial.println("BLE Server Started");
  102.  
  103.   //hacky advertising window
  104.   int i = 0; Serial.println("Start Advertising");
  105.   while(i < 100 && deviceConnected == false)
  106.   {
  107.     digitalWrite(ONBOARD_LED, HIGH);
  108.     delay(50);
  109.     digitalWrite(ONBOARD_LED, LOW);
  110.     delay(50);
  111.     i++;
  112.   }
  113. }
  114.  
  115. void loop() {
  116.  
  117. }

Llamachine
Posts: 2
Joined: Wed Mar 24, 2021 10:04 pm

Re: Cannot connect to non-advertise esp32 BLE server

Postby Llamachine » Thu Mar 25, 2021 2:15 am

A less hacked out copy of my arduino code. I've seen similar posts like mine around the internet on the subject of connect to paired without the server advertising but they by and large get no response. :-\
  1. #include <BLEDevice.h>
  2. #include <BLEServer.h>
  3. #include <BLEUtils.h>
  4. #include <BLE2902.h>
  5.  
  6. //Service and RX/TX characteristics for UART
  7. #define SERVICE_UUID           "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
  8. #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  9. #define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  10.  
  11. #define ONBOARD_LED 2
  12.  
  13. int currentCommand = 0; //Current  command
  14. int newCommand = 0; //compare to current command
  15.  
  16.  
  17. /*====== BLE VARIABLES ======*/
  18. bool deviceConnected = false;
  19. const char* DEVICE_NAME = "ESP32 BLE Test";
  20.  
  21. BLECharacteristic *pCharacteristic;
  22.  
  23.  
  24. /*====== Incoming Message Handler ======*/
  25. void ParseMessage(std::string receiveValue)
  26. {
  27.   int newCommand = (int)atoi(receiveValue.c_str());
  28.   if (newCommand != currentCommand)
  29.   {
  30.     currentCommand = newCommand;  
  31.     Serial.println("New Command");Serial.println(newCommand);
  32.   }
  33.   pCharacteristic->setValue(receiveValue);
  34.   pCharacteristic->notify();
  35.   delay(10);
  36. }
  37.  
  38.  
  39. /* ====== On Connect/Disconnect Callbacks ======*/
  40. class MyServerCallbacks: public BLEServerCallbacks
  41. {
  42.   void onConnect(BLEServer* pServer)
  43.   {
  44.     deviceConnected = true;
  45.     uint16_t conn_id =  pServer->getConnId();
  46.     Serial.print("Conn ID: "); Serial.println(conn_id);
  47.     Serial.println("A device has connected");
  48.   };
  49.  
  50.   void onDisconnect(BLEServer* pServer)
  51.   {
  52.     deviceConnected = false;
  53.     Serial.println("A device has disconnected");
  54.   }
  55. };
  56.  
  57. /*====== NEW BLE DATA CALLBACK ======*/
  58. class MyCallbacks: public BLECharacteristicCallbacks
  59. {
  60.   //On receive message call back
  61.   void onWrite(BLECharacteristic *pCharacteristic)
  62.   {
  63.     std::string receiveValue = pCharacteristic->getValue();
  64.     if (receiveValue.length() > 0)
  65.     {
  66.       ParseMessage(receiveValue);
  67.       delay(10);
  68.     }
  69.   }
  70. };
  71.  
  72.  
  73. void setup()
  74. {
  75.   Serial.begin(115200); Serial.println("Serial Started");
  76.   pinMode(26, INPUT_PULLUP);
  77.  
  78.  
  79.   pinMode(ONBOARD_LED, OUTPUT);
  80.   digitalWrite(ONBOARD_LED, LOW);
  81.  
  82.   /*====== Setup the server ======*/
  83.   BLEDevice::init(DEVICE_NAME);
  84.   BLEServer *pServer = BLEDevice::createServer();
  85.   pServer->setCallbacks(new MyServerCallbacks());
  86.  
  87.   //Create a service to advertise
  88.   BLEService *pService = pServer->createService(SERVICE_UUID);
  89.  
  90.  
  91.   /*======= Create TX Characteristic ======*/
  92.   pCharacteristic = pService -> createCharacteristic
  93.   (
  94.     CHARACTERISTIC_UUID_TX,
  95.     BLECharacteristic::PROPERTY_NOTIFY
  96.   );
  97.   // Add descriptor
  98.    pCharacteristic -> addDescriptor(new BLE2902());
  99.  
  100.  
  101.   /*======= Create RX Characteristic ======*/
  102.   BLECharacteristic* pCharacteristic = pService -> createCharacteristic
  103.   (
  104.     CHARACTERISTIC_UUID_RX,
  105.     BLECharacteristic::PROPERTY_WRITE
  106.   );
  107.   pCharacteristic -> setCallbacks(new MyCallbacks());
  108.   Serial.println("BLE Server ready");
  109.  
  110.   /*====== Start Server ======*/
  111.   pService->start();
  112.   pServer->getAdvertising()->start();
  113.   Serial.println("BLE Server Started");
  114.  
  115.   //hacky advertising window.
  116.   int i = 0; Serial.println("Start Advertising");
  117.   while(i < 100 && deviceConnected == false)
  118.   {
  119.     digitalWrite(ONBOARD_LED, HIGH);
  120.     delay(50);
  121.     digitalWrite(ONBOARD_LED, LOW);
  122.     delay(50);
  123.     i++;
  124.   }
  125.   pServer->getAdvertising()->stop(); Serial.println("Advertising stopped");
  126.   pCharacteristic->setValue("0");
  127. }
  128.  
  129.  
  130. void loop()
  131. {
  132. }

Who is online

Users browsing this forum: No registered users and 89 guests