Page 1 of 1

Problem with BLEDevice library.

Posted: Wed Sep 14, 2022 3:03 pm
by TheCiosiek
  1. /**
  2.  * A BLE client example that is rich in capabilities.
  3.  * There is a lot new capabilities implemented.
  4.  * author unknown
  5.  * updated by chegewara
  6.  */
  7.  
  8. #include "BLEDevice.h"
  9. #include <Arduino.h>
  10. #include <Wire.h>
  11. //#include "BLEScan.h"
  12.  
  13. // The remote service we wish to connect to.
  14. static BLEUUID serviceUUID("0492fcec-7194-11eb-9439-0242ac130002");
  15. // The characteristic of the remote service we are interested in.
  16. static BLEUUID    charUUID("0492fcec-7194-11eb-9439-0242ac130003");
  17.  
  18. static boolean doConnect = false;
  19. static boolean connected = false;
  20. static boolean doScan = false;
  21. static BLERemoteCharacteristic* pRemoteCharacteristic;
  22. static BLEAdvertisedDevice* myDevice;
  23.  
  24. static void notifyCallback(
  25.   BLERemoteCharacteristic* pBLERemoteCharacteristic,
  26.   uint8_t* pData,
  27.   size_t length,
  28.   bool isNotify) {
  29.     Serial.print("Notify callback for characteristic ");
  30.     Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  31.     Serial.print(" of data length ");
  32.     Serial.println(length);
  33.     Serial.print("data: ");
  34.     Serial.println((char*)pData);
  35. }
  36.  
  37. class MyClientCallback : public BLEClientCallbacks {
  38.   void onConnect(BLEClient* pclient) {
  39.   }
  40.  
  41.   void onDisconnect(BLEClient* pclient) {
  42.     connected = false;
  43.     doConnect = false;
  44.     Serial.println("onDisconnect");
  45.   }
  46. };
  47.  
  48. bool connectToServer() {
  49.     Serial.print("Forming a connection to ");
  50.     Serial.println(myDevice->getAddress().toString().c_str());
  51.    
  52.     BLEClient*  pClient  = BLEDevice::createClient();
  53.     Serial.println(" - Created client");
  54.  
  55.     pClient->setClientCallbacks(new MyClientCallback());
  56.    
  57.     Serial.print("Connecting");
  58.     // Connect to the remove BLE Server.
  59.     pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
  60.     Serial.println(" - Connected to server");
  61.  
  62.     // Obtain a reference to the service we are after in the remote BLE server.
  63.     BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  64.     if (pRemoteService == nullptr) {
  65.       Serial.print("Failed to find our service UUID: ");
  66.       Serial.println(serviceUUID.toString().c_str());
  67.       pClient->disconnect();
  68.       return false;
  69.     }
  70.     Serial.println(" - Found our service");
  71.  
  72.  
  73.     // Obtain a reference to the characteristic in the service of the remote BLE server.
  74.     pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  75.     if (pRemoteCharacteristic == nullptr) {
  76.       Serial.print("Failed to find our characteristic UUID: ");
  77.       Serial.println(charUUID.toString().c_str());
  78.       pClient->disconnect();
  79.       return false;
  80.     }
  81.     Serial.println(" - Found our characteristic");
  82.  
  83.     // Read the value of the characteristic.
  84.     if(pRemoteCharacteristic->canRead()) {
  85.       std::string value = pRemoteCharacteristic->readValue();
  86.       Serial.print("The characteristic value was: ");
  87.       Serial.println(value.c_str());
  88.     }
  89.  
  90.     if(pRemoteCharacteristic->canNotify())
  91.       pRemoteCharacteristic->registerForNotify(notifyCallback);
  92.  
  93.     connected = true;
  94. }
  95. /**
  96.  * Scan for BLE servers and find the first one that advertises the service we are looking for.
  97.  */
  98. class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  99.  /**
  100.    * Called for each advertising BLE server.
  101.    */
  102.   void onResult(BLEAdvertisedDevice advertisedDevice) {
  103.     BLEAdvertisedDevice* searchDevice = new BLEAdvertisedDevice(advertisedDevice);
  104.     Serial.print("BLE Advertised Device found: ");
  105.     Serial.println(advertisedDevice.toString().c_str());
  106.  
  107.     // We have found a device, let us now see if it contains the service we are looking for.
  108.     if (advertisedDevice.haveName() && String(searchDevice->getAddress().toString().c_str()).equals("58:bf:25:3a:cd:1a")) {
  109.       Serial.print("Found our device");
  110.       BLEDevice::getScan()->stop();
  111.       myDevice = new BLEAdvertisedDevice(advertisedDevice);
  112.       doConnect = true;
  113.       doScan = true;
  114.     } // Found our server
  115.   } // onResult
  116. }; // MyAdvertisedDeviceCallbacks
  117.  
  118.  
  119. void setup() {
  120.   Serial.begin(115200);
  121.   Serial.println("Starting Arduino BLE Client application...");
  122.   BLEDevice::init("");
  123.  
  124.   // Retrieve a Scanner and set the callback we want to use to be informed when we
  125.   // have detected a new device.  Specify that we want active scanning and start the
  126.   // scan to run for 5 seconds.
  127.   BLEScan* pBLEScan = BLEDevice::getScan();
  128.   pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  129.   pBLEScan->setInterval(1349);
  130.   pBLEScan->setWindow(449);
  131.   pBLEScan->setActiveScan(true);
  132.   pBLEScan->start(5, false);
  133. } // End of setup.
  134.  
  135.  
  136. // This is the Arduino main loop function.
  137. void loop() {
  138.  
  139.   // If the flag "doConnect" is true then we have scanned for and found the desired
  140.   // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  141.   // connected we set the connected flag to be true.
  142.   if (doConnect == true) {
  143.     if (connectToServer()) {
  144.       Serial.println("We are now connected to the BLE Server.");
  145.     } else {
  146.       Serial.println("We have failed to connect to the server; there is nothin more we will do.");
  147.     }
  148.     doConnect = false;
  149.   }
  150.  
  151.   // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  152.   // with the current time since boot.
  153.   if (connected) {
  154.     String newValue = "Time since boot: " + String(millis()/1000);
  155.     Serial.println("Setting new characteristic value to \"" + newValue + "\"");
  156.    
  157.     // Set the characteristic's value to be the array of bytes that is actually a string.
  158.     pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  159.   }else if(doScan){
  160.     BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  161.   }
  162.  
  163.   delay(1000); // Delay a second between loops.
  164. } // End of loop
