connection between two CYBLE-2241100-00 (central and peripheral)

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

cross mob
umg_4380326
Level 3
Level 3
First like received

Hi,

I am trying for assest tracking.I want to establish connection between a peripheral(device A) and central (device B)cyble2241100-00 modules.

I am able to advertise , scan the device get the advertising report and then displaying the rssi value in the event CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:

Now i have to try and establish connection between cyble2241100 modules and then get that data  from the peripheral and switch the central device to peripheral mode and then send that data over to another device.

1) how do i establish connection bwtween device A and device B ?

2) how can i switch from central mode to peripheral mode in device B?

3) how to send the for Device B after switching mode?

Is this part ble event correct?is this how connection can be established between a peripheral and central?

switch(event)

    {

          case CYBLE_EVT_STACK_ON:

          CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

          break;

          case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

          CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

          printf("Disconnection reason is %x\r\n", (uint8)(*disconnectionreason));

           break;

    

          case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:

          scanreport = (CYBLE_GAPC_ADV_REPORT_T *) eventParam;

          if(scanreport->peerBdAddr[0]==0xD0 && scanreport->peerBdAddr[1]==0x37 && scanreport->peerBdAddr[2]==0x84&& scanreport->peerBdAddr[3]==0x00 &&                scanreport->peerBdAddr[4]==0xa0 && scanreport- >peerBdAddr[5]==0x00)

             {

                     UART_UartPutString("CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT - Peripheral found\r\n");

                     CyBle_GapcStopScan();

             }

            break;

         case CYBLE_EVT_GAPC_SCAN_START_STOP:     

         CyBle_GapcConnectDevice(&peerdevice);

         printf("RSSI value is %d \r\n", scanreport->rssi);

        break;

  }

Also i found a code

#include "project.h"

void ble(uint32 eventCode, void *eventParam);

uint8     type =0;

CYBLE_GATTC_WRITE_REQ_T writeReqParam;

static volatile CYBLE_API_RESULT_T res;

//static CYBLE_GAP_BD_ADDR_T  add;

uint8     add[CYBLE_GAP_BD_ADDR_SIZE]= {0};

void v_workerBLE_slaveSelectionHandler(CYBLE_GAPC_ADV_REPORT_T* eventParam);

void resolveErr(CYBLE_HCI_ERROR_T *eventParam);

uint8 u8slaveFound;

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

     CyBle_Start(ble);

     while(1)

     {

       CyBle_ProcessEvents();

     }

}

void ble(uint32 eventCode, void *eventParam)

{

switch(eventCode)

    {

        case CYBLE_EVT_STACK_ON:

        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

      //CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);

        CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

        break;

     // Master events

        case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:

            v_workerBLE_slaveSelectionHandler((CYBLE_GAPC_ADV_REPORT_T *)eventParam);

           break;

        case  CYBLE_EVT_GAPC_SCAN_START_STOP:

        { 

            if(u8slaveFound)

            {

                //peerAddr.type = 0u;

                  static CYBLE_GAP_BD_ADDR_T  add1;

                add1.bdAddr[0] = add[0];

                add1.bdAddr[1] = add[1];

                add1.bdAddr[2] = add[2];

                add1.bdAddr[3] = add[3];

                add1.bdAddr[4] = add[4];

                add1.type = type;

                //  connect to the device

                 res = CyBle_GapcConnectDevice((const CYBLE_GAP_BD_ADDR_T* )&add);

            }

        } 

            break;

        case CYBLE_EVT_GAP_DEVICE_CONNECTED:

            CyBle_GattcStartDiscovery(cyBle_connHandle);

        break;

        case CYBLE_EVT_GATTC_DISCOVERY_COMPLETE:

            // do some work

            CyBle_GattcWriteCharacteristicDescriptors

            (

                cyBle_connHandle,  //CYBLE_CONN_HANDLE_T   connHandle,

                  &writeReqParam

            );

                   break;

          case CYBLE_EVT_HCI_STATUS:

            resolveErr(eventParam);

              CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

            break;

    }

}

