h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
670 stars 138 forks source link

NimBLEDevice::setDeviceName doesn't work #601

Closed wwj718 closed 8 months ago

wwj718 commented 8 months ago

from the changelog: setDeviceName allowing for changing the device name while the BLE stack is active.

setDeviceName not working properly

I use esp32(esp32doit-devkit-v1, NimBLE-Arduino@^1.4.1), below is my code:

The DeviceName is always "test1"

#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
#include <NimBLEAdvertisedDevice.h>

/*
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = h2zero/NimBLE-Arduino@^1.4.1
*/

static NimBLEAdvertising *pAdvertising = nullptr;
static uint32_t count = 0;
BLEUUID serviceUUID("123456-0003-4538-BCD7-7ECFB51297C1");

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  NimBLEDevice::init("test1");
  pAdvertising = NimBLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(serviceUUID);
}

void loop() {
  pAdvertising->stop();
  pAdvertising->start();
  delay(3000);
  // setDeviceName allowing for changing the device name while the BLE stack is active.: https://github.com/h2zero/NimBLE-Arduino/blob/bc333ccb6e8d9ff2059af9cbd5006a290a4de3a5/CHANGELOG.md?plain=1#L17
  NimBLEDevice::setDeviceName("test2"); // do not work
}
h2zero commented 8 months ago

If you are meaning to change the advertised name you can only do that while advertising is stopped. That name change you are using here will change the device name characteristic value only in this case.

wwj718 commented 8 months ago

@h2zero Thanks! It works!

I encountered another problem. Is the DeviceName only allowed to be changed once?

#include <Arduino.h>
#include <NimBLEDevice.h>
#include <NimBLEServer.h>
#include <NimBLEAdvertisedDevice.h>

/*
[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = h2zero/NimBLE-Arduino@^1.4.1
*/

static NimBLEAdvertising *pAdvertising = nullptr;
static uint32_t count = 0;
BLEUUID serviceUUID("12345678-0003-4538-BCD7-7ECFB51297C1");

void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");
  NimBLEDevice::init("test1");
  pAdvertising = NimBLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(serviceUUID);
}

void loop() {
  pAdvertising->stop();

  count++;
  char test[100] = "name"; 
  char buffer[50];
  sprintf(buffer, "%u", count);
  strcat(test, buffer);
  Serial.printf(test);
  NimBLEDevice::setDeviceName(test); 

  pAdvertising->start();
  delay(3000);
  // setDeviceName allowing for changing the device name while the BLE stack is active.: https://github.com/h2zero/NimBLE-Arduino/blob/bc333ccb6e8d9ff2059af9cbd5006a290a4de3a5/CHANGELOG.md?plain=1#L17
}

The DeviceName is always "name1" (observe via NRF Connect).

h2zero commented 8 months ago

There are 2 names, one in the advertisement and the other in the device name characteristic. If you just want to change the advertised name you should use pAdvertising->setName("new name");.

wwj718 commented 8 months ago

It works for me, thanks a lot!