Want to use random static BLE address

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
JaEd_1453761
Level 1
Level 1
First like received

I am trying to change the BLE address to a "random static" address and have tried the following API calls: 

   
CYBLE_GAP_BD_ADDR_T addr;CyBle_GapGenerateDeviceAddress(&addr, CYBLE_GAP_RANDOM_STATIC_ADDR, NULL);CyBle_SetDeviceAddress(&addr);CyBle_GapSetIdAddress(&addr);
   

The function to generate the address works but neither of the functions to set it seem to work.  When I start advertising the address that shows up in CySmart is the original factory silicon generated address and not my newly generated address.

   

Ideas?

0 Likes
1 Solution
JaEd_1453761
Level 1
Level 1
First like received

After several hours of looking through the Cypress source code I found a solution but it feels like a hack because I am required to modify a global variable that is within the automatically generated source code.

   

Here is the code I added to the BLE event handler call back function:

   

 /**********************************************************
 *                       General Events
 **********************************************************

   

 case CYBLE_EVT_STACK_ON: /* This event is received when the component is Started */
 
 // Generate a random static address
 apiResult = CyBle_GapGenerateDeviceAddress(&localAddr, CYBLE_GAP_RANDOM_STATIC_ADDR, NULL);
 if(CYBLE_ERROR_OK == apiResult)
 {
     // Set the device address
     CyBle_SetDeviceAddress(&localAddr);
     CyBle_GapSetIdAddress(&localAddr);
                
     // Change the address type in the discovery parameters
     // NOTE: THERE SHOULD BE AN API FOR THIS!!!
     cyBle_discoveryParam.ownAddrType = CYBLE_GAP_ADDR_TYPE_RANDOM;
 }
 else
 {
     // Failed to generate an address so just use
     // the default "public" Cypress address
     localAddr.type = 0u;
 }
            
 // Show the address we are using...
 CyBle_GetDeviceAddress(&localAddr);
 DBG_PRINTF("Bluetooth On, addr: ");
 for(i = CYBLE_GAP_BD_ADDR_SIZE; i > 0u; i--)
 {
     DBG_PRINTF("%2.2x", localAddr.bdAddr[i-1u]);
 }
 DBG_PRINTF("\r\n");
 break;

   

 

   

This line is the one that bothers me:

   

cyBle_discoveryParam.ownAddrType = CYBLE_GAP_ADDR_TYPE_RANDOM;

   

 

   

I figured this out when I noticed this comment in BLE.c

   

/* This variable of type CYBLE_GAPP_DISC_MODE_INFO_T is present only when 
   the BLE component is configured for Peripheral GAP role or Central and 
   Peripheral GAP role. It contains the Advertisement settings and also the 
   Advertisement and Scan response data parameters entered in the customizer.
   This variable can be used by advanced users to change Advertisement settings,
   Advertisement or Scan response data in runtime. 
*/
CYBLE_GAPP_DISC_MODE_INFO_T cyBle_discoveryModeInfo =
{
    0x02u,      /* uint8 discMode */
    &cyBle_discoveryParam,
    &cyBle_discoveryData,
    &cyBle_scanRspData,
    0x001Eu,    /* uint16 advTo */
};

   

 

   

Maybe a Cypress employee can let us know if this is just a case of the component customizer not being mature or a missing API or ???

View solution in original post

2 Replies
JaEd_1453761
Level 1
Level 1
First like received

After several hours of looking through the Cypress source code I found a solution but it feels like a hack because I am required to modify a global variable that is within the automatically generated source code.

   

Here is the code I added to the BLE event handler call back function:

   

 /**********************************************************
 *                       General Events
 **********************************************************

   

 case CYBLE_EVT_STACK_ON: /* This event is received when the component is Started */
 
 // Generate a random static address
 apiResult = CyBle_GapGenerateDeviceAddress(&localAddr, CYBLE_GAP_RANDOM_STATIC_ADDR, NULL);
 if(CYBLE_ERROR_OK == apiResult)
 {
     // Set the device address
     CyBle_SetDeviceAddress(&localAddr);
     CyBle_GapSetIdAddress(&localAddr);
                
     // Change the address type in the discovery parameters
     // NOTE: THERE SHOULD BE AN API FOR THIS!!!
     cyBle_discoveryParam.ownAddrType = CYBLE_GAP_ADDR_TYPE_RANDOM;
 }
 else
 {
     // Failed to generate an address so just use
     // the default "public" Cypress address
     localAddr.type = 0u;
 }
            
 // Show the address we are using...
 CyBle_GetDeviceAddress(&localAddr);
 DBG_PRINTF("Bluetooth On, addr: ");
 for(i = CYBLE_GAP_BD_ADDR_SIZE; i > 0u; i--)
 {
     DBG_PRINTF("%2.2x", localAddr.bdAddr[i-1u]);
 }
 DBG_PRINTF("\r\n");
 break;

   

 

   

This line is the one that bothers me:

   

cyBle_discoveryParam.ownAddrType = CYBLE_GAP_ADDR_TYPE_RANDOM;

   

 

   

I figured this out when I noticed this comment in BLE.c

   

/* This variable of type CYBLE_GAPP_DISC_MODE_INFO_T is present only when 
   the BLE component is configured for Peripheral GAP role or Central and 
   Peripheral GAP role. It contains the Advertisement settings and also the 
   Advertisement and Scan response data parameters entered in the customizer.
   This variable can be used by advanced users to change Advertisement settings,
   Advertisement or Scan response data in runtime. 
*/
CYBLE_GAPP_DISC_MODE_INFO_T cyBle_discoveryModeInfo =
{
    0x02u,      /* uint8 discMode */
    &cyBle_discoveryParam,
    &cyBle_discoveryData,
    &cyBle_scanRspData,
    0x001Eu,    /* uint16 advTo */
};

   

 

   

Maybe a Cypress employee can let us know if this is just a case of the component customizer not being mature or a missing API or ???

Anonymous
Not applicable

By default the component allows you set the public address.

   

If user wants to change it to random or resolvable private address , use has  to edit the global variable

   

as you did.

   

 

   

Regards.

   

Vikas