void v_workerBLE_slaveSelectionHandler(CYBLE_GAPC_ADV_REPORT_T* eventParam)

{

   // memcpy(add.bdAddr, eventParam->peerBdAddr, CYBLE_GAP_BD_ADDR_SIZE);

   // add.type = eventParam->eventType;

//    if( (eventParam->peerBdAddr[5] == 0x00) && (eventParam->peerBdAddr[4] == 0xA0) && (eventParam->peerBdAddr[3] == 0x50))

    {

        // handle little endian as of now

        add[0] = eventParam->peerBdAddr[5];

        add[1] = eventParam->peerBdAddr[4];

        add[2] = eventParam->peerBdAddr[3];

        add[3] = eventParam->peerBdAddr[2];

        add[4] = eventParam->peerBdAddr[1];

        add[5] = eventParam->peerBdAddr[0];

        type = eventParam->eventType;

        u8slaveFound = 1u;

       CyBle_GapcStopScan();

    }

    //ptr = eventParam->data;

}

void resolveErr(CYBLE_HCI_ERROR_T *eventParam)

{

    volatile uint8 err = *eventParam;

}

/* [] END OF FILE */

to this code i  am getting error undefined reference to  cyble_gattcstart discovery and a warning implicit  declaration  of function cyble_gattcstart discovery

What i do ?How should i proceed to establish connection?

Thanks

Uma

0 Likes
1 Solution
Yugandhar
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 5 likes given

Hello Uma,

1) How do I establish connection between device A and device B ?

Ans: In the scan progress result event(CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT), check for the peer device address or advertisement data/scan response data with the event parameter "CYBLE_GAPC_ADV_REPORT_T" . Once the device is found enable a flag and stop scanning using CyBle_GapcStopScan(). In the scan stop event(CYBLE_EVT_GAPC_SCAN_START_STOP), check the flag status and connect to the peer device using CyBle_GapcConnectDevice() API. Please refer to the Day013_Client_Server project in the GitHub for more information on connection establishment between central and peripheral.

2) How can I switch from central mode to peripheral mode in device B?

Ans: Please refer to the Day012_Central_Peripheral project where it uses the PSoC4 BLE in both Central and Peripheral GAP role. In this example project, when device is acting as Central, it can scan and connect to peer device which is advertising. When acting as peripheral it can advertise and other Central can connect to the PSoC4 BLE. The project is controlled based on the commands from UART, accordingly changing PSoC4 BLE role. PSoC 4 BLE can only connect maintain one connection at a time.

3) How to send the for Device B after switching mode?

Ans: Server uses the Notify or Indicate property for sending the data to the client. Please refer to the Day_024_Throughput project(GATT_Notification projects) in which when the devices are connected, the server will sends continuous notification data to the central device.

Please let me know if you have any questions.

Thanks,

P Yugandhar.

View solution in original post

0 Likes
6 Replies
Yugandhar
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 5 likes given

Hello Uma,

1) How do I establish connection between device A and device B ?

Ans: In the scan progress result event(CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT), check for the peer device address or advertisement data/scan response data with the event parameter "CYBLE_GAPC_ADV_REPORT_T" . Once the device is found enable a flag and stop scanning using CyBle_GapcStopScan(). In the scan stop event(CYBLE_EVT_GAPC_SCAN_START_STOP), check the flag status and connect to the peer device using CyBle_GapcConnectDevice() API. Please refer to the Day013_Client_Server project in the GitHub for more information on connection establishment between central and peripheral.

2) How can I switch from central mode to peripheral mode in device B?

Ans: Please refer to the Day012_Central_Peripheral project where it uses the PSoC4 BLE in both Central and Peripheral GAP role. In this example project, when device is acting as Central, it can scan and connect to peer device which is advertising. When acting as peripheral it can advertise and other Central can connect to the PSoC4 BLE. The project is controlled based on the commands from UART, accordingly changing PSoC4 BLE role. PSoC 4 BLE can only connect maintain one connection at a time.

