Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Advertisement Name "on the fly" does not work. #197

Closed
Splitframe opened this issue Nov 8, 2018 · 12 comments
Closed

Change Advertisement Name "on the fly" does not work. #197

Splitframe opened this issue Nov 8, 2018 · 12 comments

Comments

@Splitframe
Copy link

Splitframe commented Nov 8, 2018

Hi,

I want to change the Advertisement name "on the fly".

Currently I do this on the disconnect callback:

void prph_disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;
  
  Bluefruit.Advertising.stop();
  Bluefruit.Advertising.clearData();
  Bluefruit.setName(cname);
  Bluefruit.ScanResponse.addName();
  Bluefruit.Advertising.start();
  
}

The device is not visible afterwards though.

@hathach
Copy link
Member

hathach commented Nov 8, 2018

you could just stop the advertising at anytime (not just disconnect callback). Then change the name and yes, clearData() and re-create the adv/scan_resp packet. Since this is not often used by most user, and it is rather trivial, we don't feel the need to implement this.

@hathach hathach closed this as completed Nov 8, 2018
@Splitframe Splitframe changed the title Change Advertisement Name "on the fly" Change Advertisement Name "on the fly" does not work. Nov 9, 2018
@Splitframe
Copy link
Author

Splitframe commented Nov 9, 2018

I don't want/need it as a seperate function or call, but currently I can't figure out how to do this.
The snipped I put in the first post does not advertise after I call Bluefruit.Advertising.start();.
I also tried the complete SetupAdv() ( from the examples ) instead of just Bluefruit.ScanResponse.addName(); and it's the same.

I changed the topic name to better reflect my problem.

@hathach
Copy link
Member

hathach commented Nov 9, 2018

Oops, I thought it worked that way. I will give it a try, meanwhile can you enable debug level to 1 (or 2) to see if there is any error message

@hathach hathach reopened this Nov 13, 2018
@Splitframe
Copy link
Author

I looked around a little, but I don't know how I enable the debug level.

@hathach
Copy link
Member

hathach commented Nov 13, 2018

it is in in IDE menu "Debug Mode"

@Splitframe
Copy link
Author

Splitframe commented Nov 14, 2018

Thanks.
Here it is: https://pastebin.com/TQtYHSfe

Here my functions:

void setupAdv(void)
{
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();
  
  // Include the BLE UART (AKA 'NUS') 128-bit UUID
  Bluefruit.Advertising.addService(bleuart);
   
  // There is no room for 'Name' in the Advertising packet
  // Use the optional secondary Scan Response packet for 'Name' instead
  Bluefruit.ScanResponse.addName();
}

void changeAdvertisementName() {

  Bluefruit.Advertising.stop();
  Bluefruit.Advertising.clearData();
  Bluefruit.setName(cname);
  setupAdv();
  Bluefruit.Advertising.start();
}

edit: When I read out the name afterwards it shows the correct one, but it's not advertised as such.

  Bluefruit.getName(newName, sizeof(newName));
  Serial.print("[prph] New Adv name: "); 
  Serial.println(newName);

@hathach
Copy link
Member

hathach commented Nov 19, 2018

hi @Splitframe , you didn't re-set the name right. Maybe we didn't make it clear. Advertising and ScanResponse is 2 separate packet.

  • Advertising: 31 bytes that broadcast by nrf52 to central, however it is too short for all information
  • ScanReponse: another 31 bytes that is a response to central when it perform active scan (thus the name scan response).

In your above code, the service is fitted to the advertising packet, and the name is fit to Scan response. If you want to change the name, you only need to clear the scan response e.g

  • Bluefruit.ScanResponse.clearData() and add back the name Bluefruit.ScanResponse.addName();
  • or clear both the Advertising and ScanResponse and call setupAdv() again
void changeAdvertisementName() {
  Bluefruit.Advertising.stop();
  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData(); // add this
  Bluefruit.setName(cname);
  setupAdv();
  Bluefruit.Advertising.start();
}

