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

Changing trasmission power with nRF51822 (Bluetooth Smart Beacon Kit)

Hello everybody.

I'm trying to modify the original firmware that comes with the nRF51822 to make it sweep all of the permitted levels of transmission power. I was able to change the default transmission power by calling the function sd_ble_gap_tx_power_set() from within the advertising_start loop.

What I'm trying to do now is force the beacon to sweep trough all of the permitted power levels while in advertising mode. Digging up the code I have found a declaration in beacon.h:

define APP_BEACON_ADV_TIMEOUT 0 /++< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout */

My idea is to set a certain value for APP_BEACON_ADV_TIMEOUT, and my guess is that an interrupt will be generated when the timer expires and the device stops advertising. I need help intercepting this interrupt.

Thanks.

  • Have you tried registering a callback using

    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    
  • Hi,

    First you will need to subscribe for BLE events, so in the function ble_stack_init(), you will need to add this:

    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);
    

    All BLE events arrive at the ble_evt_dispatch() function, so here we can add your advertising timeout function:

    static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
    {
        adv_timeout_on_ble_evt(p_ble_evt);
    }
    

    In the advertising timeout function you will need to check for the BLE event called BLE_GAP_EVT_TIMEOUT and with source BLE_GAP_TIMEOUT_SRC_ADVERTISING. You can then change the TX-power as you like, and then start the advertising again.

    void adv_timeout_on_ble_evt(ble_evt_t * p_ble_evt){
        
        if(p_ble_evt->header.evt_id == BLE_GAP_EVT_TIMEOUT && 
           p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING){
               //Change TX-power with sd_ble_gap_tx_power_set()
               advertising_start();
        }
            
    }
    
  • Hello, thank you for the in-depth reply.

    I followed your instructions, but instead of creating a new function like void adv_timeout_on_ble_evt() as you suggested, I stuffed my code inside the pre-existing on_ble_evt( ), inside the case BLE_GAP_EVT_TIMEOUT. I have set the timeout to 10 seconds, after which advertising_start()is called.

    For the sake of those who might read this in the future, there was a minor mispell in your code, in fact the correct macro is BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT and not BLE_GAP_TIMEOUT_SRC_ADVERTISING.

    Thank you both again for your time and expertise.

  • Yes, for SoftDevice S110 the correct macro is BLE_GAP_TIMEOUT_SRC_ADVERTISEMENT. For SoftDevice S130/S132/S332 the macro is changed to BLE_GAP_TIMEOUT_SRC_ADVERTISING.

Related