This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Device Name changing with nRF52840 using sd_ble_gap_device_name_set in Python

Hi,

I'm trying to make a script in Python using the nRF52840 dongle, to make some BLE advertising.

I'm using:

- Python 3.7.2 (32 bit)

- pc_ble_driver_py library

- Windows 10

- Visual Code

nRF52840 dongle with USB CDC ACM FW

Now I would like to set a name to my device as I can see it through nRF APP on my mobile phone.

Searching through similar issues in other posts, I've found this function sd_ble_gap_device_name_set, which defined as follow:

def sd_ble_gap_device_name_set(adapter, p_write_perm, p_dev_name, len):
 return _nrf_ble_driver_sd_api_v2.sd_ble_gap_device_name_set(adapter, p_write_perm, p_dev_name, len)

To figure out how to use it, I've searched in some tutorials and found Bluetooth low energy Advertising, a beginner's tutorial which are written in C;

The function in C is declared as follow:

uint32_t sd_ble_gap_device_name_set	(	ble_gap_conn_sec_mode_t const * 	p_write_perm,
                                        uint8_t const * 	p_dev_name,
                                        uint16_t 	len 
                                    )	

In my script, the function is used as follow:

    # Device Name Change
    device_name = split_str(DEVICE_NAME)
    gap_device_name = [hex(ord(x)) for x in device_name]
    str_length = len(DEVICE_NAME.encode(encoding='utf_8'))
    driver.sd_ble_gap_device_name_set(
            peripheral.adapter.driver.rpc_adapter,
            None,
            DEVICE_NAME.encode(encoding='utf_8'),
            str_length
            )

when in Debug mode with Visual Code, I receive this message:
Can anyone help me to solve this issue?
Thanx,
Fabio
Parents
  • Hello Fabio,

    What is the type of your passed str_length variable?
    As you see in the sd_ble_gap_device_name_set the len parameter needs to be of type uint16_t, while then python len functions returns an int ( represented by 4 bytes ).
    I assume this is why you have got your type error.
    Please try to typecast the len variable to uint16_t, and see if this resolves your issue.

    Best regards,
    Karl

  • Hello Karl,

    Yes, the str_length variable is of course an int. 

    The variable DEVICE_NAME is a string "HelloWorld", which is converted and stored in device_name as ['H','e','l','l','o','W','o','r','l','d']

    Googling around there's the ctypes library which seems to have only ctypes.c_uint8 and ctypes.c_uint16, but no c_uint8_t or c_uint16_t. Trying to import ctypes library and using c_uint8 or c_uint16 it in my code, the answer is the same:
    Is there a better way to make a change name on the device?
    Thanx
  • Hello Karl,

    I finally solved it in a simpler and more reliable way, without any conversion:

        # Device Name and Dara Set 
        adv_complete_local_name = 'HelloWorld!'
        adv_manuf_data  =  [0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04]
        adv_data = BLEAdvData(
            complete_local_name=adv_complete_local_name,
            manufacturer_specific_data=adv_manuf_data
        )

    which gives this result:

    I've notice only one thing: the more bytes you insert in manufacturer_specific_data, the shorter adv_complete_local_name must be;if not, (in OS Windows) something happens on the USB port, just like if the nRF52840 dongle is unlink and then link again.

    Thank you,

    Fabio

  • Hello again Fabio,

    Fabio Ballanti said:
    I finally solved it in a simpler and more reliable way, without any conversion:

    Great, I am glad to hear that! This is exactly how the pc-ble-driver-py is intended to be used - no need to look into the C functions running behind the scenes.
    I hope my explanation of this in my last comment was helpful to you.

    Fabio Ballanti said:
    I've notice only one thing: the more bytes you insert in manufacturer_specific_data, the shorter adv_complete_local_name must be;

    Yes, the advertising packet length is indeed limited to 31 bytes ( including overheard ).
    The SoftDevice will shorten the device length if it exceeds this limit.
    How long is the packet length when you notice this behavior?

    Fabio Ballanti said:
    if not, (in OS Windows) something happens on the USB port, just like if the nRF52840 dongle is unlink and then link again.

    I am not sure I understand what you mean by this part. Does the serial port close when you attempt to exceed the advertising length limit, is this what you are saying?

    I am happy to hear that you are now able to dynamically change the name as intended.

    Best regards,
    Karl

  • Hi Karl,

    I hope my explanation of this in my last comment was helpful to you.

    Yes, it was: Thank you!

    How long is the packet length when you notice this behavior?

    OK, 31 bytes is what I've noticed; making the count: 

    if advertising data has 21 bytes payload (+ 2 bytes for type and length) and flags has 3 bytes(type, length and value), local name can have only 3 bytes with 2 bytes for type and length, so you can display 'Hi!';

    if you remove flags for example, you can assign more bytes to local name and display 'Hello!'

    Is there a possibility to increase the number of bytes for advertising data?

    Does the serial port close when you attempt to exceed the advertising length limit, is this what you are saying?

    Yes, it is! Suppose you have, as above, 21 bytes payload and assign to local name 'Hello !' (7 chars), running the script you'll hear a disconnect sound and then a riconnect sound.

    Regards,

    Fabio

  • Hello Fabio.

    Fabio Ballanti said:
    Yes, it was: Thank you!

    I am happy to hear that, no problem at all!

    Fabio Ballanti said:
    if you remove flags for example, you can assign more bytes to local name and display 'Hello!'

    I do not think you should remove the flags as they are required to conform to the BLE protocol.

    Fabio Ballanti said:
    Is there a possibility to increase the number of bytes for advertising data?

    You may also make use of the Scan response feature, in which you may place an additional 31 bytes.
    You can read more about the Scan response data in the Service's tutorial.

    Fabio Ballanti said:
    Yes, it is! Suppose you have, as above, 21 bytes payload and assign to local name 'Hello !' (7 chars), running the script you'll hear a disconnect sound and then a riconnect sound.

    Thank you for clarifying - I was not certain whether you were referring to BLE or USB when you said 'disconnect'.
    I suppose the disconnect and connecting USB sound in windows might play if the device is reset.
    Are you seeing any error written to your logger output when this occurs?

    Best regards,
    Karl

  • Hi Karl,

    You may also make use of the Scan response feature, in which you may place an additional 31 bytes.

    Ok, sounds good. I'll read it.

    Are you seeing any error written to your logger output when this occurs?

    Yes, from the output logger the following message:

      File "..\Python\Python37-32\lib\site-packages\pc_ble_driver_py\ble_driver.py", line 107, in wrapper
        error_code=err_code,
    pc_ble_driver_py.exceptions.NordicSemiException: Failed to ble_gap_adv_data_set. Error code: NRF_ERROR_SD_RPC_SEND

    Thanks,

    Fabio

Reply Children
  • Hello Fabio,

    Fabio Ballanti said:
    Ok, sounds good. I'll read it.

    Great! Please do not hesitate to open a new ticket if you should encounter any issues or questions regarding how to use the Scan response.

    Fabio Ballanti said:
    Yes, from the output logger the following message:

    Thank you for sharing the full error message. It seems this error is returned when you attempt to exceed the legal length of the advertising data.
    As you have already figured out, the best way to avoid this error is staying within the allowed 31 byte limit.

    Hopefully the additional 31 bytes of the scan response will allow you to meet the requirements of your application.

    Best regards,
    Karl

Related