ESP32 BLE 唯一标识符

ESP32 BLE unique identifier

提问人:Bottle 提问时间:10/26/2023 更新时间:10/26/2023 访问量:40

问:

美好的一天,我目前正在使用我的 ESP32 作为蓝牙服务器设备,它正在通过我设计的应用程序从我的手机中检索数据。我的问题是,是否可以通过蓝牙获取手机的MAC地址或任何唯一标识符,而无需发送特定的PIN码?基本上,我希望我的 esp32 对设备进行 ping 操作,因为它是唯一标识的,并使用它来打开绿灯或红灯。我拥有的当前代码仅用于检索通过 BLE 从应用程序发送的引脚。帮助将不胜感激!

这是我当前的代码:

/*
  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;
    }
}
macOS 安全性 蓝牙低功耗 ESP32 标识符

评论

0赞 Rob Napier 10/26/2023
你已经标记了这个,但提到“我的手机”。这是 iOS 问题吗?人造人?你描述的设计似乎是 ESP32 是外围设备,手机是中心。目前尚不清楚您在这种情况下所说的“我的 esp32 对设备执行 ping 操作”是什么意思。如果手机连接到 ESP32,它可以在 ESP32 的特征中写入标识符来识别自己。如果你指的是另一种关系,从你的问题或你的代码中并不清楚。您是否正在配对设备?(这就是你所说的PIN码吗?macos

答: 暂无答案