@Splitframe
Copy link
Author

That works, thanks you.

Sorry for the inconvenience.

@hathach
Copy link
Member

hathach commented Nov 21, 2018

Glad that work for you

@hathach hathach closed this as completed Nov 21, 2018
@aqibnasim
Copy link

Hi @hathach ,

I am trying to do the same procedure to change the name and getting the same. I am getting error when debug mode is enabled:

bool BLEAdvertising::_start(uint16_t, uint16_t): 364: verify failed, error = NRF_ERROR_INVALID_LENGTH

These are my device details:

18:57:16.979 -> [CFG   ] SoftDevice's RAM requires: 0x20002D30
18:57:22.012 -> Advertising is started
18:57:22.012 -> 
18:57:22.012 -> BSP Library : 1.0.1
18:57:22.012 -> Bootloader  : s140 6.1.1
18:57:22.012 -> Serial No   : AB3586AB30FBC471
18:57:22.012 -> 
18:57:22.012 -> --------- SoftDevice Config ---------
18:57:22.012 -> Max UUID128     : 10
18:57:22.012 -> ATTR Table Size : 4096
18:57:22.012 -> Service Changed : 1
18:57:22.012 -> Peripheral Connect Setting
18:57:22.012 ->   - Max MTU         : 23
18:57:22.012 ->   - Event Length    : 3
18:57:22.012 ->   - HVN Queue Size  : 1
18:57:22.012 ->   - WrCmd Queue Size: 1
18:57:22.012 -> 
18:57:22.012 -> --------- BLE Settings ---------
18:57:22.012 -> Name            : Bye world
18:57:22.012 -> Max Connections : Peripheral = 1, Central = 0 
18:57:22.012 -> Address         : C6:CA:9F:1A:75:3D (Static)
18:57:22.012 -> TX Power        : 4 dBm
18:57:22.012 -> Conn Intervals  : min = 20.00 ms, max = 30.00 ms
18:57:22.012 -> Conn Timeout    : 2000 ms
18:57:22.012 -> Peripheral Paired Devices:

@aqibnasim
Copy link

aqibnasim commented Jan 25, 2023

My code snippets:

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();
  Bluefruit.ScanResponse.addName();
  Bluefruit.Advertising.start();
}

... and the flow of code:

  Serial.println("Stopping Advertising");
  Bluefruit.Advertising.stop();

  delay(5000);
  
  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData();
  Bluefruit.setName("Hello world");
  
  Serial.println("Resuming Advertising");
  startAdv();
  delay(20000);

  Serial.println("Stopping Advertising");
  Bluefruit.Advertising.stop();

  delay(5000);

  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData();
  Bluefruit.setName("Bye world");

  Serial.println("Resuming Advertising");
  startAdv();
  delay(20000);

@CSC-Sendance
Copy link
Contributor

My code snippets:

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();
  Bluefruit.ScanResponse.addName();
  Bluefruit.Advertising.start();
}

... and the flow of code:

  Serial.println("Stopping Advertising");
  Bluefruit.Advertising.stop();

  delay(5000);
  
  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData();
  Bluefruit.setName("Hello world");
  
  Serial.println("Resuming Advertising");
  startAdv();
  delay(20000);

  Serial.println("Stopping Advertising");
  Bluefruit.Advertising.stop();

  delay(5000);

  Bluefruit.Advertising.clearData();
  Bluefruit.ScanResponse.clearData();
  Bluefruit.setName("Bye world");

  Serial.println("Resuming Advertising");
  startAdv();
  delay(20000);

Hi, I had the same issue and fixed it in this PR: #784 .

@hathach would be lovely if you could check it out!

hathach added a commit that referenced this issue Jul 26, 2023
FIX #197 : Replace varclr with arrclr for BLEAdvertising _data fields
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants