Advertisement
pleasedontcode

Beacon code

Aug 11th, 2024
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 1.48 KB | Source Code | 0 0
  1. #include <BLEDevice.h>
  2. #include <BLEUtils.h>
  3. #include <BLEServer.h>
  4.  
  5. // Define the UUID for the beacon
  6. #define BEACON_UUID "12345678-1234-5678-1234-56789abcdef0"
  7.  
  8. void setup() {
  9.   Serial.begin(115200);
  10.   BLEDevice::init(""); // Initialize BLE with an empty name
  11.  
  12.   // Create the BLE Server
  13.   BLEServer *pServer = BLEDevice::createServer();
  14.  
  15.   // Create the BLE Service (we use a custom UUID here)
  16.   BLEService *pService = pServer->createService(BEACON_UUID);
  17.  
  18.   // Create a BLE Characteristic
  19.   BLECharacteristic *pCharacteristic = pService->createCharacteristic(
  20.                                           BLEUUID("12345678-1234-5678-1234-56789abcdef1"),
  21.                                           BLECharacteristic::PROPERTY_READ
  22.                                         );
  23.  
  24.   // Set the value of the characteristic (this will be broadcasted)
  25.   pCharacteristic->setValue("ESP32 Beacon");
  26.  
  27.   // Start the service
  28.   pService->start();
  29.  
  30.   // Create the BLE Advertising object
  31.   BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  32.   pAdvertising->addServiceUUID(BEACON_UUID);
  33.   pAdvertising->setScanResponse(true);
  34.  
  35.   // Set advertising parameters
  36.   pAdvertising->setMinPreferred(0x06);  // Function to change advertising interval
  37.   pAdvertising->setMaxPreferred(0x12);  // Function to change advertising interval
  38.  
  39.   // Start advertising
  40.   pAdvertising->start();
  41.  
  42.   Serial.println("BLE Beacon is now advertising...");
  43. }
  44.  
  45. void loop() {
  46.   // Nothing to do here
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement