Zephyr BLE example advertise not working.

Hi,

I am working on creating an example for BLE application where I try to start the advertising of BLE device. I have seen other example in the folder bluetooth/peripharal which works fine and device can advertise itself. The following is the code spinet off the application I flashing separately on both the cores of the device.

The code also have defined callbacks for connection and disconnection. Still the device failed to advertise itself.

static void bt_ready(void){

    int err;

	printk("Bluetooth initialized\n");

    char addr_s[BT_ADDR_LE_STR_LEN];
    
    settings_load();
    
    err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));

    if(err) {
    printk("Advertise did not start..");
    }


}

void main(void)

{
    int ret;

    if(!gpio_is_ready_dt(&bpled)){
        return;
    }
    ret = gpio_pin_configure_dt(&bpled, GPIO_OUTPUT_ACTIVE);

    if(ret<0){
        return;
    }

    if(!gpio_is_ready_dt(&button)){
        return;
    }

    ret = gpio_pin_configure_dt(&button, GPIO_INPUT);

    ret = gpio_pin_interrupt_configure_dt(&button, GPIO_INT_EDGE_TO_ACTIVE);

    gpio_init_callback(&button_callback_data, btn_pressed, BIT(button.pin));
	gpio_add_callback(button.port, &button_callback_data);

    ret = bt_enable(NULL);

    if(ret){
    printk("Bluetooth failed to init");
    }

    if(ret) {
    printk("Bluetooth failed to intialise");
    return;
    }

    bt_ready(); 

}

The following is the code of .conf config settings which also I have copied from the example which works.

CONFIG_GPIO=y


CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048

CONFIG_BT=y
CONFIG_BT_DEBUG_LOG=y
CONFIG_BT_SMP=y
CONFIG_BT_SIGNING=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DIS=y
CONFIG_BT_ATT_PREPARE_COUNT=5
CONFIG_BT_BAS=y
CONFIG_BT_HRS=y
CONFIG_BT_IAS=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral Sample Long Name"
CONFIG_BT_DEVICE_APPEARANCE=833
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
CONFIG_BT_DEVICE_NAME_MAX=65

CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y

Parents
  • Hello,

    Do you see the log output? What does it say? And perhaps try to add a line of logging after bt_ready() so that you know that this is reached, and that you didn't encounter any errors before that.

    Best regards,

    Edvin

  • Hi,

    The calls are all executed. I can see that in log.

    And also that it prints.

    "Advertise did not start.." So the problem must be in advertising call.

    Here is other part of my code.

    static const struct bt_data ad[] ={
        BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN)
    };
    

    static void connected(struct bt_conn *connected, uint8_t err)
    {
    	if (err) {
    		//LOG_ERR("Connection failed (err %u)", err);
    	} else {
    		
    		if (!conn) {
    			conn = bt_conn_ref(connected);
    		}
    	}
    }
    
    static void disconnected(struct bt_conn *disconn, uint8_t reason)
    {
    	if (conn) {
    		bt_conn_unref(conn);
    		conn = NULL;
    	}	
    }
    
    /* Set Scan Response data */
    
    static const struct bt_data sd[] = {
    
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.connected = connected,
    	.disconnected = disconnected,
    };

    I tried to remove the GPIO interrupt related things completely to see weather that can help but it did not.

Reply
  • Hi,

    The calls are all executed. I can see that in log.

    And also that it prints.

    "Advertise did not start.." So the problem must be in advertising call.

    Here is other part of my code.

    static const struct bt_data ad[] ={
        BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN)
    };
    

    static void connected(struct bt_conn *connected, uint8_t err)
    {
    	if (err) {
    		//LOG_ERR("Connection failed (err %u)", err);
    	} else {
    		
    		if (!conn) {
    			conn = bt_conn_ref(connected);
    		}
    	}
    }
    
    static void disconnected(struct bt_conn *disconn, uint8_t reason)
    {
    	if (conn) {
    		bt_conn_unref(conn);
    		conn = NULL;
    	}	
    }
    
    /* Set Scan Response data */
    
    static const struct bt_data sd[] = {
    
    	BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
    
    };
    
    
    
    BT_CONN_CB_DEFINE(conn_callbacks) = {
    	.connected = connected,
    	.disconnected = disconnected,
    };

    I tried to remove the GPIO interrupt related things completely to see weather that can help but it did not.

Children
  • Try to replace your log about advertising not starting with this:

        err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
    
        if(err) {
        printk("Advertise did not start. bt_le_adv_start returned %d \n", err);
        }

    What does the log say then?

    Are you using an nRF5340 DK, or is it a custom board? Do the standard samples, such as peripheral_uart work?

    Best regards,

    Edvin

  • Hi,

    The error code is -22 which means "Zero on success or (negative) error code otherwise" according to documentation.

    Yes, I am using the nRF5340-DK board. I have not tested peripheral-uart sample but I have tested some other peripheral samples which works fine.

  • You can look up most error codes in errno.h (zephyr\lib\libc\armstdc\include\errno.h). -22 refers to EINVAL  - Invalid argument.

    I don't know what your "ad" parameter looks like, but I see that the advertising name that you are trying to use is very (!) long. 34 characters. Since the length of the advertising packet is 31 bytes, that will not work. Try something a lot shorter. Perhaps "test_name" for testing purposes. You can experiment with the length of the name that you can use. Actually, the length that you can fit depends on what else you have in your advertising packet, and how many bytes that is. 

    Best regards,

    Edvin

  • This issue might solve the issue for you  Longer BLE Device name . I was fighting with similar (or the same issue) and by changing  BT_LE_ADV_CONN_NAME into BT_LE_ADV_CONN allowed me to add the name into advertisement. Before that I would only be able to add GUID in there.

Related