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,

    Fabio Ballanti said:
    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:

    I do not have any personal experience with the ctypes library you reference, but I see that you are typecasting it to an uint8_t, while the length argument needs to be of type uint16_t. Please try to typecast it to uint16_t, and see if the error persists.

    Fabio Ballanti said:
    Is there a better way to make a change name on the device?

    I suppose you could create your own function to do this, in which you mask out the particular bytes from the int and place them over in a two byte structure - making sure to account for the potentially signed value bit of the int.
    I would guess this is what they are doing behind the scenes of the ctypes library anyways - you could check their source code, and if they are doing exactly this then there is no reason to implement your own function to do the same.

    Best regards,
    Karl

Reply
  • Hello,

    Fabio Ballanti said:
    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:

    I do not have any personal experience with the ctypes library you reference, but I see that you are typecasting it to an uint8_t, while the length argument needs to be of type uint16_t. Please try to typecast it to uint16_t, and see if the error persists.

    Fabio Ballanti said:
    Is there a better way to make a change name on the device?

    I suppose you could create your own function to do this, in which you mask out the particular bytes from the int and place them over in a two byte structure - making sure to account for the potentially signed value bit of the int.
    I would guess this is what they are doing behind the scenes of the ctypes library anyways - you could check their source code, and if they are doing exactly this then there is no reason to implement your own function to do the same.

    Best regards,
    Karl

Children
No Data
Related