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

Changing the Advertising Name using NRF tool App

Hi,

I am using NRF51822 pca10028 UART ble example ,

I need to give provision to change the name of the device after pairing and reset the device so that it starts advertising with the name provided by the user, 

Thank you

Parents
  • Hi Mttrinh,

    I tried using pstorage ,got the data in nus_data_handler() and used sd_ble_gap_device_name_set () passed the value and gave a soft reset sd_nvic_SystemReset() .

    The name change works or reflects  only in NRF toolbox UART app but when i search in normal BLE Scanner the name is not updated .

     
    int main(void)
    {
     uint32_t err_code;
        bool erase_bonds;
        uint8_t  start_string[] = START_STRING;
       
        // Initialize.
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
         uart_init();
        buttons_leds_init(&erase_bonds);
        ble_stack_init();
    		 device_manager_init(erase_bonds);
    		
    				pstorage_test_store_and_update();
      //		printf("pstorage load blocks 0 \r\n");
    				pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    				nrf_delay_ms(2000);
    			
    				printf("    Data loaded from block 0: %s\r\n", dest_data_0);  
    		
    		gap_params_init();
        services_init();
    	
        advertising_init();
        conn_params_init();
        
    
    
        err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
        APP_ERROR_CHECK(err_code);
        
    	    printf("%s",start_string);
    				
        
    //		DATA_NAME=dest_data_0;
    		// Enter main loop.
        for (;;)
        {
    			
            power_manage();
            printf("\n\rtime0=%lld",time0);
    					nrf_delay_ms(1000);
    					time0++;
        }
    }
    
    
    static void example_cb_handler(pstorage_handle_t  * handle,
    															 uint8_t              op_code,
                                   uint32_t             result,
                                   uint8_t            * p_data,
                                   uint32_t             data_len)
    {
    		if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; }  //If we are waiting for this callback, clear the wait flag.
    	
    		switch(op_code)
    		{
    			case PSTORAGE_LOAD_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage LOAD callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_0);				 
    				 }
    				 else
    				 {
    						 printf("pstorage LOAD ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;
    			case PSTORAGE_STORE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage STORE callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_1);
    				 }
    				 else
    				 {
    					   printf("pstorage STORE ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;				 
    			case PSTORAGE_UPDATE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage UPDATE callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_2);
    				 }
    				 else
    				 {
    						 printf("pstorage UPDATE ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;
    			case PSTORAGE_CLEAR_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage1 CLEAR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_3);
    				 }
    				 else
    				 {
    					   printf("pstorage 2CLEAR ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;	 
    		}			
    }
    
    static void pstorage_test_store_and_update(void)
    {
    	
    		pstorage_module_param_t param;
    
    	
    	
    		retval = pstorage_init();
    		if(retval != NRF_SUCCESS)
    		{
    				bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    		}
    	     
    		param.block_size  = 16;                   //Select block size of 16 bytes
    		param.block_count = 4;                   //Select 10 blocks, total of 160 bytes
    		param.cb          = example_cb_handler;   //Set the pstorage callback handler
    			nrf_delay_ms(1000);
    		printf("\r\n\r\npstorage initializing ... \r\n");
    		retval = pstorage_register(&param, &handle);
    		if (retval != NRF_SUCCESS)
    		{
    				bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    		}
    
            pstorage_block_identifier_get(&handle, 0, &block_0_handle);
    }
    
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
    if(p_data[0] == 'N')
        {
    					for (int a = 1; a < length; a++) 
            { 
           source_data_0[a] =p_data[a] ; 
    			// while(app_uart_put(p_data[a]) != NRF_SUCCESS);
    		}
    		printf("pstorage clear 48 bytes from block 0 \r\n");
    		retval = pstorage_clear(&block_0_handle, 48);                       
    		if(retval != NRF_SUCCESS)  { bsp_indication_set(BSP_INDICATE_RCV_ERROR); }
    		
    			printf("pstorage store into block 0\r\n    Data written into block 0: %s\r\n", source_data_0);
    		pstorage_store(&block_0_handle, source_data_0, 16, 0);     //Write to flash
    			nrf_delay_ms(2000);
    		printf("pstorage wait for block 0 store to complete ... \r\n");
    		while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
    		nrf_delay_ms(2000);
    		
    		printf("pstorage load blocks 0 \r\n");
    		pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    			nrf_delay_ms(3000);
    			
    			printf("    Data loaded from block 0: %s\r\n", dest_data_0);
         }
         if(p_data[0] == 'R')
    				{
    					app_uart_flush();
    					printf("Resetcause0x01");
    					nrf_delay_ms(3000);
    					sd_nvic_SystemReset();
    				}
    }
    
    static void gap_params_init(void)
    {
        uint32_t                err_code;
        ble_gap_conn_params_t   gap_conn_params;
        ble_gap_conn_sec_mode_t sec_mode;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                              (const uint8_t *) dest_data_0,
                                             sizeof(dest_data_0));																	
        APP_ERROR_CHECK(err_code);
    
        memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
        gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
        gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
        gap_conn_params.slave_latency     = SLAVE_LATENCY;
        gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;
    
        err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
        APP_ERROR_CHECK(err_code);
    		m_static_pin_option.gap_opt.passkey.p_passkey = &passkey[0];
        err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
        APP_ERROR_CHECK(err_code); 
    }
        
        
        

Reply
  • Hi Mttrinh,

    I tried using pstorage ,got the data in nus_data_handler() and used sd_ble_gap_device_name_set () passed the value and gave a soft reset sd_nvic_SystemReset() .

    The name change works or reflects  only in NRF toolbox UART app but when i search in normal BLE Scanner the name is not updated .

     
    int main(void)
    {
     uint32_t err_code;
        bool erase_bonds;
        uint8_t  start_string[] = START_STRING;
       
        // Initialize.
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
         uart_init();
        buttons_leds_init(&erase_bonds);
        ble_stack_init();
    		 device_manager_init(erase_bonds);
    		
    				pstorage_test_store_and_update();
      //		printf("pstorage load blocks 0 \r\n");
    				pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    				nrf_delay_ms(2000);
    			
    				printf("    Data loaded from block 0: %s\r\n", dest_data_0);  
    		
    		gap_params_init();
        services_init();
    	
        advertising_init();
        conn_params_init();
        
    
    
        err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
        APP_ERROR_CHECK(err_code);
        
    	    printf("%s",start_string);
    				
        
    //		DATA_NAME=dest_data_0;
    		// Enter main loop.
        for (;;)
        {
    			
            power_manage();
            printf("\n\rtime0=%lld",time0);
    					nrf_delay_ms(1000);
    					time0++;
        }
    }
    
    
    static void example_cb_handler(pstorage_handle_t  * handle,
    															 uint8_t              op_code,
                                   uint32_t             result,
                                   uint8_t            * p_data,
                                   uint32_t             data_len)
    {
    		if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; }  //If we are waiting for this callback, clear the wait flag.
    	
    		switch(op_code)
    		{
    			case PSTORAGE_LOAD_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage LOAD callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_0);				 
    				 }
    				 else
    				 {
    						 printf("pstorage LOAD ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;
    			case PSTORAGE_STORE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage STORE callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_1);
    				 }
    				 else
    				 {
    					   printf("pstorage STORE ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;				 
    			case PSTORAGE_UPDATE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage UPDATE callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_2);
    				 }
    				 else
    				 {
    						 printf("pstorage UPDATE ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;
    			case PSTORAGE_CLEAR_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 printf("pstorage1 CLEAR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_ALERT_3);
    				 }
    				 else
    				 {
    					   printf("pstorage 2CLEAR ERROR callback received \r\n");
    						 bsp_indication_set(BSP_INDICATE_RCV_ERROR);
    				 }
    				 break;	 
    		}			
    }
    
    static void pstorage_test_store_and_update(void)
    {
    	
    		pstorage_module_param_t param;
    
    	
    	
    		retval = pstorage_init();
    		if(retval != NRF_SUCCESS)
    		{
    				bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    		}
    	     
    		param.block_size  = 16;                   //Select block size of 16 bytes
    		param.block_count = 4;                   //Select 10 blocks, total of 160 bytes
    		param.cb          = example_cb_handler;   //Set the pstorage callback handler
    			nrf_delay_ms(1000);
    		printf("\r\n\r\npstorage initializing ... \r\n");
    		retval = pstorage_register(&param, &handle);
    		if (retval != NRF_SUCCESS)
    		{
    				bsp_indication_set(BSP_INDICATE_FATAL_ERROR);
    		}
    
            pstorage_block_identifier_get(&handle, 0, &block_0_handle);
    }
    
    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
    {
    if(p_data[0] == 'N')
        {
    					for (int a = 1; a < length; a++) 
            { 
           source_data_0[a] =p_data[a] ; 
    			// while(app_uart_put(p_data[a]) != NRF_SUCCESS);
    		}
    		printf("pstorage clear 48 bytes from block 0 \r\n");
    		retval = pstorage_clear(&block_0_handle, 48);                       
    		if(retval != NRF_SUCCESS)  { bsp_indication_set(BSP_INDICATE_RCV_ERROR); }
    		
    			printf("pstorage store into block 0\r\n    Data written into block 0: %s\r\n", source_data_0);
    		pstorage_store(&block_0_handle, source_data_0, 16, 0);     //Write to flash
    			nrf_delay_ms(2000);
    		printf("pstorage wait for block 0 store to complete ... \r\n");
    		while(pstorage_wait_flag) { power_manage(); }              //Sleep until store operation is finished.
    		nrf_delay_ms(2000);
    		
    		printf("pstorage load blocks 0 \r\n");
    		pstorage_load(dest_data_0, &block_0_handle, 16, 0);				 //Read from flash, only one block is allowed for each pstorage_load command
    			nrf_delay_ms(3000);
    			
    			printf("    Data loaded from block 0: %s\r\n", dest_data_0);
         }
         if(p_data[0] == 'R')
    				{
    					app_uart_flush();
    					printf("Resetcause0x01");
    					nrf_delay_ms(3000);
    					sd_nvic_SystemReset();
    				}
    }
    
    static void gap_params_init(void)
    {
        uint32_t                err_code;
        ble_gap_conn_params_t   gap_conn_params;
        ble_gap_conn_sec_mode_t sec_mode;
    
        BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
        
        err_code = sd_ble_gap_device_name_set(&sec_mode,
                                              (const uint8_t *) dest_data_0,
                                             sizeof(dest_data_0));																	
        APP_ERROR_CHECK(err_code);
    
        memset(&gap_conn_params, 0, sizeof(gap_conn_params));
    
        gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
        gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
        gap_conn_params.slave_latency     = SLAVE_LATENCY;
        gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;
    
        err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
        APP_ERROR_CHECK(err_code);
    		m_static_pin_option.gap_opt.passkey.p_passkey = &passkey[0];
        err_code = sd_ble_opt_set(BLE_GAP_OPT_PASSKEY, &m_static_pin_option);
        APP_ERROR_CHECK(err_code); 
    }
        
        
        

Children
Related