3) How to send the for Device B after switching mode?

Ans: Server uses the Notify or Indicate property for sending the data to the client. Please refer to the Day_024_Throughput project(GATT_Notification projects) in which when the devices are connected, the server will sends continuous notification data to the central device.

Please let me know if you have any questions.

Thanks,

P Yugandhar.

0 Likes

Hi Yugandhar

Thankyou for the suggestion.

i tried the 1) also considering the example you mentioned.in the generated ble files , in ble eventhandler.h
for pheripheral, the cyble_connected is not highlighting why.

is that why am i not able to establish connection ?

what would i be missing?

Thanks

Uma

0 Likes
lock attach
Attachments are accessible only for community members.

Hello Uma,

Q: In the generated ble files , in ble eventhandler.h for pheripheral, the cyble_connected is not highlighting why.

Ans: Could you please send the screenshot of this portion?

In the peripheral device, please make sure that the Advertising type is set to "Connectable undirected advertising"(This option is used for general advertising of the advertising and scan response data and allows any other device to connect to this device) as shown in attached image.

Thanks,

P Yugandhar.

0 Likes

Hi

pastedImage_0.png

these are the advertising settings i have set

pastedImage_1.png

Thanks

Uma

0 Likes

hi,

can u let me knoiw the following is correct or am i missing anything?

void AppCallBack(uint32 event, void *eventParam)

    CYBLE_GAPC_ADV_REPORT_T             *advReport;

    CYBLE_GAP_BD_ADDR_T                     peerBdAddr; 

    CYBLE_API_RESULT_T                      apiResult; 

    CYBLE_CONN_HANDLE_T             connHandle;

  

    uint8 deviceDetected=0;

    uint8 i;

   

    switch (event)

    {

        case CYBLE_EVT_STACK_ON:

             CSD_Write(1);

             CPS_Write(1);

             CY_SET_XTND_REG32((void CYFAR *)(CYREG_BLE_BLESS_RF_CONFIG), 0x0331);

             CY_SET_XTND_REG32((void CYFAR *)(CYREG_SRSS_TST_DDFT_CTRL), 0x80000302);

             UART_UartPutString("BLE STACK ON:\r\n");

             CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

        break;

            

        case CYBLE_EVT_TIMEOUT:

         if( CYBLE_GAP_SCAN_TO ==*(uint16*) eventParam)

            {

                //Start Scanning again when timeout occurs

                CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

            }

        break;

           

            //This event is triggered every time a device is discovered;

        case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT: //SCAN progress result

            {

                if(advReport->peerBdAddr[0]==0x98&&  advReport->peerBdAddr[1]==0x35 && advReport->peerBdAddr[2]==0x51

                &&advReport->peerBdAddr[3]==0x00 && advReport->peerBdAddr[4]==0xa0 && advReport->peerBdAddr[5]==0x00)

 

                {

                    printf ("Found Device \r\n");

                    printf("RSSI: %d \r\n",advReport->rssi);

                   

                    for(i=0;i<CYBLE_GAP_BD_ADDR_SIZE;i++)

                    {

                        peerBdAddr.bdAddr=advReport->peerBdAddr;

                        printf("%2.2x",advReport->peerBdAddr[CYBLE_GAP_BD_ADDR_SIZE-1-i]);

                    }

                   

                    printf("\r\n");

                    deviceDetected = TRUE;

                    printf("device dected, scan stop:\r\n");

                    /*Start to scan after disconnection*/

                    CyBle_GapcStopScan();

                }

            }

        break;  

          

        case CYBLE_EVT_GAPC_SCAN_START_STOP:

               

                if(CyBle_GetState()==CYBLE_STATE_SCANNING)

                {

                    printf("Start scanning:CYBLE_STATE_SCANNING\r\n");

                }

                else if(CyBle_GetState()==CYBLE_STATE_DISCONNECTED)

                {   

                    printf("Stop scanning:CYBLE_STATE_DISCONNECTED\r\n");

                    if(deviceDetected==TRUE)

                    {

                        apiResult=CyBle_GapcConnectDevice(&peerBdAddr);

                        if(apiResult==CYBLE_ERROR_OK)

                        {

                            printf("success\r\n");

                        }

                        else

                        {

                            printf("connection failed:%x\r\n",apiResult);

                        }

                        deviceDetected=FALSE;  

                    }

                }

            break;

               

        case CYBLE_EVT_GAP_DEVICE_CONNECTED:

            

         printf("Peripheral connected. \r\n");

             /*Start to dicovery the servioes of the serve after connection*/

                CyBle_GattcStartDiscovery(cyBle_connHandle);

           

        break;       

    

           

        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

         

            printf("Peripheral Disconnected \r\n");

            /*Start to scan after disconnection*/

                CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

                printf ("Scanning\r\n");

            break;

           

        case CYBLE_EVT_GATT_CONNECT_IND:

/* When the peripheral device is connected, store the connection handle.*/

      

            connHandle = *(CYBLE_CONN_HANDLE_T *)eventParam;

break;

               

        default:           

            break;

               

    }          

           

}

