Skip to content

Instantly share code, notes, and snippets.

@wybiral
Created September 19, 2018 12:53
Show Gist options
  • Select an option

  • Save wybiral/2a96c1d1605af7efa11b690586c4b13e to your computer and use it in GitHub Desktop.

Select an option

Save wybiral/2a96c1d1605af7efa11b690586c4b13e to your computer and use it in GitHub Desktop.
#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>
const int PIN = 2;
const int CUTOFF = -60;
void setup() {
pinMode(PIN, OUTPUT);
BLEDevice::init("");
}
void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++) {
BLEAdvertisedDevice device = results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
best = rssi;
}
}
digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
}
@wybiral

wybiral commented Sep 19, 2018

Copy link
Copy Markdown
Author

Here's a demo of the Bluetooth proximity sensor in action: https://www.youtube.com/watch?v=azT2rosEo0k

@brandonlawrence1996

Copy link
Copy Markdown

Hi. May I know how to vary the bluetooth detection distance?

@wybiral

wybiral commented Sep 23, 2018

Copy link
Copy Markdown
Author

Hi. May I know how to vary the bluetooth detection distance?

Change the value of CUTOFF. Currently it's -60 but if you make it further from zero it'll detect from further away and closer to zero will detect closer.

@qguerra

qguerra commented Oct 6, 2018

Copy link
Copy Markdown

Hi Can you tell me how setup to this up to detect a specific device ID?

@waldorffsmama

Copy link
Copy Markdown

Too bad it does not detect classic bluetooth but only BLE devices. Most phones will only emit classic bluetooth IDs

@mlyder

mlyder commented Aug 28, 2019

Copy link
Copy Markdown

This is great! thanks! Would you know how to set this up to so that I can have a beacon with an ID I specify and have the LED respond to this specific device and not just any BLE device in the area?

@ivancho523

Copy link
Copy Markdown

Hello, how can I make them detect each other? that is, in a single ESP32 that scans and can additionally behave as a client so that another ESP32 can detect it

@tnrajvignesh

Copy link
Copy Markdown

Hi,

  1. Is it possible to make the node scan and advertise at the same time, or scan and advertise in round robin? Basically I want them to detect each other.
  2. How to identify a node. I understand UUID cant be used because its application or service specific. Is there a unique identifier for every BLE chip that can be accessed inside the program as a part of scan result?

Thanks in advance.

@anandpbabu

Copy link
Copy Markdown

hello wybiral,
can you help me out, i am not a arduino coder.
i am stuck at this. In the code, the loop never stops.
I want to trigger 2 relays using BLE and also set
the activetion distance area to 5meters from the esp32 so that it
triggers only when the BLE device is 5mtrs away.when it is detected
i want it to stay on, while running the code the relay switches on and
off every 2 seconds. hope you would fix it for me.
Thanks Anand.
(the code is:)

#include <BLEDevice.h> //Header file for BLE

static BLEUUID serviceUUID("0000fff0-0000-1000-8000-00805f9b34fb");
static BLEUUID charUUID("0000fff0-0000-1000-8000-00805f9b34fb");
String My_BLE_Address = "c1:b4:70:74:fb:66";
static BLERemoteCharacteristic* pRemoteCharacteristic;

BLEScan* pBLEScan;
BLEScanResults foundDevices;

static BLEAddress *Server_BLE_Address;
String Scaned_BLE_Address;

boolean paired = false;
boolean chkConnet = false;

bool connectToserver (BLEAddress pAddress)
{

BLEClient*  pClient  = BLEDevice::createClient();
Serial.println(" - Created client");

// Connect to the BLE Server.
pClient->connect(pAddress);
Serial.println(" - Connected");
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
if (pRemoteService != nullptr)
{
  Serial.println(" - Found our service");
  return true;
}
else
return false;

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);
Serial.println("Project-4");

BLEDevice::init("");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());  
pBLEScan->setActiveScan(true); 

pinMode(12, OUTPUT);
pinMode(14, OUTPUT);
}

void loop()
{

foundDevices = pBLEScan->start(3);
Serial.println(chkConnet);
if(chkConnet == false)
{
while (foundDevices.getCount() >= 1)
{
if (Scaned_BLE_Address == My_BLE_Address && paired == false)
{
Serial.println("connecting .....");
if (connectToserver(Server_BLE_Address))
{
paired = true;
Serial.println("ON");
if(chkConnet == false)
{
digitalWrite (12,HIGH);
digitalWrite (14,HIGH);
chkConnet = true;
Serial.println("On");
}
break;
}
else
{
Serial.println("Pairing failed");
break;
}
}
break;
}
}
while (foundDevices.getCount() == 0)
{
Serial.println("
**************");
Serial.println(foundDevices.getCount());

  Serial.println("out of range");
  paired = false;
 
  if(chkConnet == true)
  {
  digitalWrite (12,LOW);
  digitalWrite (14,LOW);
  chkConnet = false;
  Serial.println("OFF");
  }
  ESP.restart();
  break;    

}
}

@bubby877

Copy link
Copy Markdown

Hi, can you tell me how setup to this up to detect a specific device ID? That is, I only want one Bluetooth device to trigger the reaction

@RensDuijsens

Copy link
Copy Markdown

How do I make this ESP device detect all phones?
For some reaon, I do not see 'most' phones.

@jasnos

jasnos commented Jan 6, 2024

Copy link
Copy Markdown

Hi, can you tell me how setup to this up to detect a specific device ID? That is, I only want one Bluetooth device to trigger the reaction

Same here...

I want to set up a scenario where a relay is triggered for 30 minutes if my iPhone or Apple Watch is detected within a custom proximity range or signal strength. This scenario should only run in the morning when I am sitting in front of my computer. The trigger will switch on an anti-depression lamp for 30 minutes.

Could you please provide me with some tips on how to achieve this? Thank you.

@StuartJames

Copy link
Copy Markdown

Hi, Can you post the contents of the Puck.js script? Thanks.

@ripred

ripred commented May 1, 2026

Copy link
Copy Markdown

This does not compile as it is written. A corrected version would be:

#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>

const int PIN = 2;
const int CUTOFF = -60;

void setup() {
  pinMode(PIN, OUTPUT);
  BLEDevice::init("");
}

void loop() {
  BLEScan *scan = BLEDevice::getScan();
  scan->setActiveScan(true);
  BLEScanResults &results = *(scan->start(1));
  int best = CUTOFF;
  for (int i = 0; i < results.getCount(); i++) {
    BLEAdvertisedDevice device = results.getDevice(i);
    int rssi = device.getRSSI();
    if (rssi > best) {
      best = rssi;
    }
  }
  digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment