This is the final part of the series. In Part I, we talked about the basics which continued to Part II.
Part III had a working code non-specific to any particular device. Part IV had a partially working code specific to a particular device.
circuitdigest.com
‘s code (in Part IV) was not working with Samsung Galaxy Watch LTE. Samsung’s smartwatches shutdown both BLE and traditional Bluetooth when Bluetooth is kept OFF from settings and pairing not going to work. So, the option was to modify the code of Part III. Thankfully, I found that João Alves (jpralves.net
) linked to a working code by Lindermann95
on Instructables. In the final code, I only modified some of the words from Spanish to English and added English comments.
This is the final working code :
---
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 | // originally written by Lindermann95 // https://www.instructables.com/id/ESP32-BLE-Presence-Detector/ // Modified by Abhishek Ghosh, thecustomizewindows.com #include "BLEDevice.h" int LED = 2; // on-board LED at pin 2 int BUTTON = 0; static BLEAddress *pServerAddress; BLEScan* pBLEScan; BLEClient* pClient; bool deviceFound = false; bool LEDoff = false; bool BotonOff = false; String knownAddresses[] = { "e0:a1:07:b7:0b:95"}; // change the MAC unsigned long entry; 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); } class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks { void onResult(BLEAdvertisedDevice Device){ // show the MAC of other BLE devices //Serial.print("BLE Advertised Device found: "); //Serial.println(Device.toString().c_str()); pServerAddress = new BLEAddress(Device.getAddress()); bool known = false; bool Master = false; for (int i = 0; i < (sizeof(knownAddresses) / sizeof(knownAddresses[0])); i++) { if (strcmp(pServerAddress->toString().c_str(), knownAddresses[i].c_str()) == 0) known = true; } if (known) { Serial.print("Our device found!"); Serial.print("Device distance:"); Serial.println(Device.getRSSI()); // adjust the value. -85 is medium distance // -60 is closer than -85 if (Device.getRSSI() > -85) { deviceFound = true; } else { deviceFound = false; } Device.getScan()->stop(); delay(100); } } }; void setup() { Serial.begin(115200); pinMode(LED,OUTPUT); digitalWrite(LED,LOW); BLEDevice::init(""); pClient = BLEDevice::createClient(); pBLEScan = BLEDevice::getScan(); pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); pBLEScan->setActiveScan(true); Serial.println("Done"); } void Bluetooth() { Serial.println(); Serial.println("BLE Scan restarted....."); deviceFound = false; BLEScanResults scanResults = pBLEScan->start(5); if (deviceFound) { Serial.println("LED is ON now"); LEDoff = true; digitalWrite(LED,HIGH); BUTTON = 0; delay(10000); } else{ digitalWrite(LED,LOW); delay(1000); } } void loop() { Bluetooth(); } |