In the previous part of this topic (here is part III), we provided a sample code to create a basic BLE scanner. The code makes the ESP32’s on-board LED to turn on whenever the watch comes in proximity.
In our earlier parts of this series, we talked about filtering the MAC address and practical matters around Samsung Galaxy Watch. I found that the website circuitdigest.com
worked with a similar project with a Fitness Band.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | // original version written by // https://circuitdigest.com/microcontroller-projects/esp32-ble-client-connecting-to-fitness-band-to-trigger-light #include <BLEDevice.h> //Header file for BLE static BLEUUID serviceUUID("0000d15a-0000-1000-8000-aabbccddeeff"); //Service UUID of fitnessband obtained through nRF connect application static BLEUUID charUUID("0000d15a-0000-1000-8000-aabbccddeeff"); //Characteristic UUID of fitnessband obtained through nRF connect application String My_BLE_Address = "e0:a1:07:b7:0b:95"; //Hardware Bluetooth MAC of my fitnessband, will vary for every band obtained through nRF connect application static BLERemoteCharacteristic* pRemoteCharacteristic; BLEScan* pBLEScan; //Name the scanning device as pBLEScan BLEScanResults foundDevices; static BLEAddress *Server_BLE_Address; String Scaned_BLE_Address; boolean paired = false; //boolean variable to togge light bool connectToserver (BLEAddress pAddress){ BLEClient* pClient = BLEDevice::createClient(); Serial.println(" - Created client"); // Connect to the BLE Server. pClient->connect(pAddress); Serial.println(" - Connected to fitnessband"); // Obtain a reference to the service we are after in the remote BLE server. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService != nullptr) { Serial.println(" - Found our service"); return true; } else return false; // Obtain a reference to the characteristic in the service of the remote BLE server. pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID); if (pRemoteCharacteristic != nullptr) Serial.println(" - Found our characteristic"); return true; } class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice advertisedDevice) { Serial.printf("Scan Result: %s \n", advertisedDevice.toString().c_str()); Server_BLE_Address = new BLEAddress(advertisedDevice.getAddress()); Scaned_BLE_Address = Server_BLE_Address->toString().c_str(); } }; void setup() { Serial.begin(115200); //Start serial monitor Serial.println("ESP32 BLE Server program"); //Intro message BLEDevice::init(""); pBLEScan = BLEDevice::getScan(); //create new scan pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Call the class that is defined above pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster pinMode (2,OUTPUT); //Declare the in-built LED pin as output } void loop() { foundDevices = pBLEScan->start(3); //Scan for 3 seconds to find the Fitness band while (foundDevices.getCount() >= 1) { if (Scaned_BLE_Address == My_BLE_Address && paired == false) { Serial.println("Found Device :-)... connecting to Server as client"); if (connectToserver(*Server_BLE_Address)) { paired = true; Serial.println("********************LED turned ON************************"); digitalWrite (2,HIGH); break; } else { Serial.println("Pairing failed"); break; } } if (Scaned_BLE_Address == My_BLE_Address && paired == true) { Serial.println("Our device went out of range"); paired = false; Serial.println("********************LED OOOFFFFF************************"); digitalWrite (2,LOW); ESP.restart(); break; } else { Serial.println("We have some other BLe device in range"); break; } } } |
You have to change these three lines :
---
1 2 | static BLEUUID charUUID("0000d15a-0000-1000-8000-aabbccddeeff"); //Characteristic UUID of Samsung Galaxy Smartwatch obtained through nRF connect application String My_BLE_Address = "E0:A1:07:B7:0B:95"; //Hardware Bluetooth MAC of my Samsung Galaxy Smartwatch, will vary for every watch obtained through nRF connect application |
Press the RESET button of ESP32 and open the serial monitor of Arduino IDE. You’ll get some output like below :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:1100 load:0x40078000,len:10208 load:0x40080400,len:6460 entry 0x400806a4 ESP32 BLE Server program Scan Result: Name: Galaxy Watch (E5CA) LE, Address: e0:a1:07:b7:0b:95, appearance: 192, manufacturer data: 750001000200010302 Found Device :-)... connecting to Server as client - Created client |
We have optimized the code in the next article.