You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

ESP32 BLE客户端开发求助:如何通过广播短名称连接BLE设备

Connecting ESP32 BLE Client by Short Name Only

Absolutely—you don't need service or characteristic UUIDs to connect to a BLE device if all you need is to recognize it as a beacon when connected. Your use case is perfect for this approach, since you're not transmitting data, just detecting the presence of the device via connection.

How to Implement This

Here's how to modify your BLE client code to connect using only the advertised short name:

  1. Scan for devices and filter by name
    Set up a scan callback that checks each discovered device's advertised name against your target (in your case, "BOX_A1"). Once found, stop scanning and target that device for connection.

  2. Connect directly without service discovery
    Since you don't need to exchange data, you can skip the service/characteristic discovery step entirely—just connect to the device once it's found.

Example Client Code

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

#define TARGET_DEVICE_NAME "BOX_A1" // Match your server's advertised name

bool targetFound = false;
BLEAdvertisedDevice discoveredTarget;

// Callback to handle discovered BLE devices
class DeviceScanCallback : public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice device) {
        String deviceName = device.getName().c_str();
        Serial.printf("Discovered device: %s\n", deviceName.c_str());

        // Check if this is our target device
        if (deviceName == TARGET_DEVICE_NAME) {
            device.getScan()->stop(); // Halt scanning once target is found
            discoveredTarget = device;
            targetFound = true;
            Serial.println("Target beacon device found!");
        }
    }
};

void setup() {
    Serial.begin(115200);
    Serial.println("Initializing BLE Client...");
    BLEDevice::init(""); // Client doesn't need a fixed name for this use case

    // Configure and start BLE scan
    BLEScan* scan = BLEDevice::getScan();
    scan->setAdvertisedDeviceCallbacks(new DeviceScanCallback());
    scan->setActiveScan(true); // Active scan is faster for discovery
    scan->start(5); // Scan for 5 seconds; adjust timeout as needed
}

void loop() {
    if (targetFound) {
        BLEClient* client = BLEDevice::createClient();
        Serial.println("Attempting to connect to target...");

        if (client->connect(&discoveredTarget)) {
            Serial.println("Connected to beacon device!");
            // Add your post-connection logic here (e.g., log the detection)
            delay(3000); // Keep connection open briefly (adjust as needed)
            client->disconnect();
            Serial.println("Disconnected from beacon");
        } else {
            Serial.println("Connection failed—retrying...");
        }

        targetFound = false;
        BLEDevice::getScan()->start(5); // Restart scanning for next detection
    }
    delay(1000);
}

Key Notes

  • Name Matching: The code relies on the target device advertising its short name (your server uses BLEDevice::init("BOX_A1"), which sets this correctly).
  • No Service Discovery: Since you don't need to read/write characteristics, we skip calling client->discoverServices() entirely—this saves resources and simplifies the code.
  • Scan Adjustments: You can tweak the scan duration (scan->start(5)) or switch to passive scan (setActiveScan(false)) based on your power consumption needs.

Your existing BLE server code works perfectly for this scenario—it will blink the LED when the client connects, which aligns with your beacon detection goal.

内容的提问来源于stack exchange,提问作者Andrew Taylor

火山引擎 最新活动