case CYBLE_EVT_STACK_ON:

       

        address.type =0x00;

        checkapiresult(CyBle_GetDeviceAddress(&address));

        printf("My public address is %x %x %x :%x %x %x  \r\n", address.bdAddr[5],address.bdAddr[4],address.bdAddr[3],address.bdAddr[2],address.bdAddr[1],address.bdAddr[0] );

        address.type =0x01;

        checkapiresult(CyBle_GetDeviceAddress(&address));

        printf("My random address is %x %x %x :%x %x %x  \r\n", address.bdAddr[5],address.bdAddr[4],address.bdAddr[3],address.bdAddr[2],address.bdAddr[1],address.bdAddr[0] );

        UART_UartPutString("CYBLE_EVT_STACK_ON \r\n");

      

        checkapiresult(CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST));

      

        CSD_Write(1);

        CPS_Write(1);

        CY_SET_XTND_REG32((void CYFAR *)(CYREG_BLE_BLESS_RF_CONFIG), 0x0331);

        CY_SET_XTND_REG32((void CYFAR *)(CYREG_SRSS_TST_DDFT_CTRL), 0x80000302);

       

           

        case CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP:

       

        UART_UartPutString("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP - \r\n");

        printf("status is %d \r\n",*((uint8 *)eventParam));

       

        if (CyBle_GetState() == CYBLE_STATE_ADVERTISING)

        {

            printf("Advertising...\r\n");

        }

        if(CyBle_GetState() == CYBLE_STATE_DISCONNECTED)

        {

      

            checkapiresult(CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST));

            UART_UartPutString("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP - Peripheral continued advertising\r\n");

        }

        if(CyBle_GetState() == CYBLE_STATE_CONNECTED)

        {

            UART_UartPutString("CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP - Advertisement stopped\r\n");

        }

       

        break;

       

       

        case CYBLE_EVT_GAP_DEVICE_CONNECTED:            

                printf("Device Connected\r\n)");                                              

            break;

               

        case CYBLE_EVT_GAP_DEVICE_DISCONNECTED:

              

                CyBle_GappStartAdvertisement(CYBLE_ADVERTISING_FAST);

            break;

Thanks

Uma

0 Likes

Hello Uma,

Could you please define the pointer to a structure of type CYBLE_GAPC_ADV_REPORT_T which is returned as a event parameter in the CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT event as shown in below code.

case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT: //SCAN progress result

            {

               advReport = (CYBLE_GAPC_ADV_REPORT_T*) eventParam;

             }

Thanks,

P Yugandhar.