BLE unique identifier.
Posted: Thu Oct 26, 2023 2:05 pm
Good day, I am currently using my ESP32 as a Bluetooth server device which is retreiving data from my phone via an app I designed (Using arduino IDE and code). My question is, is it possible to gain the MAC address or any unique identifier of my phone over Bluetooth without having to send specific pin codes? Basically I would like my esp32 to ping the device for it's unique identified and use it to turn on a green or red light. The current code I have is only for retreiving pins sent from an app over BLE. Help would be greatly appreciated!
This is my current code:
This is my current code:
Code: Select all
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleNotify.cpp
Ported to Arduino ESP32 by Evandro Copercini
updated by chegewara and MoThunderz
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <WiFi.h>
#include <HTTPClient.h>
const char *ssid = "L"; // Your Wi-Fi SSID
const char *password = "Leoleoleo12345"; // Your Wi-Fi password
const char *url = "https://databasefyp.000webhostapp.com/insert.php";
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
BLECharacteristic* pCharacteristic_2 = NULL;
BLECharacteristic* pCharacteristic_3 = NULL;
BLECharacteristic* pCharacteristic_4 = NULL;
BLECharacteristic* pCharacteristic_5 = NULL;
BLEDescriptor *pDescr;
BLE2902 *pBLE2902;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHAR1_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define CHAR2_UUID "e3223119-9445-4e96-a4a1-85358c4046a2"
#define CHAR3_UUID "e3223119-9445-4e96-a4a1-85358c4046a3"
#define CHAR4_UUID "e3223119-9445-4e96-a4a1-85358c4046a4"
#define CHAR5_UUID "e3223119-9445-4e96-a4a1-85358c4046a5"
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
class CharacteristicCallBack_1: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pChar) override {
std::string pChar2_value_stdstr = pChar->getValue();
String pChar2_value_string = String(pChar2_value_stdstr.c_str());
int pChar2_value_int = pChar2_value_string.toInt();
Serial.println("pChar2: " + String(pChar2_value_int));
}
};
class CharacteristicCallBack_2: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pChar) override {
std::string pChar3_value_stdstr = pChar->getValue();
String pChar3_value_string = String(pChar3_value_stdstr.c_str());
int pChar3_value_int = pChar3_value_string.toInt();
Serial.println("pChar3: " + String(pChar3_value_int));
}
};
class CharacteristicCallBack_3: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pChar) override {
std::string pChar4_value_stdstr = pChar->getValue();
String pChar4_value_string = String(pChar4_value_stdstr.c_str());
int pChar4_value_int = pChar4_value_string.toInt();
Serial.println("pChar4: " + String(pChar4_value_int));
}
};
class CharacteristicCallBack_4: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pChar) override {
std::string pChar5_value_stdstr = pChar->getValue();
String pChar5_value_string = String(pChar5_value_stdstr.c_str());
int pChar5_value_int = pChar5_value_string.toInt();
Serial.println("pChar5: " + String(pChar5_value_int));
}
};
void makeHttpRequest(const char *url) {
HTTPClient http;
// Send an HTTP GET request to the specified URL
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.print("HTTP Response Code: ");
Serial.println(httpResponseCode);
Serial.println("Response: " + response);
} else {
Serial.print("Error on HTTP request. Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init("ESP32");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHAR1_UUID,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic_2 = pService->createCharacteristic(
CHAR2_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic_3 = pService->createCharacteristic(
CHAR3_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic_4 = pService->createCharacteristic(
CHAR4_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic_5 = pService->createCharacteristic(
CHAR5_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// Create a BLE Descriptor
pDescr = new BLEDescriptor((uint16_t)0x2901);
pDescr->setValue("A very interesting variable");
pCharacteristic->addDescriptor(pDescr);
pBLE2902 = new BLE2902();
pBLE2902->setNotifications(true);
pCharacteristic->addDescriptor(pBLE2902);
// Add all Descriptors here
pCharacteristic_2->addDescriptor(new BLE2902());
pCharacteristic_2->setCallbacks(new CharacteristicCallBack_1());
pCharacteristic_3->addDescriptor(new BLE2902());
pCharacteristic_3->setCallbacks(new CharacteristicCallBack_2());
// After defining the desriptors, set the callback functions
pCharacteristic_4->addDescriptor(new BLE2902());
pCharacteristic_4->setCallbacks(new CharacteristicCallBack_3());
pCharacteristic_5->addDescriptor(new BLE2902());
pCharacteristic_5->setCallbacks(new CharacteristicCallBack_4());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop() {
// notify changed value
if (deviceConnected) {
pCharacteristic->setValue(value);
pCharacteristic->notify();
value++;
delay(1000);
}
// disconnecting
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}