PSOC 4 BLE : central not connecting to peripheral. central scans peripherals properly, but dont connect.

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

cross mob
Anonymous
Not applicable

I have two cy8ckit-042-ble and I am trying to connect my 1st BLE kit configured as GAP role: central and GATT Client with a 2nd BLE kit configured as GAP role: peripheral and GATT server. It can generate event
CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT: I can fetch address of peer device. STOP scanning using CyBle_GapcStopScan().
As a result following event is generated CYBLE_EVT_GAPC_SCAN_START_STOP: When I call  res = CyBle_GapcConnectDevice(&peerAddr); value in res is CYBLE_ERROR_OK, but not generating CYBLE_EVT_GAP_DEVICE_CONNECTED. I get HCI error.
CYBLE_HCI_COMMAND_DISALLOWED_ERROR.

   

*****Is thereany specific way to call : CyBle_GapcConnectDevice((const CYBLE_GAP_BD_ADDR_T* )&add);

   

i have called this API wen event  generated is CYBLE_EVT_GAPC_SCAN_START_STOP on calling CyBle_GapcStopScan().

   

code for central is as follows:

   

*************************

   

#include "project.h"

   

void ble(uint32 eventCode, void *eventParam);

   

CYBLE_GATTC_WRITE_REQ_T writeReqParam;

   

static volatile CYBLE_API_RESULT_T res;

   

static CYBLE_GAP_BD_ADDR_T add;

   

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:

   

          CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

   

break;

   

case CYBLE_EVT_GAPC_SCAN_PROGRESS_RESULT:

   

         // call api to vew n list scanned device, and save the address of device

   

        // this API working fine and shows scanned devices and generates

   

        // CYBLE_EVT_GAPC_SCAN_START_STOP event.

   

v_workerBLE_slaveSelectionHandler((CYBLE_GAPC_ADV_REPORT_T *)eventParam);

   

break;

   

case CYBLE_EVT_GAPC_SCAN_START_STOP:

   

{

   

// **** error section. not generating next event. CYBLE_EVT_GAP_DEVICE_CONNECTED

   

// API - CyBle_GapcConnectDevice((const CYBLE_GAP_BD_ADDR_T* )&add); generates  

   

//  CYBLE_EVT_HCI_STATUS event , which is error. res variable shows all OK-no error.

   

if(u8slaveFound)

   

{

   

//peerAddr.type = 0u;

   

// connect to the device

   

// ** error API- THIS API GENERATES HCI ERROR

   

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 

   

break;

   

case CYBLE_EVT_HCI_STATUS:

   

resolveErr(eventParam);

   

break;

   

default:

   

CyBle_GapcStartScan(CYBLE_SCANNING_FAST);

   

break;

   

}

   

}

   

void v_workerBLE_slaveSelectionHandler(CYBLE_GAPC_ADV_REPORT_T* eventParam)

   

{

   

uint8 newDevice = 0u, device = 0u;

   

uint8 i;

   

uint8 adStructPtr = 0u;

   

uint8 adTypePtr = 0u;

   

uint8 nextPtr = 0u;

   

// copy peer device address- address of device which we want to connect to

   

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

   

add.type = eventParam->eventType;

   

{

   

// convert little endian to big endian as for now

   

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

   

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

   

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

   

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

   

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

   

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

   

u8slaveFound = 1u;

   

CyBle_GapcStopScan();

   

}

   

}

   

void resolveErr(CYBLE_HCI_ERROR_T *eventParam)

   

{

   

// see error code- CYBLE_HCI_COMMAND_DISALLOWED_ERROR in my case

   

volatile uint8 err = *eventParam;

   

}

0 Likes
1 Solution
Anonymous
Not applicable

 Dalip,
 

   

In your project, the device address mapping  was not in the correct order. I just corrected it and was able to connect to a slave device. 

   

Please make the corrections as given below.

   

------------------------------------
                add1.bdAddr[5] = add[5];
                add1.bdAddr[4] = add[4];
                add1.bdAddr[3] = add[3];
                add1.bdAddr[2] = add[2];
                add1.bdAddr[1] = add[1];
                add1.bdAddr[0] = add[0];
                add1.type = type;
                //  connect to the device
              //  res = CyBle_GapcConnectDevice((const CYBLE_GAP_BD_ADDR_T* )&add);

   

     res = CyBle_GapcConnectDevice(&add1);

   

---------------------------------------------     

   

 

   

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[0];
        add[1] = eventParam->peerBdAddr[1];
        add[2] = eventParam->peerBdAddr[2];
        add[3] = eventParam->peerBdAddr[3];
        add[4] = eventParam->peerBdAddr[4];
        add[5] = eventParam->peerBdAddr[5];
        
        type = eventParam->eventType;
     ---------------------------------------------------   
       

View solution in original post

0 Likes
4 Replies
himam_31
Employee
Employee
50 likes received 25 likes received 10 likes received

Hello,

   

Can you please attach the archived version of Cenral and Peripheral project, so that we will have a clear idea on the BLE configurations.

   

Thanks,

   

Hima

0 Likes
Anonymous
Not applicable

Hi Hima,

   

please find attached, the archives.

   

Thanks,

   

Dalip.

0 Likes
Anonymous
Not applicable

 Dalip,
 

   

In your project, the device address mapping  was not in the correct order. I just corrected it and was able to connect to a slave device. 

   

Please make the corrections as given below.

   

------------------------------------
                add1.bdAddr[5] = add[5];
                add1.bdAddr[4] = add[4];
                add1.bdAddr[3] = add[3];
                add1.bdAddr[2] = add[2];
                add1.bdAddr[1] = add[1];
                add1.bdAddr[0] = add[0];
                add1.type = type;
                //  connect to the device
              //  res = CyBle_GapcConnectDevice((const CYBLE_GAP_BD_ADDR_T* )&add);

   

     res = CyBle_GapcConnectDevice(&add1);

   

---------------------------------------------     

   

 

   

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[0];
        add[1] = eventParam->peerBdAddr[1];
        add[2] = eventParam->peerBdAddr[2];
        add[3] = eventParam->peerBdAddr[3];
        add[4] = eventParam->peerBdAddr[4];
        add[5] = eventParam->peerBdAddr[5];
        
        type = eventParam->eventType;
     ---------------------------------------------------   
       

0 Likes
Anonymous
Not applicable

I have changed endianness of device address for my local use. By mistake used the same for connecting.

   

its working fine now.

   

thanks for your support.

   

Regards.

   

Dalip.

0 Likes