Instead of publishing a single article on a complex matter, we split into parts. This methodology makes the thing boring to the initial readers but in the long term, the readers can browse our website and fix themselves. In the previous two parts of this topic (here is part I and here is part II), we created the base with theory and practical so that even an unused reader does not get confused or need to web search to get our code working.
In this part of the guide, we are providing a basic code which will respond to any nearby BLE device including Samsung Smartwatch. This code has no MAC address filtering. We found this code on GitHub as Gist published by wybiral.
Upload the above code to ESP32 configured as Arduino. Whenever you’ll bring your Samsung Smartwatch (with Bluetooth ON) closer to it, the onboard blue LED of ESP32 will glow. The code is non-specific to the MAC address of Samsung Smartwatch. The code gives the idea “It Works!”. The variable “const int CUTOFF” is for adjusting the distance. -60 is a value which is quite closer.
How we will filter the devices and make ESP32 respond to only our watch? We have to inform the ESP32 about the BLE address, Service and characteristic UUID that we obtained through the nRF connect mobile application as explained in the previous guides. Part of the extra snippets will look like below :
static BLEUUID serviceUUID("paste-the-number"); //Service UUID of the watch we got through nRF connect mobile application, optional to pair
static BLEUUID charUUID("paste-the-number"); //Characteristic UUID of the watch we got through nRF connect mobile application, optional to pair
String My_BLE_Address = "paste-the-number"; //Hardware Bluetooth MAC address of the watch,
Now, when our watch will find the devices by scanning, our code should start to check if the discovered BLE address matches with the one which we entered in the code. But, ultimately we have to make the ESP32 to act as a client and then connect to the address of the BLE server.