I have this code that does not work. I get this error:

assert failed: xQueueGenericSend queue.c:832 (pxQueue->pcHead != ((void *)0) || pxQueue->u.xSemaphore.xMutexHolder == ((void *)0) || pxQueue->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle())

Core debug level:
Starting Arduino BLE Client application...
[ 734][D][BLEScan.cpp:204] start(): [] >> start(duration=5)
[ 735][D][FreeRTOS.cpp:165] take(): [] Semaphore taking: name: ScanEnd (0x3ffda614), owner: <N/A> for start
[ 739][D][FreeRTOS.cpp:174] take(): [] Semaphore taken: name: ScanEnd (0x3ffda614), owner: start
[ 748][D][BLEScan.cpp:236] start(): [] << start()
[ 752][V][FreeRTOS.cpp:70] wait(): [] >> wait: Semaphore waiting: name: ScanEnd (0x3ffda614), owner: start for start
[ 748][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 2 0x02
[ 748][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 779][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 787][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 7 0x07
[ 787][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 803][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 815][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 3 0x03
[ 815][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 828][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 837][D][BLEAdvertisedDevice.cpp:424] setRSSI(): [] - setRSSI(): rssi: -39
[ 844][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0x1
[ 844][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x01 (), length: 1, data: 1a
[ 858][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0xa
[ 858][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x0a (), length: 1, data: 0c
[ 873][D][BLEAdvertisedDevice.cpp:484] setTXPower(): [] - txPower: 12
[ 879][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0xff
[ 879][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0xff (), length: 9, data: 4c0010055b1cf810eb
[ 896][D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): [] - manufacturer data: 4c0010055b1cf810eb
BLE Advertised Device found: Name: , Address: 4a:1d:5b:f8:86:89, manufacturer data: 4c0010055b1cf810eb, txPower: 12
[ 918][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 3 0x03
[ 918][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 932][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 942][D][BLEAdvertisedDevice.cpp:424] setRSSI(): [] - setRSSI(): rssi: -41
[ 947][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0xff
[ 947][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0xff (), length: 20, data: 06000109210ab50292caf2d84c6170746f70696b
[ 965][D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): [] - manufacturer data: 06000109210ab50292caf2d84c6170746f70696b
BLE Advertised Device found: Name: , Address: 1c:e5:69:d3:80:6a, manufacturer data: 06000109210ab50292caf2d84c6170746f70696b
[ 989][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 3 0x03
[ 989][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 1004][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 1013][D][BLEAdvertisedDevice.cpp:424] setRSSI(): [] - setRSSI(): rssi: -90
[ 1019][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0x1
[ 1019][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x01 (), length: 1, data: 1a
[ 1034][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0xa
[ 1034][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x0a (), length: 1, data: 0c
[ 1049][D][BLEAdvertisedDevice.cpp:484] setTXPower(): [] - txPower: 12
[ 1057][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0xff
[ 1057][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0xff (), length: 9, data: 4c001005591c21e02f
[ 1072][D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): [] - manufacturer data: 4c001005591c21e02f
BLE Advertised Device found: Name: , Address: 67:08:5d:28:8e:1a, manufacturer data: 4c001005591c21e02f, txPower: 12
[ 1094][V][BLEUtils.cpp:1818] gapEventToString(): [] gapEventToString: Unknown event type 3 0x03
[ 1094][V][BLEUtils.cpp:1050] dumpGapEvent(): [] Received a GAP event: Unknown event type
[ 1108][V][BLEUtils.cpp:1265] dumpGapEvent(): [] *** dumpGapEvent: Logger not coded ***
[ 1116][D][BLEAdvertisedDevice.cpp:424] setRSSI(): [] - setRSSI(): rssi: -38
[ 1123][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0x1
[ 1123][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x01 (), length: 1, data: 06
[ 1138][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0x9
[ 1138][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x09 (), length: 8, data: 6d70792d66696c65
[ 1154][D][BLEAdvertisedDevice.cpp:413] setName(): [] - setName(): name: mpy-file
[ 1162][V][BLEUtils.cpp:747] advTypeToString(): [] adv data type: 0x7
[ 1162][D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): [] Type: 0x07 (), length: 16, data: 020013ac42023994eb119471ecfc9204
[ 1181][D][BLEAdvertisedDevice.cpp:453] setServiceUUID(): [] - addServiceUUID(): serviceUUID: 0492fcec-7194-11eb-9439-0242ac130002
BLE Advertised Device found: Name: mpy-file, Address: 58:bf:25:3a:cd:1a, serviceUUID: 0492fcec-7194-11eb-9439-0242ac130002
Found our device[ 1203][D][BLEScan.cpp:259] stop(): [] >> stop()
[ 1208][V][FreeRTOS.cpp:120] give(): [] Semaphore giving: name: ScanEnd (0x3ffda614), owner: start

assert failed: xQueueGenericSend queue.c:832 (pxQueue->pcHead != ((void *)0) || pxQueue->u.xSemaphore.xMutexHolder == ((void *)0) || pxQueue->u.xSemaphore.xMutexHolder == xTaskGetCurrentTaskHandle())


Backtrace:0x400832fd:0x3ffcea400x400935f1:0x3ffcea60 0x40098559:0x3ffcea80 0x40093f7a:0x3ffcebb0 0x400d9939:0x3ffcebf0 0x400d77f6:0x3ffcec50 0x400d25f9:0x3ffcec90 0x400d7f25:0x3ffcecf0 0x400d4c26:0x3ffcee00 0x400eaec5:0x3ffcee60 0x401139c9:0x3ffcee80 0x40115853:0x3ffceea0

Can someone help me please?

Re: Problem with BLEDevice library.

Posted: Sat Sep 17, 2022 10:56 am
by chegewara
To be honest, i have no idea. It crashed on queue, but i dont remember any queues in the library.
Is it really whole code? Try to update arduino/library version.