BLE client connect device
Posted: Sun Dec 18, 2022 8:24 am
This is my code
- #include "BLEDevice.h"
- //#include "BLEScan.h"
- #define bleServerName "70001697"
- // The remote service we wish to connect to.
- static BLEUUID serviceUUID("6ECB2400-DC4C-40CC-A6E0-81E0DBDA54E5");
- // The characteristic of the remote service we are interested in.
- static BLEUUID charUUID("6ECB2401-DC4C-40CC-A6E0-81E0DBDA54E5");
- static boolean doConnect = false;
- static boolean connected = false;
- static boolean doScan = false;
- static BLERemoteCharacteristic* pRemoteCharacteristic;
- static BLEAdvertisedDevice* myDevice;
- static BLEAddress *pServerAddress;
- 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;
- 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());
- // 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 {
- void onResult(BLEAdvertisedDevice advertisedDevice) {
- Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
- if (advertisedDevice.getName() == bleServerName) { //Check if the name of the advertiser matches
- advertisedDevice.getScan()->stop(); //Scan can be stopped, we found what we are looking for
- pServerAddress = new BLEAddress(advertisedDevice.getAddress()); //Address of advertiser is the one we need
- doConnect = true; //Set indicator, stating that we are ready to connect
- Serial.println("Device found. Connecting!");
- }
- else doScan = true;
- }
- };
- 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(2, 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(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(2000); // Delay a second between loops.
- } // 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...