- /**
- * A BLE client example that is rich in capabilities.
- * There is a lot new capabilities implemented.
- * author unknown
- * updated by chegewara
- */
- #include "BLEDevice.h"
- #include <Arduino.h>
- #include <Wire.h>
- //#include "BLEScan.h"
- // The remote service we wish to connect to.
- static BLEUUID serviceUUID("0492fcec-7194-11eb-9439-0242ac130002");
- // The characteristic of the remote service we are interested in.
- static BLEUUID charUUID("0492fcec-7194-11eb-9439-0242ac130003");
- static boolean doConnect = false;
- static boolean connected = false;
- static boolean doScan = false;
- static BLERemoteCharacteristic* pRemoteCharacteristic;
- static BLEAdvertisedDevice* myDevice;
- static void notifyCallback(
- BLERemoteCharacteristic* pBLERemoteCharacteristic,
- uint8_t* pData,
- size_t length,
- bool isNotify) {
- Serial.print("Notify callback for characteristic ");
- Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
- Serial.print(" of data length ");
- Serial.println(length);
- Serial.print("data: ");
- Serial.println((char*)pData);
- }
- class MyClientCallback : public BLEClientCallbacks {
- void onConnect(BLEClient* pclient) {
- }
- void onDisconnect(BLEClient* pclient) {
- connected = false;
- doConnect = false;
- Serial.println("onDisconnect");
- }
- };
- bool connectToServer() {
- Serial.print("Forming a connection to ");
- Serial.println(myDevice->getAddress().toString().c_str());
- BLEClient* pClient = BLEDevice::createClient();
- Serial.println(" - Created client");
- pClient->setClientCallbacks(new MyClientCallback());
- Serial.print("Connecting");
- // Connect to the remove BLE Server.
- pClient->connect(myDevice); // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
- Serial.println(" - Connected to server");
- // Obtain a reference to the service we are after in the remote BLE server.
- BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
- if (pRemoteService == nullptr) {
- Serial.print("Failed to find our service UUID: ");
- Serial.println(serviceUUID.toString().c_str());
- pClient->disconnect();
- return false;
- }
- Serial.println(" - Found our service");
- // Obtain a reference to the characteristic in the service of the remote BLE server.
- pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
- if (pRemoteCharacteristic == nullptr) {
- Serial.print("Failed to find our characteristic UUID: ");
- Serial.println(charUUID.toString().c_str());
- pClient->disconnect();
- return false;
- }
- Serial.println(" - Found our characteristic");
- // Read the value of the characteristic.
- if(pRemoteCharacteristic->canRead()) {
- std::string value = pRemoteCharacteristic->readValue();
- Serial.print("The characteristic value was: ");
- Serial.println(value.c_str());
- }
- if(pRemoteCharacteristic->canNotify())
- pRemoteCharacteristic->registerForNotify(notifyCallback);
- connected = true;
- }
- /**
- * Scan for BLE servers and find the first one that advertises the service we are looking for.
- */
- class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
- /**
- * Called for each advertising BLE server.
- */
- void onResult(BLEAdvertisedDevice advertisedDevice) {
- BLEAdvertisedDevice* searchDevice = new BLEAdvertisedDevice(advertisedDevice);
- Serial.print("BLE Advertised Device found: ");
- Serial.println(advertisedDevice.toString().c_str());
- // We have found a device, let us now see if it contains the service we are looking for.
- if (advertisedDevice.haveName() && String(searchDevice->getAddress().toString().c_str()).equals("58:bf:25:3a:cd:1a")) {
- Serial.print("Found our device");
- BLEDevice::getScan()->stop();
- myDevice = new BLEAdvertisedDevice(advertisedDevice);
- doConnect = true;
- doScan = true;
- } // Found our server
- } // onResult
- }; // MyAdvertisedDeviceCallbacks
- void setup() {
- Serial.begin(115200);
- Serial.println("Starting Arduino BLE Client application...");
- BLEDevice::init("");
- // Retrieve a Scanner and set the callback we want to use to be informed when we
- // have detected a new device. Specify that we want active scanning and start the
- // scan to run for 5 seconds.
- BLEScan* pBLEScan = BLEDevice::getScan();
- pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
- pBLEScan->setInterval(1349);
- pBLEScan->setWindow(449);
- pBLEScan->setActiveScan(true);
- pBLEScan->start(5, false);
- } // End of setup.
- // This is the Arduino main loop function.
- void loop() {
- // If the flag "doConnect" is true then we have scanned for and found the desired
- // BLE Server with which we wish to connect. Now we connect to it. Once we are
- // connected we set the connected flag to be true.
- if (doConnect == true) {
- if (connectToServer()) {
- Serial.println("We are now connected to the BLE Server.");
- } else {
- Serial.println("We have failed to connect to the server; there is nothin more we will do.");
- }
- doConnect = false;
- }
- // If we are connected to a peer BLE Server, update the characteristic each time we are reached
- // with the current time since boot.
- if (connected) {
- String newValue = "Time since boot: " + String(millis()/1000);
- Serial.println("Setting new characteristic value to \"" + newValue + "\"");
- // Set the characteristic's value to be the array of bytes that is actually a string.
- pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
- }else if(doScan){
- BLEDevice::getScan()->start(0); // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
- }
- delay(1000); // Delay a second between loops.
- } // End of loop
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?