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

how to send mac address as characteristic value?

hello! i want to send mac address of my bluetooth device. i have written function

uint32_t get_address(void)
{
    uint32_t err_code;
    ble_gap_addr_t  ble_gap_addr;
    memset(&ble_gap_addr, 0, sizeof(ble_gap_addr));
    ble_gap_addr.addr_type= BLE_GAP_ADDR_TYPE_PUBLIC;
    err_code = sd_ble_gap_address_get(&ble_gap_addr);
    return(err_code);
}

but i can't store data into array. can you help me?

  • The address is stored in the ble_gap_addr variable, which goes out of scope once you leave the function. You need to either declare the variable outside of this function, or make the function take a ble_gap_addr_t-pointer which you then use in the sd_ble_gap_address_get() call.

  • hello i declared ble_gap_addr outside function and write function as below

    uint8_t get_address(void)
    {
        uint8_t err_code;
        memset(&ble_gap_addr, 0, sizeof(ble_gap_addr));
        ble_gap_addr.addr_type= BLE_GAP_ADDR_TYPE_RANDOM_STATIC;//BLE_GAP_ADDR_TYPE_PUBLIC;
        err_code = sd_ble_gap_address_get(&ble_gap_addr);
        return(err_code);
    } 
    

    and then i write main code as below

    int main(void)
    { 
        uint32_t err_code;
        uint8_t		char2_data[CHARACTERISTIC_SIZE];
        uint8_t i=0;
        // Initialize
        timers_init();
        ble_stack_init();
        scheduler_init();
        gap_params_init();
        advertising_init();
        conn_params_init();
        sec_params_init();
    
        // Start execution
        advertising_start();
        get_address();
        services_init();
    
     while(1)
     {
            char2_data[8] = get_address();//{0x10,0x20,};
            err_code = custom_service_update_data(m_conn_handle,char2_data);
            APP_ERROR_CHECK(err_code);
            app_sched_execute();
            power_manage();
        }
    }
    

    but in master control panel i am getting like this

    AttributeHandleschanged Value:BD-9D-01-00-A8-2C-00-20-00-00-00-00-ED-84-01
    

    can you tell me what should i return?

  • The return value of your function get_address() is not mac address but the error code of nRF51 SDK. The value is NRF_SUCCESS or NRF_ERROR_INVALID_ADDR.

  • like this:

    ble_gap_addr_t ble_gap_addr;
    
    uint32_t get_address(void)
    {
        return sd_ble_gap_address_get(&ble_gap_addr);
    }
    
    int main(void)
    {
        ......
    
        while (1) {
            uint32_t err_code;
            err_code = get_address();
            APP_ERROR_CHECK(err_code);
            err_code = custom_service_update_data(m_conn_handle, ble_gap_addr.addr);
            APP_ERROR_CHECK(err_code);
            app_sched_execute();
            power_manage(); 
        } 
    }
    
Related