Using BLE with 1.3. How to set the local name of the device to show in the advertising packet

Yes. I just did This with a class of 15 students and made sure they set a unique name before ever connecting to the phone and it worked

@iitgrad And that was possible with this code only?

I haven't tried it, but the previous statements came from my understanding of how advertising works

You have only one set of advertising data you can tell the BLE hardware to advertise, hence it has to contain all info you want to advertise and all services as well as the local name and potentially custom data.

For the sake of advertising it's quite irrelevant whether you want to bundle the bleSerial service with another service or a local name or a custom data block.
You just have one instance of BleAdvertisingData to pass to BLE.advertise() and bleSerial.advertise() does not take any parameters, hence you can't just hand it a BleAdvertisingData instance that only contains the local name.

For Rick's statement in your screenshot:
That portion you undlerlined in green has to be read in connection with the comment
// append your own service UUIDs here
If you don't want any services added but a name (which Rick didn't explicitly state but the common pattern should be self-explanatory) you'd replace that comment with
data.addLocalName("yourDesiredName");

To break it down

BleAdvertisingData data;                            // prepare a fresh/empty advertising data instance
data.appendLocalName("yourDesiredName");            // give the device a name to advertise (can be anywhere in the bundle)
data.appendServiceUUID(bleSerial.getServiceUuid()); // add the UUID that is created by the bleSerial object
// optionally add any additional service UUIDs
// optionally add any custom data you want (if it still fits)
BLE.advertise(&data);                               // tell BLE to advertise all of the above

I’m kinda confused but think I get that what your saying about the BLE Data Burst has to contain the Serial UART Data + the LocalName Data.

Based on @iitgrad 's example that he says worked for him I feel as if the code below would be the minimum required to only change the BLE Local Name? Is that right?

I loaded it but didn’t see the Local Name Change and didn’t see it in the iOS nRF app but I was having issues with the iOS version in previous testing. Maybe it is working and its a App or caching issue where the old Local Name is still stored.

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

    BleAdvertisingData data;
    data.appendLocalName("Test");
    BLE.advertise(&data);
}

If the UUID is required to be added for Serial BLE to also be broadcast then I would think the code should look like below where the UUID has been added:

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

    BleAdvertisingData data;
    data.appendServiceUUID(bleSerial.getServiceUuid());
    data.appendLocalName("Test");
    BLE.advertise(&data);
}

Should one of those be actually working?

I don’t know if it’s the code or BLE App Caching issues causing the Local Name not to change?

Correct!

Yup, that should do the trick - notwithstanding any roadblocks any given mobile app might throw at you with local name caching of any known device.

OK, so that’s pretty simple.

So to confirm, I do need to add the UUID changing the BLE Local Name?

The required code to get both the Serial UART UUID + The custom Local Name to broadcast together is below?

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

    BleAdvertisingData data;
    data.appendServiceUUID(bleSerial.getServiceUuid());
    data.appendLocalName("Test");
    BLE.advertise(&data);
}

Not quite the way you formulate it there :wink:
You need to add/append the UUID in order to advertise the UART service.
You need to add/append the Local Name in order to tell the outer world about the name.
And you need to do both when you want both things advertised simultaneously.

On holiday you don't need to pack your swim shorts in order to have your passport with you but if you want to go swimming it would be good to pack them anyway :wink:

Am I missing the Add Characteristic first?

If so what is exactly required when I’m just using Serial UART and Changing the Local Name? I see @iitgrad Added txCharacteristic() + RxCharacteristic() but I’m not sure if that required when using Ricks New BLE Serial UART library?

I think what is confusing me is that I’m not seeing what is being set in the background when I’m just running the code below which sends and receives Serial BLE data perfectly. In the example below none of the addCharacteristic’s or data.Append functions need to be added or called.

Now when all I want to do is change the BLE Local Name I have to start adding Characteristics + data.append the added characteristic’s. It just feels like it’s getting more complicated than it needs to be.

I’m trying to just keep it simple for the sake of the code and for the sake of my understanding.

#include "BleSerialPeripheralRK.h"
SerialLogHandler logHandler;
SYSTEM_THREAD(ENABLED);
// First parameter is the transmit buffer size, second parameter is the receive buffer size
BleSerialPeripheralStatic<256, 256> bleSerial;
const unsigned long TRANSMIT_PERIOD_MS = 1000;
unsigned long lastTransmit = 0;
int counter = 0;
void setup() {
    Mesh.off();
    WiFi.off();
    Serial.begin();
    // This must be called from setup()!
    bleSerial.setup();
    // If you don't have any other services to advertise, just call advertise().
    // Otherwise, call getServiceUuid() to get the serial service UUID and add that to your
    // custom advertising data payload and call BLE.advertise() yourself with all of your necessary
    // services added.
    BleAdvertisingData data;
 
    bleSerial.advertise();

    // take control of the LED to turn it off to lowest power operation. Prevents LED from lighting up on every wake. 
  RGB.control(true);
  // scales brightness of all three colors, 0-255.
  // the following sets the RGB LED brightness to 25%:
  RGB.brightness(0);
}
void loop() {
    // This must be called from loop() on every call to loop.
    bleSerial.loop();
    // Print out anything we receive
    if(bleSerial.available()) {
        String s = bleSerial.readString();
        Log.info("received: %s", s.c_str());
    }
    if (millis() - lastTransmit >= TRANSMIT_PERIOD_MS) {
        lastTransmit = millis();
        // Every two seconds, send something to the other side
        bleSerial.printlnf("Bat V %d, I %d, W %d / Solar V %d, I %d, W %d", counter,counter,counter,counter,counter, counter);
        Log.info("counter=%d", counter++);
    }
}

