Page 1 of 1

BLE connection status

Posted: Thu Jan 03, 2019 4:40 pm
by wuzipu
Hello everyone,

I'm currently worken on a project where I let two ESP32 communicate with each other via BLE and it is working pretty fine. There is only one problem:
When the ESP32 running as BLE client disconnects form the ESP32 running as server (because it is too far away or whatever) the client crashes and reboots because it tries reading values from the server which it isn't connected to anymore. So I was wondering if anyone could tell me how I can request if the client is still connected to the server.

Thank you for your help!

The code:
  1. #include <BLEDevice.h>
  2. #include <Strings.h>
  3.  
  4. std::string rxValue;
  5. String rxValueArduino ="";
  6. static BLEUUID SERVICE_UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
  7. static BLEUUID    CHARACTERISTIC_UUID_RX("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
  8. static BLEUUID    CHARACTERISTIC_UUID_TX("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
  9. static BLEAddress *pServerAddress;
  10. static boolean doConnect = false;
  11. static boolean deviceConnected = false;
  12. static BLERemoteCharacteristic* pRemoteCharacteristic;
  13. BLERemoteService* pRemoteService;
  14.  
  15. bool connectToServer(BLEAddress pAddress)
  16. {
  17.     Serial.print("Forming a connection to ");
  18.     Serial.println(pAddress.toString().c_str());
  19.     BLEClient*  pClient  = BLEDevice::createClient();
  20.     pClient->connect(pAddress);
  21.     Serial.println(" - Connected to server");
  22.     pRemoteService = pClient->getService(SERVICE_UUID);
  23.     if (pRemoteService == nullptr)
  24.     {
  25.         Serial.print("Failed to find our service UUID: ");
  26.         Serial.println(SERVICE_UUID.toString().c_str());
  27.         return false;
  28.     }
  29.     Serial.println(" - Found our service");
  30. }
  31.  
  32. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks          
  33. {
  34.     void onResult(BLEAdvertisedDevice advertisedDevice)
  35.     {
  36.         Serial.print("BLE Advertised Device found: ");
  37.         Serial.println(advertisedDevice.toString().c_str());
  38.         if (advertisedDevice.haveServiceUUID() && advertisedDevice.getServiceUUID().equals(SERVICE_UUID))      
  39.         {
  40.             Serial.print("Found our device!  address: ");
  41.             advertisedDevice.getScan()->stop();
  42.             pServerAddress = new BLEAddress(advertisedDevice.getAddress());        
  43.             doConnect = true;
  44.         }
  45.     }
  46. };
  47.  
  48. void setup()
  49. {
  50.     BLEDevice::init("ESP32");    
  51.     BLEScan* pBLEScan = BLEDevice::getScan();
  52.     pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());  
  53.     pBLEScan->setActiveScan(true);    
  54.     pBLEScan->start(10);              
  55. }
  56.  
  57. void loop()
  58. {
  59.    if (doConnect == true)      
  60.    {
  61.        if (connectToServer(*pServerAddress))  
  62.        {
  63.             Serial.println("We are now connected to the BLE Server.");
  64.             deviceConnected = true;
  65.        }
  66.        else
  67.        {
  68.             Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  69.        }
  70.             doConnect = false;
  71.     }
  72.     if (deviceConnected)
  73.     {
  74.         pRemoteCharacteristic = pRemoteService->getCharacteristic(CHARACTERISTIC_UUID_RX);      
  75.         rxValue = pRemoteCharacteristic->readValue();          
  76.         rxValueArduino ="";
  77.         for (int i = 0; i < rxValue.length(); i++)      
  78.         {
  79.             rxValueArduino += rxValue[i];
  80.         }
  81.     }
  82. }
  83.  

Re: BLE connection status

Posted: Thu Jan 03, 2019 6:03 pm
by wuzipu
I added following lines of code:
  1. bool status;
  2. BLEClient* getStatus;
  3.  
  4. status=getStatus->isConnected();                
  5. Serial.println(status);
This leads to a Guru Meditation error but it should actually give me the connection status, hmmm

Re: BLE connection status

Posted: Thu Jan 03, 2019 11:35 pm
by chegewara
Hi,
did you initialize getStatus before you call isConnected(), and is it pointing to the same BLEClient that *pClient.
From this snippet i am assuming you are calling isConnected() on NULL object.

Re: BLE connection status

Posted: Fri Jan 04, 2019 10:42 am
by wuzipu
Hi,
Thank you isConnected() is now being called on pClient and it is working :)

Re: BLE connection status

Posted: Thu Mar 14, 2019 2:48 pm
by Dykkeren
Hey, wuzipu


Would it be possible for you too send me your code with ESP32 to ESP32, communicating via bluetooth? :) I would be so glad!

Mail:
seb2012@live.dk


Thanks!

Re: BLE connection status

Posted: Tue Mar 19, 2019 7:32 am
by tommyshelby
The BLE connection issue is also happening with me as well and the connection is not getting established and the main thing is that I am not being able to access the module after doing the implementation of that.