iPhone Bluetooth Presence detection help please

Posts: 1
Joined: Sat Nov 21, 2020 10:26 pm

iPhone Bluetooth Presence detection help please

Postby s200bym » Sat Nov 21, 2020 10:32 pm

Hi all,

I am trying to get my ESP32 to detect my iPhone via Bluetooth to set a GPIO pin high.

I can get this to work on my Raspberry Pi Zero W with Python fine with the below code:

Code: Select all


import bluetooth
import time
import RPi.GPIO as GPIO

RELAY = 23

while True:
    print "Checking " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
    result = bluetooth.lookup_name('MY iPHONE MAC', timeout=5)
    if (result != None):
        print "User present"
        print "User out of range"

When I load the below code to the ESP32 board it doesn't work. Does Python do more than Arduino C++ in regards to Bluetooth detection? I know the phone gives out random mac addresses but why does the Pi work when I punch in the phones static mac address but the ESP32 doesn't?

Code: Select all

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
String knownBLEAddresses[] = {"MY iPHONE MAC"};
bool device_found;
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      for (int i = 0; i < (sizeof(knownBLEAddresses) / sizeof(knownBLEAddresses[0])); i++)
        //Uncomment to Enable Debug Information
        if (strcmp(advertisedDevice.getAddress().toString().c_str(), knownBLEAddresses[i].c_str()) == 0)
          device_found = true;
          device_found = false;
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
void setup() {
  Serial.begin(115200); //Enable UART on ESP32
  Serial.println("Scanning..."); // Print Scanning
  pinMode(LED_BUILTIN, OUTPUT); //make BUILTIN_LED pin as output
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Init Callback Function
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100); // set Scan interval
  pBLEScan->setWindow(99);  // less or equal setInterval value
void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  for (int i = 0; i < foundDevices.getCount(); i++)
    BLEAdvertisedDevice device = foundDevices.getDevice(i);
    int rssi = device.getRSSI();
    Serial.print("RSSI: ");
    if (rssi > RSSI_THRESHOLD && device_found == true)
      digitalWrite(LED_BUILTIN, HIGH);
      digitalWrite(LED_BUILTIN, LOW);
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
I want to use the ESP32 as the Linux loading times of the Pi is a problem.

Many Thanks,

Who is online

Users browsing this forum: No registered users and 24 guests