BLE client connect device

thuanhai0203
Posts: 1
Joined: Sun Dec 18, 2022 8:13 am

BLE client connect device

Postby thuanhai0203 » Sun Dec 18, 2022 8:24 am

This is my code
  1. #include "BLEDevice.h"
  2. //#include "BLEScan.h"
  3.  
  4. #define bleServerName "70001697"
  5. // The remote service we wish to connect to.
  6. static BLEUUID serviceUUID("6ECB2400-DC4C-40CC-A6E0-81E0DBDA54E5");
  7. // The characteristic of the remote service we are interested in.
  8. static BLEUUID    charUUID("6ECB2401-DC4C-40CC-A6E0-81E0DBDA54E5");
  9.  
  10. static boolean doConnect = false;
  11. static boolean connected = false;
  12. static boolean doScan = false;
  13. static BLERemoteCharacteristic* pRemoteCharacteristic;
  14. static BLEAdvertisedDevice* myDevice;
  15. static BLEAddress *pServerAddress;
  16.  
  17. static void notifyCallback(
  18.   BLERemoteCharacteristic* pBLERemoteCharacteristic,
  19.   uint8_t* pData,
  20.   size_t length,
  21.   bool isNotify) {
  22.     Serial.print("Notify callback for characteristic ");
  23.     Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  24.     Serial.print(" of data length ");
  25.     Serial.println(length);
  26.     Serial.print("data: ");
  27.     Serial.println((char*)pData);
  28. }
  29.  
  30. class MyClientCallback : public BLEClientCallbacks {
  31.   void onConnect(BLEClient* pclient) {
  32.   }
  33.  
  34.   void onDisconnect(BLEClient* pclient) {
  35.     connected = false;
  36.     Serial.println("onDisconnect");
  37.   }
  38. };
  39.  
  40. bool connectToServer() {
  41.     Serial.print("Forming a connection to ");
  42.     Serial.println(myDevice->getAddress().toString().c_str());
  43.    
  44.     BLEClient*  pClient  = BLEDevice::createClient();
  45.     Serial.println(" - Created client");
  46.  
  47.     pClient->setClientCallbacks(new MyClientCallback());
  48.  
  49.     // Connect to the remove BLE Server.
  50.     pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
  51.     Serial.println(" - Connected to server");
  52.  
  53.     // Obtain a reference to the service we are after in the remote BLE server.
  54.     BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  55.     if (pRemoteService == nullptr) {
  56.       Serial.print("Failed to find our service UUID: ");
  57.       Serial.println(serviceUUID.toString().c_str());
  58.       pClient->disconnect();
  59.       return false;
  60.     }
  61.     Serial.println(" - Found our service");
  62.  
  63.  
  64.     // Obtain a reference to the characteristic in the service of the remote BLE server.
  65.     pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  66.     if (pRemoteCharacteristic == nullptr) {
  67.       Serial.print("Failed to find our characteristic UUID: ");
  68.       Serial.println(charUUID.toString().c_str());
  69.       pClient->disconnect();
  70.       return false;
  71.     }
  72.     Serial.println(" - Found our characteristic");
  73.  
  74.     // Read the value of the characteristic.
  75.     if(pRemoteCharacteristic->canRead()) {
  76.       std::string value = pRemoteCharacteristic->readValue();
  77.       Serial.print("The characteristic value was: ");
  78.       Serial.println(value.c_str());
  79.     }
  80.  
  81.     if(pRemoteCharacteristic->canNotify())
  82.       pRemoteCharacteristic->registerForNotify(notifyCallback);
  83.  
  84.     connected = true;
  85. }
  86. /**
  87.  * Scan for BLE servers and find the first one that advertises the service we are looking for.
  88.  */
  89. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  90.   void onResult(BLEAdvertisedDevice advertisedDevice) {
  91.     Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
  92.     if (advertisedDevice.getName() == bleServerName) { //Check if the name of the advertiser matches
  93.       advertisedDevice.getScan()->stop(); //Scan can be stopped, we found what we are looking for
  94.       pServerAddress = new BLEAddress(advertisedDevice.getAddress()); //Address of advertiser is the one we need
  95.       doConnect = true; //Set indicator, stating that we are ready to connect
  96.       Serial.println("Device found. Connecting!");
  97.     }
  98.     else doScan = true;
  99.   }
  100. };
  101.  
  102.  
  103. void setup() {
  104.   Serial.begin(115200);
  105.   Serial.println("Starting Arduino BLE Client application...");
  106.   BLEDevice::init("");
  107.  
  108.   // Retrieve a Scanner and set the callback we want to use to be informed when we
  109.   // have detected a new device.  Specify that we want active scanning and start the
  110.   // scan to run for 5 seconds.
  111.   BLEScan* pBLEScan = BLEDevice::getScan();
  112.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  113.   pBLEScan->setInterval(1349);
  114.   pBLEScan->setWindow(449);
  115.   pBLEScan->setActiveScan(true);
  116.   pBLEScan->start(2, false);
  117. } // End of setup.
  118.  
  119.  
  120. // This is the Arduino main loop function.
  121. void loop() {
  122.  
  123.   // If the flag "doConnect" is true then we have scanned for and found the desired
  124.   // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  125.   // connected we set the connected flag to be true.
  126.   if (doConnect == true) {
  127.     if (connectToServer()) {
  128.       Serial.println("We are now connected to the BLE Server.");
  129.     } else {
  130.       Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  131.     }
  132.     doConnect = false;
  133.   }
  134.  
  135.   // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  136.   // with the current time since boot.
  137.  
  138.   if(doScan)
  139.   {
  140.     BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  141.   }
  142.  
  143.   delay(2000); // Delay a second between loops.
  144. } // End of loop
But esp32 constant resets

Code: Select all

15:08:52.749 -> Forming a connection to Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
15:08:52.749 -> Core 1 register dump:
15:08:52.749 -> PC      : 0x40091c74  PS      : 0x00060530  A0      : 0x800d2270  A1      : 0x3ffc87f0  
15:08:52.796 -> A2      : 0x3ffc880a  A3      : 0x00000007  A4      : 0x00000006  A5      : 0x3ffc880a  
15:08:52.796 -> A6      : 0x00060320  A7      : 0x00000000  A8      : 0x800da201  A9      : 0x3ffc87d0  
15:08:52.796 -> A10     : 0x00000018  A11     : 0x3f4001c7  A12     : 0x00000018  A13     : 0x0000ff00  
15:08:52.796 -> A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
15:08:52.796 -> EXCVADDR: 0x00000007  LBEG    : 0x40092ead  LEND    : 0x40092ebd  LCOUNT  : 0xfffffff9  
15:08:52.796 -> 
15:08:52.796 -> ELF file SHA256: 0000000000000000
15:08:52.843 -> 
15:08:52.843 -> Backtrace: 0x40091c74:0x3ffc87f0 0x400d226d:0x3ffc8800 0x400d1a62:0x3ffc8830 0x400d1d66:0x3ffc8880 0x400da7e4:0x3ffc88c0 0x40094ce2:0x3ffc88e0
15:08:52.843 -> 
15:08:52.843 -> Rebooting...

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: BLE client connect device

Postby ESP_Sprite » Mon Dec 19, 2022 2:04 am

Can you decode that backtrace? It should lead you to where the crash happens.

Who is online

Users browsing this forum: Michaelboeding, ok-home and 97 guests