最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arduino - Unable to set BLE Services and Characterstics - Stack Overflow

programmeradmin2浏览0评论

Hardware: ESP32 S3 XIAO Sense. I am establishing a BLE server on ESP device. However, whatever UUIDs I may define for my services and characteristics for some reason they don't reflect and a constant UUID is placed for them.

I tried to make more services and characteristics but only 1 of each is made.

Here's the code for ESP32:

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLECharacteristic.h>
#include <BLE2902.h>

// Define UUIDs for the service and characteristics
#define SERVICE_UUID        "12345678-1234-1234-1234-1234567890ab"
#define CHARACTERISTIC_1_UUID "12345678-1234-1234-1234-1234567890ac"
#define CHARACTERISTIC_2_UUID "12345678-1234-1234-1234-1234567890ab"

BLECharacteristic *pCharacteristic1;
BLECharacteristic *pCharacteristic2;

// BLE Server callbacks
class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    Serial.println("Client Connected");
  }

  void onDisconnect(BLEServer* pServer) {
    Serial.println("Client Disconnected");
  }
};

void setup() {
  Serial.begin(115200);

  // Initialize BLE
  BLEDevice::init("ESP32_S3_Xiao_Sense_BLE_Server");
  
  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  // Create BLE Characteristics
  pCharacteristic1 = pService->createCharacteristic(
                      CHARACTERISTIC_1_UUID,
                      BLECharacteristic::PROPERTY_READ |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pCharacteristic1->addDescriptor(new BLE2902());

  pCharacteristic2 = pService->createCharacteristic(
                      CHARACTERISTIC_2_UUID,
                      BLECharacteristic::PROPERTY_READ |
                      BLECharacteristic::PROPERTY_NOTIFY
                    );
  pCharacteristic2->addDescriptor(new BLE2902());

  // Start the Service
  pService->start();
  delay(100);

  // Start advertising
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();

  Serial.println("Waiting for a client to connect...");
}

void loop() {
  // Sample data to send
  static uint8_t value1 = 0;
  static uint8_t value2 = 100;

  // Write sample data to characteristics
  pCharacteristic1->setValue(&value1, 1);
  pCharacteristic1->notify();
  value1++;

  pCharacteristic2->setValue(&value2, 1);
  pCharacteristic2->notify();
  value2--;

  delay(1000); // Notify every second
}

Here's the client side discover code:

import asyncio
from bleak import BleakScanner
from bleak import BleakClient

async def scan_for_services(device_address):
    # Connect to the device and list its services
    scanner = BleakScanner()
    devices = await scanner.discover()
    
    for device in devices:
        if device.address == device_address:  # Check for the ESP32 device by address
            print(f"Found device {device.address}")
            async with BleakClient(device) as client:
                services = await client.get_services()
                for service in services:
                    print(f"Service: {service.uuid}")
                    for characteristic in service.characteristics:
                        print(f"  Characteristic: {characteristic.uuid}")

if __name__ == "__main__":
    device_address = "XX:XX:XX:XX:XX:XX:XX"  # Replace with your ESP32 address
    asyncio.run(scan_for_services(device_address))

Here's the output

Service: 00001801-0000-1000-8000-00805f9b34fb
  Characteristic: 00002a05-0000-1000-8000-00805f9b34fb
Service: 00001800-0000-1000-8000-00805f9b34fb
  Characteristic: 00002a00-0000-1000-8000-00805f9b34fb
  Characteristic: 00002a01-0000-1000-8000-00805f9b34fb
  Characteristic: 00002aa6-0000-1000-8000-00805f9b34fb
Service: 12345678-1234-1234-1234-1234567890ab
  Characteristic: 12345678-1234-1234-1234-1234567890ac

I tried using random UUIDs from the UUID generator, some from out of my head. Some of the UUIDs I used:

  1. 00000011-0000-1000-8000-00805f9b34fb
  2. 00002a57-0000-1000-8000-00805f9b34fb
发布评论

评论列表(0)

  1. 暂无评论