This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

error: invalid length in advertising init

hii...

i hv maufature_array of size 7 to advertise.... am facing a problem while advertising battery level service with dat... my code is lyk dis

 uint32_t      err_code;
 uint8_t       flags =BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
 ble_advdata_t              advdata1;
 ble_advdata_service_data_t service_data[1];
ble_advdata_manuf_data_t   manuf_specific_data;
uint8_t                    manuf_data_array[6];	

manuf_data_array[0] = 0x01;
manuf_data_array[1] = 0x02;
manuf_data_array[2] = 0x03;
manuf_data_array[3] =0x04;
manuf_data_array[4] = 0x05;
manuf_data_array[5] = 0x06;

manuf_specific_data.company_identifier = COMPANY_IDENTIFER;  /* COMPANY IDENTIFIER */
manuf_specific_data.data.p_data = manuf_data_array;
manuf_specific_data.data.size = sizeof(manuf_data_array);

uint8_t battery              = do_battery_measurement();
service_data[0].service_uuid = BLE_UUID_BATTERY_SERVICE;
service_data[0].data.p_data  = &battery;
service_data[0].data.size    = sizeof(battery);


// Build and set advertising data
memset(&advdata1, 0, sizeof(advdata1));

advdata1.name_type               = BLE_ADVDATA_FULL_NAME;
advdata1.flags.size              = sizeof(flags);
advdata1.flags.p_data            = &flags;
advdata1.p_service_data_array    = service_data;
advdata1.service_data_count      = 1;
advdata1.p_manuf_specific_data   = &manuf_specific_data;

err_code = ble_advdata_set(&advdata1, NULL);
APP_ERROR_CHECK(err_code);
  • I'm guessing it's sd_ble_gap_adv_data_set that is throwing the NRF_ERROR_INVALID_LENGTH? I would perhaps try printing both dlen and srdlen before the call to this function to see what they are

  • A BLE advertising packet contains at most 31 bytes data. You can try putting some data into the scan response, another 31bytes packet. For instance,

    uint32_t      err_code;
    ble_advdata_t advdata;
    ble_advdata_t scanrsp;
    
    uint8_t       flags = BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE;
    
    ble_uuid_t adv_uuids[] =
    {
        {BLE_UUID_BATTERY_SERVICE,                  BLE_UUID_TYPE_BLE},
        {BLE_UUID_HEART_RATE_SERVICE,               BLE_UUID_TYPE_BLE},
        {BLE_UUID_DEVICE_INFORMATION_SERVICE,       BLE_UUID_TYPE_BLE},
        {BLE_UUID_NUS_SERVICE,                      m_nus.uuid_type}
    }; // Too long to fit all in advdata
    
    // Build and set advertising data
    memset(&advdata, 0, sizeof(advdata));
    memset(&scanrsp, 0, sizeof(scanrsp));
    
    advdata.name_type               = BLE_ADVDATA_FULL_NAME;
    advdata.include_appearance      = true;
    advdata.flags.size              = sizeof(flags);
    advdata.flags.p_data            = &flags;
    scanrsp.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
    scanrsp.uuids_complete.p_uuids  = adv_uuids;
    
    err_code = ble_advdata_set(&advdata, &scanrsp);
    APP_ERROR_CHECK(err_code);
    
  • but i want to advertise battery level as a service....

  • As @scytulip says, you cannot have more than 31 bytes of data in the advertising packets. He has given you a solution, to use scan request and scan response to obtain additional data.

    This and this may also be helpful to you.

Related