That shouldn't be required as all that should be done by the bleSerial.setup() call.

However, I was going to put all the theory into practice and found that something seems to be fishy.
The UUIDs for service and characteristics don't seem to be advertised (not even with bleSerial.advertise().

Nice to know I’m not the only one this is not working for.

Did the vanilla sample with that library work for you at all (before trying to add the name)?

I had accidentally left this added line in that code above.

This line was removed from Ricks example code: BleAdvertisingData data;

I’m running exactly what’s below.

#include "BleSerialPeripheralRK.h"
SerialLogHandler logHandler;
SYSTEM_THREAD(ENABLED);
// First parameter is the transmit buffer size, second parameter is the receive buffer size
BleSerialPeripheralStatic<256, 256> bleSerial;
const unsigned long TRANSMIT_PERIOD_MS = 1000;
unsigned long lastTransmit = 0;
int counter = 0;
void setup() {
    Mesh.off();
    WiFi.off();
    Serial.begin();
    // This must be called from setup()!
    bleSerial.setup();
    // If you don't have any other services to advertise, just call advertise().
    // Otherwise, call getServiceUuid() to get the serial service UUID and add that to your
    // custom advertising data payload and call BLE.advertise() yourself with all of your necessary
    // services added.

    bleSerial.advertise();

    // take control of the LED to turn it off to lowest power operation. Prevents LED from lighting up on every wake. 
  RGB.control(true);
  // scales brightness of all three colors, 0-255.
  // the following sets the RGB LED brightness to 25%:
  RGB.brightness(0);
}
void loop() {
    // This must be called from loop() on every call to loop.
    bleSerial.loop();
    // Print out anything we receive
    if(bleSerial.available()) {
        String s = bleSerial.readString();
        Log.info("received: %s", s.c_str());
    }
    if (millis() - lastTransmit >= TRANSMIT_PERIOD_MS) {
        lastTransmit = millis();
        // Every two seconds, send something to the other side
        bleSerial.printlnf("Bat V %d, I %d, W %d / Solar V %d, I %d, W %d", counter,counter,counter,counter,counter, counter);
        Log.info("counter=%d", counter++);
    }
}

Actually I meant the sample that comes with the library (without any modifications)

#include "BleSerialPeripheralRK.h"

SerialLogHandler logHandler;

SYSTEM_THREAD(ENABLED);

// First parameter is the transmit buffer size, second parameter is the receive buffer size
BleSerialPeripheralStatic<256, 256> bleSerial;

const unsigned long TRANSMIT_PERIOD_MS = 2000;
unsigned long lastTransmit = 0;
int counter = 0;


void setup() {
	Serial.begin();

	// This must be called from setup()!
	bleSerial.setup();

	// If you don't have any other services to advertise, just call advertise().
	// Otherwise, call getServiceUuid() to get the serial service UUID and add that to your
	// custom advertising data payload and call BLE.advertise() yourself with all of your necessary
	// services added.
	bleSerial.advertise();
}

void loop() {
	// This must be called from loop() on every call to loop.
	bleSerial.loop();

	// Print out anything we receive
	if(bleSerial.available()) {
		String s = bleSerial.readString();

		Log.info("received: %s", s.c_str());
	}

	if (millis() - lastTransmit >= TRANSMIT_PERIOD_MS) {
		lastTransmit = millis();

		// Every two seconds, send something to the other side
		bleSerial.printlnf("testing %d", ++counter);
		Log.info("counter=%d", counter);
	}
}

This is only advertises the UUID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e service but once connected it doesn’t expose it

nRF Connect and Bluefruit app can only see these four services

UUID: 0x1800
UUID: 0x1801
UUID: 6fa90001-5c4e-48a8-94f4-8030546f46fc
UUID: f5720000-13a9-49dd-ac15-f87b7427e37b

but not the expected

UUID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e

(Argon 1.3.0-rc.1)

Yea the code straight from his example worked just fine.

I just increased the TX buffer size and then changed the data that was being sent out over Serial BLE.

Then I turned off Wifi & Mesh to see how low the power consumption was.

Then I started asking about how to change the Local Name for the BLE and here we are now not knowing how to actually make that happen easily.

Is this a bug?

You mean, with that code you can connect via Bluefruit and send data to the device?
This is what I get with that code when I connect to the device

This is how it looks with the BLEPeripheral example

I'd consider this a bug in the library.

Yea, works perfect. I just don’t know how to change the Argon—xxxxx to the My-name like you have it.

I’m using this library: BLESerial library alternatives

That’s what I’m using too :confused:

@ScruffR On IOS or Android?

On android version of Adafruit BLfruit I get hex for the device name.

Show me the code that changed the Name to myName :slight_smile:

Android (Samsung) I’ll try other devices tomorrow.

In the mean time can you share the code that allowed you to change the name to “myName”? :wink: