This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Device Discovery Event not set

Other Parts Discussed in Thread: CC2540

Hello,

I am trying to scan for advertising nearby peripheral devices (CC2540) using a device in central role (CC2540) (I am trying to modify simpleBLESwitch application). When I call "GAPCentralRole_StartDiscovery" function, the scan request is genrated by the central and in return, the peripheral role device also sends back scanRsp data. But the GAP_DEVICE_DISCOVERY_EVENT or GAP_DEVICE_INFO_EVENT are not set hence the "simpleBLECentralEventCB" call back function is not called by the application on the central.

Can anyone suggest a solution for this?

Thanks and regards,

Manan

  • Hi Manan,

    Are you sure the device role is set to Central:
    // Init device role
    if(deviceRole == ROLE_CENTRAL)
    {
    // Start the Device
    VOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );

    // Always connecting or connected when central
    masterSlave_State = BLE_STATE_CONNECTING;
    }

    and add the additional case statements in simpleBLECentralEventCB function definition?
  • Hi Zahid,

    Yes the device role is set to Central. The start device event occurs fine.

    Following is the modified simpleBLECentralEventCB function:

    static uint8 simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
    {
    uint8 i;

    switch ( pEvent->gap.opcode )
    {
    case GAP_DEVICE_INFO_EVENT:
    {
    // if filtering device discovery results based on service UUID
    if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE )
    {
    simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType );
    }
    }
    break;

    case GAP_DEVICE_DISCOVERY_EVENT:
    {

    // if not filtering device discovery results based on service UUID
    if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE )
    {
    // Copy results
    simpleBLEScanRes = pEvent->discCmpl.numDevs;

    osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList,
    (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) );
    }
    }
    break;

    case GAP_LINK_ESTABLISHED_EVENT:
    {
    if ( pEvent->gap.hdr.status == SUCCESS )
    {
    for(i=0; i<MAX_CONNECTIONS;i++)
    {
    //Check against addresses we want to connect to
    if(deviceList[i].addr[5] == pEvent->linkCmpl.devAddr[5])
    {
    deviceList[i].state = BLE_STATE_CONNECTED;
    deviceList[i].connHandle = pEvent->linkCmpl.connectionHandle;
    }
    }
    }
    }
    break;

    case GAP_LINK_TERMINATED_EVENT:
    {
    for(i=0; i<MAX_CONNECTIONS;i++)
    {
    //Check against addresses we want to connect to
    if(deviceList[i].connHandle == pEvent->linkTerminate.connectionHandle)
    {
    deviceList[i].state = BLE_STATE_IDLE;
    }
    }

    }
    break;
    }
    return SUCCESS;
    }

    Do I need to make some other changes as well in the CallBack function?

    Thanks and regards,
    Manan
  • Which library are you using? Can you try using the full library: CC2540_BLE.lib
  • Hi Zahid,

    I am using the full library CC2540_BLE.lib , and also CC254x_BLE_HCI_TL_None.lib.

    Thanks and regards,
    Manan
  • Hello,

    Any update on this? Or is there any other possible method for the central to discover the nearby advertising peripherals and then connect to them?

    Thanks and regards,

    Manan

  • Hi Manan,
    We are looking into this. Also does your application require establishing a connection by the central?
  • Hi Zahid,

    Thank you for your support. Yes, I need to scan for the peripheral devices in the vicinity and then establish connection with them in order to read data.

    Looking forward to a positive reply.

    Thanks and regards,

    Manan

  • Hi Manan,

    It seems those scanning results gets passed through the central.c from the stack. But we need to enable that central task in OSAL.

    In OSAL_SimpleBLESwitch.c, make the following modifications:

    #include "central.h"

    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] =
    {
      LL_ProcessEvent,                                                  // task 0
      Hal_ProcessEvent,                                                 // task 1
      HCI_ProcessEvent,                                                 // task 2
    #if defined ( OSAL_CBTIMER_NUM_TASKS )
      OSAL_CBTIMER_PROCESS_EVENT( osal_CbTimerProcessEvent ),           // task 3
    #endif
      L2CAP_ProcessEvent,                                               // task 4
      GAP_ProcessEvent,                                                 // task 5
      SM_ProcessEvent,                                                  // task 6
      GATT_ProcessEvent,                                                // task 7
      GAPRole_ProcessEvent,                                             // task 8
      GAPCentralRole_ProcessEvent,                                    // task 8.5
      GAPBondMgr_ProcessEvent,                                          // task 9
      GATTServApp_ProcessEvent,                                         // task 10
      SimpleBLESwitch_ProcessEvent                                  // task 11
    };

    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;

    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/

    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;

      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

      /* LL Task */
      LL_Init( taskID++ );

      /* Hal Task */
      Hal_Init( taskID++ );

      /* HCI Task */
      HCI_Init( taskID++ );

    #if defined ( OSAL_CBTIMER_NUM_TASKS )
      /* Callback Timer Tasks */
      osal_CbTimerInit( taskID );
      taskID += OSAL_CBTIMER_NUM_TASKS;
    #endif

      /* L2CAP Task */
      L2CAP_Init( taskID++ );

      /* GAP Task */
      GAP_Init( taskID++ );

      /* SM Task */
      SM_Init( taskID++ );
     
      /* GATT Task */
      GATT_Init( taskID++ );

      /* Profiles */
      GAPRole_Init( taskID++ );
      GAPCentralRole_Init( taskID++ );
     
      GAPBondMgr_Init( taskID++ );

      GATTServApp_Init( taskID++ );

      /* Application */
      SimpleBLESwitch_Init( taskID );
    }

    Best wishes

  • Hello Zahid,

    Sorry for a late reply.
    These modifications are also not working for me. Is it working fine for you? If yes, can you provide me your source so that I can look into the mistake that I might have made?

    Thanks and regards,
    Manan
  • /**************************************************************************************************
      Filename:       simpleBLESwitch.c
      Revised:        $Date: 2010-08-06 08:56:11 -0700 (Fri, 06 Aug 2010) $
      Revision:       $Revision: 23333 $
    
      Description:    This file contains the Simple BLE Switch sample application
                      for use with the CC2540 Bluetooth Low Energy Protocol Stack.
    
      Copyright 2010 - 2014 Texas Instruments Incorporated. All rights reserved.
    
      IMPORTANT: Your use of this Software is limited to those specific rights
      granted under the terms of a software license agreement between the user
      who downloaded the software, his/her employer (which must be your employer)
      and Texas Instruments Incorporated (the "License").  You may not use this
      Software unless you agree to abide by the terms of the License. The License
      limits your use, and you acknowledge, that the Software may not be modified,
      copied or distributed unless embedded on a Texas Instruments microcontroller
      or used solely and exclusively in conjunction with a Texas Instruments radio
      frequency transceiver, which is integrated into your product.  Other than for
      the foregoing purpose, you may not use, reproduce, copy, prepare derivative
      works of, modify, distribute, perform, display or sell this Software and/or
      its documentation for any purpose.
    
      YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
      PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
      INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
      NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
      TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
      NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
      LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
      INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
      OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
      OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
      (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
    
      Should you have any questions regarding your right to use this Software,
      contact Texas Instruments Incorporated at www.TI.com.
    **************************************************************************************************/
    
    /*********************************************************************
     * INCLUDES
     */
    
    #include "bcomdef.h"
    #include "OSAL.h"
    #include "OSAL_PwrMgr.h"
    
    #include "OnBoard.h"
    #include "hal_adc.h"
    #include "hal_led.h"
    #include "hal_key.h"
    #include "hal_lcd.h"
    
    #include "gatt.h"
    
    #include "hci.h"
    
    #include "gapgattserver.h"
    #include "gattservapp.h"
    #include "devinfoservice.h"
    #include "simpleGATTprofile.h"
    
    #if defined( CC2540_MINIDK )
      #include "simplekeys.h"
    #endif
    
    #include "peripheral.h"
    #include "central.h"
    
    #include "gapbondmgr.h"
    
    #include "simpleBLESwitch.h"
    
    /*********************************************************************
     * MACROS
     */
    
    /*********************************************************************
     * CONSTANTS
     */
    
    // Maximum number of scan responses
    #define DEFAULT_MAX_SCAN_RES                  8
    
    // Scan duration in ms
    #define DEFAULT_SCAN_DURATION                 4000
    
    // How often to perform periodic event
    #define SBP_PERIODIC_EVT_PERIOD                   2000
    
    // What is the advertising interval when device is discoverable (units of 625us, 160=100ms)
    #define DEFAULT_ADVERTISING_INTERVAL          160
    
    // Limited discoverable mode advertises for 30.72s, and then stops
    // General discoverable mode advertises indefinitely
    
    #if defined ( CC2540_MINIDK )
    #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_LIMITED
    #else
    #define DEFAULT_DISCOVERABLE_MODE             GAP_ADTYPE_FLAGS_GENERAL
    #endif  // defined ( CC2540_MINIDK )
    
    // Minimum connection interval (units of 1.25ms, 80=100ms) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_MIN_CONN_INTERVAL     80
    
    // Maximum connection interval (units of 1.25ms, 800=1000ms) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_MAX_CONN_INTERVAL     800
    
    // Slave latency to use if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_SLAVE_LATENCY         0
    
    // Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled
    #define DEFAULT_DESIRED_CONN_TIMEOUT          1000
    
    // Whether to enable automatic parameter update request when a connection is formed
    #define DEFAULT_ENABLE_UPDATE_REQUEST         FALSE
    
    // Connection Pause Peripheral time value (in seconds)
    #define DEFAULT_CONN_PAUSE_PERIPHERAL         6
    
    // Company Identifier: Texas Instruments Inc. (13)
    #define TI_COMPANY_ID                         0x000D
    
    #define INVALID_CONNHANDLE                    0xFFFF
    
    // Length of bd addr as a string
    #define B_ADDR_STR_LEN                        15
    
    #define ROLE_CENTRAL                          1
    #define ROLE_PERIPHERAL                       2
    
    #define MAX_CONNECTIONS                       3
    
    // Maximum number of scan responses
    #define DEFAULT_MAX_SCAN_RES                  8
    
    // Scan duration in ms
    #define DEFAULT_SCAN_DURATION                 4000
    
    // Discovey mode (limited, general, all)
    #define DEFAULT_DISCOVERY_MODE                DEVDISC_MODE_ALL
    
    // TRUE to use active scan
    #define DEFAULT_DISCOVERY_ACTIVE_SCAN         TRUE
    
    // TRUE to use white list during discovery
    #define DEFAULT_DISCOVERY_WHITE_LIST          FALSE
    
    // TRUE to use high scan duty cycle when creating link
    #define DEFAULT_LINK_HIGH_DUTY_CYCLE          FALSE
    
    // TRUE to use white list when creating link
    #define DEFAULT_LINK_WHITE_LIST               FALSE
    
    // Default RSSI polling period in ms
    #define DEFAULT_RSSI_PERIOD                   1000
    
    // Default passcode
    #define DEFAULT_PASSCODE                      19655
    
    // Default GAP pairing mode
    #define DEFAULT_PAIRING_MODE                  GAPBOND_PAIRING_MODE_WAIT_FOR_REQ
    
    // Default MITM mode (TRUE to require passcode or OOB when pairing)
    #define DEFAULT_MITM_MODE                     FALSE
    
    // Default bonding mode, TRUE to bond
    #define DEFAULT_BONDING_MODE                  TRUE
    
    // Default GAP bonding I/O capabilities
    #define DEFAULT_IO_CAPABILITIES               GAPBOND_IO_CAP_DISPLAY_ONLY
    
    // Default service discovery timer delay in ms
    #define DEFAULT_SVC_DISCOVERY_DELAY           1000
    
    // TRUE to filter discovery results on desired service UUID
    #define DEFAULT_DEV_DISC_BY_SVC_UUID          TRUE
    
    /*********************************************************************
     * TYPEDEFS
     */
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL VARIABLES
     */
    
    /*********************************************************************
     * EXTERNAL FUNCTIONS
     */
    
    /*********************************************************************
     * LOCAL VARIABLES
     */
    static uint8 simpleBLESwitch_TaskID;   // Task ID for internal task/event processing
    
    // GAP - SCAN RSP data (max size = 31 bytes)
    static uint8 scanRspData[] =
    {
      // complete name
      0x14,   // length of this data
      GAP_ADTYPE_LOCAL_NAME_COMPLETE,
      0x53,   // 'S'
      0x69,   // 'i'
      0x6d,   // 'm'
      0x70,   // 'p'
      0x6c,   // 'l'
      0x65,   // 'e'
      0x42,   // 'B'
      0x4c,   // 'L'
      0x45,   // 'E'
      0x50,   // 'P'
      0x65,   // 'e'
      0x72,   // 'r'
      0x69,   // 'i'
      0x70,   // 'p'
      0x68,   // 'h'
      0x65,   // 'e'
      0x72,   // 'r'
      0x61,   // 'a'
      0x6c,   // 'l'
    
      // connection interval range
      0x05,   // length of this data
      GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
      LO_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),   // 100ms
      HI_UINT16( DEFAULT_DESIRED_MIN_CONN_INTERVAL ),
      LO_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),   // 1s
      HI_UINT16( DEFAULT_DESIRED_MAX_CONN_INTERVAL ),
    
      // Tx power level
      0x02,   // length of this data
      GAP_ADTYPE_POWER_LEVEL,
      0       // 0dBm
    };
    
    // GAP - Advertisement data (max size = 31 bytes, though this is
    // best kept short to conserve power while advertisting)
    static uint8 advertData[] =
    {
      // Flags; this sets the device to use limited discoverable
      // mode (advertises for 30 seconds at a time) instead of general
      // discoverable mode (advertises indefinitely)
      0x02,   // length of this data
      GAP_ADTYPE_FLAGS,
      DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
    
      // service UUID, to notify central devices what services are included
      // in this peripheral
      0x03,   // length of this data
      GAP_ADTYPE_16BIT_MORE,      // some of the UUID's, but not all
      LO_UINT16( SIMPLEPROFILE_SERV_UUID ),
      HI_UINT16( SIMPLEPROFILE_SERV_UUID ),
    
    };
    
    // GAP GATT Attributes
    static uint8 attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple BLE Switch";
    
    // Application states
    enum
    {
      BLE_STATE_IDLE,
      BLE_STATE_CONNECTING,
      BLE_STATE_CONNECTED,
      BLE_STATE_DISCONNECTING,
      BLE_STATE_ADVERTISING 
    };
    
    typedef struct
    {
      uint8 state;
      uint8 connHandle;
      uint8 addr[B_ADDR_LEN]; 
    } deviceList_t;
    
    
    static deviceList_t deviceList[3];
    
    static uint8 currentDevice = 0;
    
    // Initial State
    static uint8 deviceRole = ROLE_PERIPHERAL;
    
    // Some initialization only do once
    static bool initDone = FALSE;
    
    // Number of init counts, a flag to determine action on role change
    static bool initCount = 0;
    
    static void simpleBLECentralRssiCB( uint16 connHandle, int8  rssi );
    static uint8 simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent );
    static void periodicCentralTask( void );
    
    // GAP Role Callbacks
    static const gapCentralRoleCB_t simpleBLERoleCB =
    {
      simpleBLECentralRssiCB,       // RSSI callback
      simpleBLECentralEventCB       // Event callback
    };
    
    // Task ID for internal task/event processing
    static uint8 simpleBLETaskId;
    
    // Application state - initial state is advertising
    static uint8 masterSlave_State = BLE_STATE_ADVERTISING;
    
    static uint8 simpleBLEScanning = FALSE;
    /*********************************************************************
     * LOCAL FUNCTIONS
     */
    static void simpleBLESwitch_ProcessOSALMsg( osal_event_hdr_t *pMsg );
    static void simpleBLESwitch_ProcessGATTMsg( gattMsgEvent_t *pMsg );
    static void peripheralStateNotificationCB( gaprole_States_t newState );
    static void performPeriodicTask( void );
    static void simpleProfileChangeCB( uint8 paramID );
    
    #if defined( CC2540_MINIDK )
    static void simpleBLESwitch_HandleKeys( uint8 shift, uint8 keys );
    #endif
    
    /*********************************************************************
     * PROFILE CALLBACKS
     */
    
    // GAP Role Callbacks
    static gapRolesCBs_t simpleBLESwitch_PeripheralCBs =
    {
      peripheralStateNotificationCB,  // Profile State Change Callbacks
      NULL                            // When a valid RSSI is read from controller (not used by application)
    };
    
    // GAP Bond Manager Callbacks
    static gapBondCBs_t simpleBLESwitch_BondMgrCBs =
    {
      NULL,                     // Passcode callback (not used by application)
      NULL                      // Pairing / Bonding state Callback (not used by application)
    };
    
    // Simple GATT Profile Callbacks
    static simpleProfileCBs_t simpleBLESwitch_SimpleProfileCBs =
    {
      simpleProfileChangeCB    // Charactersitic value change callback
    };
    /*********************************************************************
     * PUBLIC FUNCTIONS
     */
    
    /*********************************************************************
     * @fn      SimpleBLESwitch_Init
     *
     * @brief   Initialization function for the Simple BLE Switch App Task.
     *          This is called during initialization and should contain
     *          any application specific initialization (ie. hardware
     *          initialization/setup, table initialization, power up
     *          notificaiton ... ).
     *
     * @param   task_id - the ID assigned by OSAL.  This ID should be
     *                    used to send messages and set timers.
     *
     * @return  none
     */
    void SimpleBLESwitch_Init( uint8 task_id )
    {
      simpleBLESwitch_TaskID = task_id;
    
      static uint8 bdAddress[6] = {0xff,0xff,0xff,0xff,0xff,0x03};
      HCI_EXT_SetBDADDRCmd(bdAddress);  
      
      // Setup the GAP
      GAP_SetParamValue( TGAP_GEN_DISC_SCAN, DEFAULT_SCAN_DURATION );
      GAP_SetParamValue( TGAP_LIM_DISC_SCAN, DEFAULT_SCAN_DURATION );
      VOID GAP_SetParamValue( TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL );
      
        // Setup Central Profile
      {
        uint8 scanRes = DEFAULT_MAX_SCAN_RES;
        GAPCentralRole_SetParameter ( GAPCENTRALROLE_MAX_SCAN_RES, sizeof( uint8 ), &scanRes );
      }
      
      // Setup the GAP Switch Role Profile
      {
        // start advertising immediately
        uint8 initial_advertising_enable = TRUE;
    
        // By setting this to zero, the device will go into the waiting state after
        // being discoverable for 30.72 second, and will not being advertising again
        // until the enabler is set back to TRUE
        uint16 gapRole_AdvertOffTime = 0;
    
        uint8 enable_update_request = DEFAULT_ENABLE_UPDATE_REQUEST;
        uint16 desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
        uint16 desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
        uint16 desired_slave_latency = DEFAULT_DESIRED_SLAVE_LATENCY;
        uint16 desired_conn_timeout = DEFAULT_DESIRED_CONN_TIMEOUT;
    
        // Set the GAP Role Parameters
        GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &initial_advertising_enable );
        GAPRole_SetParameter( GAPROLE_ADVERT_OFF_TIME, sizeof( uint16 ), &gapRole_AdvertOffTime );
    
        GAPRole_SetParameter( GAPROLE_SCAN_RSP_DATA, sizeof ( scanRspData ), scanRspData );
        GAPRole_SetParameter( GAPROLE_ADVERT_DATA, sizeof( advertData ), advertData );
    
        GAPRole_SetParameter( GAPROLE_PARAM_UPDATE_ENABLE, sizeof( uint8 ), &enable_update_request );
        GAPRole_SetParameter( GAPROLE_MIN_CONN_INTERVAL, sizeof( uint16 ), &desired_min_interval );
        GAPRole_SetParameter( GAPROLE_MAX_CONN_INTERVAL, sizeof( uint16 ), &desired_max_interval );
        GAPRole_SetParameter( GAPROLE_SLAVE_LATENCY, sizeof( uint16 ), &desired_slave_latency );
        GAPRole_SetParameter( GAPROLE_TIMEOUT_MULTIPLIER, sizeof( uint16 ), &desired_conn_timeout );
      }
    
      // Set the GAP Characteristics
      GGS_SetParameter( GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName );
    
      // Set advertising interval
      {
        uint16 advInt = DEFAULT_ADVERTISING_INTERVAL;
    
        GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MIN, advInt );
        GAP_SetParamValue( TGAP_LIM_DISC_ADV_INT_MAX, advInt );
        GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MIN, advInt );
        GAP_SetParamValue( TGAP_GEN_DISC_ADV_INT_MAX, advInt );
      }
    
      // Setup the GAP Bond Manager
      {
        uint32 passkey = 0; // passkey "000000"
        uint8 pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
        uint8 mitm = TRUE;
        uint8 ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
        uint8 bonding = TRUE;
        GAPBondMgr_SetParameter( GAPBOND_DEFAULT_PASSCODE, sizeof ( uint32 ), &passkey );
        GAPBondMgr_SetParameter( GAPBOND_PAIRING_MODE, sizeof ( uint8 ), &pairMode );
        GAPBondMgr_SetParameter( GAPBOND_MITM_PROTECTION, sizeof ( uint8 ), &mitm );
        GAPBondMgr_SetParameter( GAPBOND_IO_CAPABILITIES, sizeof ( uint8 ), &ioCap );
        GAPBondMgr_SetParameter( GAPBOND_BONDING_ENABLED, sizeof ( uint8 ), &bonding );
      }
    
      // Initialize GATT attributes
      GGS_AddService( GATT_ALL_SERVICES );            // GAP
      GATTServApp_AddService( GATT_ALL_SERVICES );    // GATT attributes
      DevInfo_AddService();                           // Device Information Service
      SimpleProfile_AddService( GATT_ALL_SERVICES );  // Simple GATT Profile
    
      // Setup the SimpleProfile Characteristic Values
      {
        uint8 charValue1 = 1;
        uint8 charValue2 = 2;
        uint8 charValue3 = 3;
        uint8 charValue4 = 4;
        uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );
      }
    
    
    #if defined( CC2540_MINIDK )
    
      SK_AddService( GATT_ALL_SERVICES ); // Simple Keys Profile
    
      // Register for all key events - This app will handle all key events
      RegisterForKeys( simpleBLESwitch_TaskID );
    
      // makes sure LEDs are off
      HalLedSet( (HAL_LED_1 | HAL_LED_2), HAL_LED_MODE_OFF );
    
      // For keyfob board set GPIO pins into a power-optimized state
      // Note that there is still some leakage current from the buzzer,
      // accelerometer, LEDs, and buttons on the PCB.
    
      P0SEL = 0; // Configure Port 0 as GPIO
      P1SEL = 0; // Configure Port 1 as GPIO
      P2SEL = 0; // Configure Port 2 as GPIO
    
      P0DIR = 0xFC; // Port 0 pins P0.0 and P0.1 as input (buttons),
                    // all others (P0.2-P0.7) as output
      P1DIR = 0xFF; // All port 1 pins (P1.0-P1.7) as output
      P2DIR = 0x1F; // All port 1 pins (P2.0-P2.4) as output
    
      P0 = 0x03; // All pins on port 0 to low except for P0.0 and P0.1 (buttons)
      P1 = 0;   // All pins on port 1 to low
      P2 = 0;   // All pins on port 2 to low
    
    #endif // #if defined( CC2540_MINIDK )
      
      // Register callback with SimpleGATTprofile
      VOID SimpleProfile_RegisterAppCBs( &simpleBLESwitch_SimpleProfileCBs );
    
      // Enable clock divide on halt
      // This reduces active current while radio is active and CC254x MCU
      // is halted
      HCI_EXT_ClkDivOnHaltCmd( HCI_EXT_ENABLE_CLK_DIVIDE_ON_HALT );
    
    #if defined ( DC_DC_P0_7 )
    
      // Enable stack to toggle bypass control on TPS62730 (DC/DC converter)
      HCI_EXT_MapPmIoPortCmd( HCI_EXT_PM_IO_PORT_P0, HCI_EXT_PM_IO_PORT_PIN7 );
    
    #endif // defined ( DC_DC_P0_7 )
      
      // Central setup
      // Initialize GATT Client
      VOID GATT_InitClient();
      
      // CENTRAL APP STARTUP  
      //devices the central will try and connect to
      uint8 addr1[6] ={0xFF,0xFF,0xFF,0xFF,0xFF,0x02};
      uint8 addr2[6] ={0xFF,0xFF,0xFF,0xFF,0xFF,0x03};
      uint8 addr3[6] ={0xFF,0xFF,0xFF,0xFF,0xFF,0x04};
      
      //uint8 addr3[6] ={0xFF,0xFF,0xFF,0xFF,0xFF,0x00}; //MASTER FOB
      
      VOID osal_memcpy( deviceList[0].addr, addr1, 6 );
      VOID osal_memcpy( deviceList[1].addr, addr2, 6 );
      VOID osal_memcpy( deviceList[2].addr, addr3, 6 );
      
      deviceList[0].state = BLE_STATE_IDLE;
      deviceList[1].state = BLE_STATE_IDLE;
      deviceList[2].state = BLE_STATE_IDLE;
      
      deviceList[0].connHandle = 1;         
      deviceList[1].connHandle = 2;
      deviceList[2].connHandle = 3;  
      
      // Setup a delayed profile startup
      osal_set_event( simpleBLESwitch_TaskID, SBP_START_DEVICE_EVT );
    
    }
    
    /*********************************************************************
     * @fn      SimpleBLESwitch_ProcessEvent
     *
     * @brief   Simple BLE Switch Application Task event processor.  This function
     *          is called to process all events for the task.  Events
     *          include timers, messages and any other user defined events.
     *
     * @param   task_id  - The OSAL assigned task ID.
     * @param   events - events to process.  This is a bit map and can
     *                   contain more than one event.
     *
     * @return  events not processed
     */
    uint16 SimpleBLESwitch_ProcessEvent( uint8 task_id, uint16 events )
    {
    
      VOID task_id; // OSAL required parameter that isn't used in this function
    
      if ( events & SYS_EVENT_MSG )
      {
        uint8 *pMsg;
    
        if ( (pMsg = osal_msg_receive( simpleBLESwitch_TaskID )) != NULL )
        {
          simpleBLESwitch_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );
    
          // Release the OSAL message
          VOID osal_msg_deallocate( pMsg );
        }
    
        // return unprocessed events
        return (events ^ SYS_EVENT_MSG);
      }
    
      if ( events & SBP_START_DEVICE_EVT )
      {
        // Init device role    
        if(deviceRole == ROLE_CENTRAL)
        {
          // Start the Device
          VOID GAPCentralRole_StartDevice( (gapCentralRoleCB_t *) &simpleBLERoleCB );
          
          // Always connecting or connected when central
          masterSlave_State = BLE_STATE_CONNECTING;
        }
        else //PERIPHERAL
        {
          // Start the Device (must be in GAPROLE_INIT state)
          VOID GAPRole_StartDevice( &simpleBLESwitch_PeripheralCBs );
          
          masterSlave_State = BLE_STATE_ADVERTISING; 
        } 
        
        if(initDone == FALSE)
        {
          // Set timer for periodic central event
          osal_start_timerEx( simpleBLESwitch_TaskID, SBP_PERIODIC_EVT, 200 );      
          
          // Start Bond Manager
          VOID GAPBondMgr_Register( &simpleBLESwitch_BondMgrCBs );    
          
          initDone = TRUE;
        }
        initCount ++;    
        
        return ( events ^ SBP_START_DEVICE_EVT );
      }
    
      if ( events & SBP_PERIODIC_EVT )
      {
        // Restart timer
        if ( SBP_PERIODIC_EVT_PERIOD )
        {
          osal_start_timerEx( simpleBLESwitch_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD );
        }
    
        // Perform periodic application task
        performPeriodicTask();
        
        // Perform master slave periodic task
        periodicCentralTask();    
    
        return (events ^ SBP_PERIODIC_EVT);
      }
    
      // Discard unknown events
      return 0;
    }
    
    /*********************************************************************
     * @fn      simpleBLESwitch_ProcessOSALMsg
     *
     * @brief   Process an incoming task message.
     *
     * @param   pMsg - message to process
     *
     * @return  none
     */
    static void simpleBLESwitch_ProcessOSALMsg( osal_event_hdr_t *pMsg )
    {
      switch ( pMsg->event )
      {     
      #if defined( CC2540_MINIDK )
        case KEY_CHANGE:
          simpleBLESwitch_HandleKeys( ((keyChange_t *)pMsg)->state, 
                                          ((keyChange_t *)pMsg)->keys );
          break;
      #endif // #if defined( CC2540_MINIDK )
     
        case GATT_MSG_EVENT:
          // Process GATT message
          simpleBLESwitch_ProcessGATTMsg( (gattMsgEvent_t *)pMsg );
          break;
          
        default:
          // do nothing
          break;
      }
    }
    
    #if defined( CC2540_MINIDK )
    /*********************************************************************
     * @fn      simpleBLESwitch_HandleKeys
     *
     * @brief   Handles all key events for this device.
     *
     * @param   shift - true if in shift/alt.
     * @param   keys - bit field for key events. Valid entries:
     *                 HAL_KEY_SW_2
     *                 HAL_KEY_SW_1
     *
     * @return  none
     */
    static void simpleBLESwitch_HandleKeys( uint8 shift, uint8 keys )
    {
      uint8 SK_Keys = 0;
      uint8 new_adv_enabled_status = TRUE;
      
      (void)shift;  // Intentionally unreferenced parameter
      
      // ROLE SWITCH  
      if ( keys & HAL_KEY_SW_1 )
      {
        SK_Keys |= SK_KEY_LEFT;
        
        if( deviceRole == ROLE_PERIPHERAL)
        {
          // PERIPHERAL --> CENTRAL
          new_adv_enabled_status = FALSE;
          
          //change the GAP advertisement status to opposite of current status
          GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status ); 
          
          deviceRole = ROLE_CENTRAL;
          
          osal_start_timerEx( simpleBLESwitch_TaskID, SBP_START_DEVICE_EVT, 500 );
        }
        else
        {
          // CENTRAL --> PERIPHERAL
          masterSlave_State = BLE_STATE_DISCONNECTING; 
          
          //cancel current link request 
          GAPCentralRole_TerminateLink( 0XFFFE );
          
          //GAPCentralRole_TerminateLink( GAP_CONNHANDLE_ALL );
          //GAPCentralRole_TerminateLink(0);
          //GAPCentralRole_TerminateLink(1);
        }
      }
      // TOOGGLE ALERT
      if ( keys & HAL_KEY_SW_2 )
      {
        SK_Keys |= SK_KEY_RIGHT;
      }  
      
      //set simple keys parameter
      SK_SetParameter( SK_KEY_ATTR, sizeof ( uint8 ), &SK_Keys );
    }
    #endif // #if defined( CC2540_MINIDK )
    
    /*********************************************************************
     * @fn      simpleBLESwitch_ProcessGATTMsg
     *
     * @brief   Process GATT messages
     *
     * @return  none
     */
    static void simpleBLESwitch_ProcessGATTMsg( gattMsgEvent_t *pMsg )
    {  
      GATT_bm_free( &pMsg->msg, pMsg->method );
    }
    
    /*********************************************************************
     * @fn      peripheralStateNotificationCB
     *
     * @brief   Notification from the profile of a state change.
     *
     * @param   newState - new state
    *
    * @return  none
    */
    static void peripheralStateNotificationCB( gaprole_States_t newState )
    {
      switch ( newState )
      {
      case GAPROLE_STARTED:
        {
          uint8 ownAddress[B_ADDR_LEN];
          uint8 systemId[DEVINFO_SYSTEM_ID_LEN];
          
          GAPRole_GetParameter(GAPROLE_BD_ADDR, ownAddress);
          
          // use 6 bytes of device address for 8 bytes of system ID value
          systemId[0] = ownAddress[0];
          systemId[1] = ownAddress[1];
          systemId[2] = ownAddress[2];
          
          // set middle bytes to zero
          systemId[4] = 0x00;
          systemId[3] = 0x00;
          
          // shift three bytes up
          systemId[7] = ownAddress[5];
          systemId[6] = ownAddress[4];
          systemId[5] = ownAddress[3];
          
          DevInfo_SetParameter(DEVINFO_SYSTEM_ID, DEVINFO_SYSTEM_ID_LEN, systemId);
        }
        
        if(initCount >1)
        {
          uint8 turnOnAdv = TRUE;
          GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &turnOnAdv );
        }
        break;
        
      case GAPROLE_ADVERTISING:
        {
        }
        break;      
        
      case GAPROLE_CONNECTED:
        {
          //status 
          masterSlave_State = BLE_STATE_CONNECTED;      
        }
        break;
         
      case GAPROLE_WAITING:
        {
          masterSlave_State = BLE_STATE_ADVERTISING;
        }
        break;
        
      case GAPROLE_WAITING_AFTER_TIMEOUT:
        {
          masterSlave_State = BLE_STATE_ADVERTISING;
        }
        break;
        
      case GAPROLE_ERROR:
        {
           //catch error?
          asm("NOP");
        }
        break;
        
      default:
        {
          masterSlave_State = BLE_STATE_ADVERTISING;      
        }
        break;
        
      }
    }
    
    /*********************************************************************
     * @fn      performPeriodicTask
     *
     * @brief   Perform a periodic application task. This function gets
     *          called every five seconds as a result of the SBP_PERIODIC_EVT
     *          OSAL event. In this example, the value of the third
     *          characteristic in the SimpleGATTProfile service is retrieved
     *          from the profile, and then copied into the value of the
     *          the fourth characteristic.
     *
     * @param   none
     *
     * @return  none
     */
    static void performPeriodicTask( void )
    {
      uint8 valueToCopy;
      uint8 stat;
    
      // Call to retrieve the value of the third characteristic in the profile
      stat = SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &valueToCopy);
    
      if( stat == SUCCESS )
      {
        /*
         * Call to set that value of the fourth characteristic in the profile. Note
         * that if notifications of the fourth characteristic have been enabled by
         * a GATT client device, then a notification will be sent every time this
         * function is called.
         */
        SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof(uint8), &valueToCopy);
      }
    }
    
    /*********************************************************************
     * @fn      simpleProfileChangeCB
     *
     * @brief   Callback from SimpleBLEProfile indicating a value change
     *
     * @param   paramID - parameter ID of the value that was changed.
     *
     * @return  none
     */
    static void simpleProfileChangeCB( uint8 paramID )
    {
      uint8 newValue;
    
      switch( paramID )
      {
        case SIMPLEPROFILE_CHAR1:
          SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
    
          #if (defined HAL_LCD) && (HAL_LCD == TRUE)
            HalLcdWriteStringValue( "Char 1:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
          #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
          break;
    
        case SIMPLEPROFILE_CHAR3:
          SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR3, &newValue );
    
          #if (defined HAL_LCD) && (HAL_LCD == TRUE)
            HalLcdWriteStringValue( "Char 3:", (uint16)(newValue), 10,  HAL_LCD_LINE_3 );
          #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
          break;
    
        default:
          // should not reach here!
          break;
      }
    }
    
    #if (defined HAL_LCD) && (HAL_LCD == TRUE)
    /*********************************************************************
     * @fn      bdAddr2Str
     *
     * @brief   Convert Bluetooth address to string. Only needed when
     *          LCD display is used.
     *
     * @return  none
     */
    char *bdAddr2Str( uint8 *pAddr )
    {
      uint8       i;
      char        hex[] = "0123456789ABCDEF";
      static char str[B_ADDR_STR_LEN];
      char        *pStr = str;
    
      *pStr++ = '0';
      *pStr++ = 'x';
    
      // Start from end of addr
      pAddr += B_ADDR_LEN;
    
      for ( i = B_ADDR_LEN; i > 0; i-- )
      {
        *pStr++ = hex[*--pAddr >> 4];
        *pStr++ = hex[*pAddr & 0x0F];
      }
    
      *pStr = 0;
    
      return str;
    }
    #endif // (defined HAL_LCD) && (HAL_LCD == TRUE)
    
     /* @fn      simpleBLECentralRssiCB
     *
     * @brief   RSSI callback.
     *
     * @param   connHandle - connection handle
     * @param   rssi - RSSI
     *
     * @return  none
     */
    static void simpleBLECentralRssiCB( uint16 connHandle, int8 rssi )
    {
       //do something if desired
    }
    
    /*********************************************************************
     * @fn      simpleBLECentralEventCB
     *
     * @brief   Central event callback function.
     *
     * @param   pEvent - pointer to event structure
     *
     * @return  none
     */
    static uint8 simpleBLECentralEventCB( gapCentralRoleEvent_t *pEvent )
    {
      uint8 i, dummy;  
      
      switch ( pEvent->gap.opcode )
      {
        
          case GAP_DEVICE_INFO_EVENT:
          {
            dummy++;
            dummy++; //put breakpoint here
          }
          break;
          
        case GAP_DEVICE_DISCOVERY_EVENT:
          {
            uint8 simpleBLEScanning = 0;
            // discovery complete
            simpleBLEScanning = FALSE;
            dummy++; //put breakpoint here
          }
          break;
          
      case GAP_LINK_ESTABLISHED_EVENT:
        {
          if ( pEvent->gap.hdr.status == SUCCESS )
          {          
            for(i=0; i<MAX_CONNECTIONS;i++)
            {
              //Check against addresses we want to connect to
              if(deviceList[i].addr[5] == pEvent->linkCmpl.devAddr[5])
              {
                deviceList[i].state = BLE_STATE_CONNECTED;
                deviceList[i].connHandle = pEvent->linkCmpl.connectionHandle;
              }
            }
          }        
        }
        break;
        
      case GAP_LINK_TERMINATED_EVENT:
        {
          for(i=0; i<MAX_CONNECTIONS;i++)
          {
            //Check against addresses we want to connect to
            if(deviceList[i].connHandle == pEvent->linkTerminate.connectionHandle)
            {
              deviceList[i].state = BLE_STATE_IDLE;
            }
          }     
        }
        break;
      }
      return SUCCESS;
    }
    
    
    /*********************************************************************
     * @fn      performPeriodicTask
     *
     * @brief   Perform a periodic application task. This function gets
     *          called every five seconds as a result of the SBP_PERIODIC_EVT
     *          OSAL event. In this example, the value of the third 
     *          characteristic in the SimpleGATTProfile service is retrieved
     *          from the profile, and then copied into the value of the
     *          the fourth characteristic.
     *
     * @param   none
     *
     * @return  none
     */
    static void periodicCentralTask( void )
    {
      uint8 numDevices=1;
      uint8 i, status;
      
      //turn all LED's off
      HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF ); 
      
      if(deviceRole == ROLE_CENTRAL)
      {
        //Calc number of connections
        for(i=0;i<MAX_CONNECTIONS;i++)
        {
          if(deviceList[i].state == BLE_STATE_CONNECTED)
          {
            numDevices = numDevices + 1;
          }
        }
      }
      
      switch (masterSlave_State)
      {
      case BLE_STATE_CONNECTING:
      case BLE_STATE_CONNECTED:
        
        if(deviceRole == ROLE_CENTRAL)
        {
         if(simpleBLEScanning == FALSE)
         {
           simpleBLEScanning = TRUE;
           status = GAPCentralRole_StartDiscovery(DEVDISC_MODE_ALL, TRUE, FALSE);
           if(status != SUCCESS)
           {
             simpleBLEScanning = FALSE;
           }
           
         }
    //      //cancel current link request - only give each fob 1 sec to complete connection
    //      GAPCentralRole_TerminateLink( 0XFFFE );
    //      
    //      GAPCentralRole_EstablishLink( DEFAULT_LINK_HIGH_DUTY_CYCLE,
    //                                   DEFAULT_LINK_WHITE_LIST,
    //                                   ADDRTYPE_PUBLIC, deviceList[currentDevice].addr );
        }                
        //green
        //HalLedBlink (1, 3, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME); 
        osal_pwrmgr_device( PWRMGR_BATTERY );
        HalLedBlink (1, numDevices, 50, 500);
        osal_pwrmgr_device( PWRMGR_ALWAYS_ON );
        break;
    
      case BLE_STATE_DISCONNECTING:
      case BLE_STATE_IDLE:
        if(deviceRole == ROLE_CENTRAL)
        {
          //are we finished disconnecting
          if(numDevices == 1)
          {  
            // set new role to peripheral           
            deviceRole = ROLE_PERIPHERAL;
            
            masterSlave_State = BLE_STATE_ADVERTISING;
            
            //restet to peripheral and advertise
            osal_start_timerEx( simpleBLETaskId, SBP_START_DEVICE_EVT, 100 );
          }
          else  //continue to terminate links
          {
            if(deviceList[currentDevice].state == BLE_STATE_CONNECTED)
            {
              //terminate link
              GAPCentralRole_TerminateLink(deviceList[currentDevice].connHandle);
            }
          }
          
          //red
          HalLedBlink (2, 2, 10, 100);
        }
        break;
        
      case BLE_STATE_ADVERTISING: 
        // HalLedBlink (2, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);    
        HalLedBlink (2, 4, 10, 100);
        break;
        
      default:
        break;
      }
      
      currentDevice = currentDevice +1;
      
      if(currentDevice >2)
        currentDevice = 0;
    }
    
    /*********************************************************************
    *********************************************************************/
    
    Sure. attaching my source. let me know if this works.

    Best wishes