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.

CC2640: CC2640 Central Read/Write from the Peripheral.

Part Number: CC2640

Hi all,

This is Anil. I am working on the CC2640 Central part. In  this one i want to check the data which is sent by peripheral by using the characteristics. 

Here i am getting the particular peripheral Service UUID, Characteristics handle, Service handle.  

Now i need a data which is sent by Peripheral.

Central Code:

static void SimpleBLECentral_processGATTDiscEvent(gattMsgEvent_t *pMsg)
{
static char c=0;
static char j=0;

if (discState == BLE_DISC_STATE_MTU)
{
// MTU size response received, discover simple service
if (pMsg->method == ATT_EXCHANGE_MTU_RSP)
{
uint8_t uuid[ATT_BT_UUID_SIZE] = { LO_UINT16(SIMPLEPROFILE_SERV_UUID),
HI_UINT16(SIMPLEPROFILE_SERV_UUID) };

// Just in case we're using the default MTU size (23 octets)
Display_print1(dispHandle, 4, 0, "MTU Size: %d", ATT_MTU_SIZE);

discState = BLE_DISC_STATE_SVC;

// Discovery simple service
VOID GATT_DiscPrimaryServiceByUUID(connHandle, uuid, ATT_BT_UUID_SIZE,
selfEntity);
memcpy( UUID_TEMP , uuid, 2); // Getting Service UUID Here
}
}
else if (discState == BLE_DISC_STATE_SVC)
{
// Service found, store handles
if (pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP &&
pMsg->msg.findByTypeValueRsp.numInfo > 0)
{
start = svcStartHdl = ATT_ATTR_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0); //Service Handle
end = svcEndHdl = ATT_GRP_END_HANDLE(pMsg->msg.findByTypeValueRsp.pHandlesInfo, 0);
}

// If procedure complete
if (((pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP) &&
(pMsg->hdr.status == bleProcedureComplete)) ||
(pMsg->method == ATT_ERROR_RSP))
{
if (svcStartHdl != 0)
{
attReadByTypeReq_t req;

// Discover characteristic
discState = BLE_DISC_STATE_CHAR;

TEMP[c++] = req.startHandle = svcStartHdl;
TEMP[c++] = req.endHandle = svcEndHdl;
TEMP[c++] = req.type.len = ATT_BT_UUID_SIZE;
// TEMP[3] = req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR1_UUID);
// TEMP[4] = req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR1_UUID);

TEMP[c++] = req.type.uuid[0] = LO_UINT16(SIMPLEPROFILE_CHAR5_UUID);
TEMP[c++] = req.type.uuid[1] = HI_UINT16(SIMPLEPROFILE_CHAR5_UUID); //Characteristics ID here

VOID GATT_ReadUsingCharUUID(connHandle, &req, selfEntity);

// GATT_ReadCharValue(connHandle, &req, selfEntity);

// memcpy(GAP_DATA,pMsg->msg.readRsp.pValue,pMsg->msg.readRsp.len);

}
}
}
else if (discState == BLE_DISC_STATE_CHAR)
{
// Characteristic found, store handle
if ((pMsg->method == ATT_READ_BY_TYPE_RSP) &&
(pMsg->msg.readByTypeRsp.numPairs > 0))
{
temp[j] = charHdl = BUILD_UINT16(pMsg->msg.readByTypeRsp.pDataList[0],
pMsg->msg.readByTypeRsp.pDataList[1]); //Characteristics Handle

Display_print0(dispHandle, 2, 0, "Simple Svc Found");
procedureInProgress = FALSE;

By this one i am getting all the details whichever i mentioned above. now i am trying to read or write data using CC2640 Central.

How to do it.?

Thanks 

Anil D.

  • Hi Anil,

    You can use GATT_ReadCharValue() to read the characteristiv value. You can read the full API description in the Software Developer's Guide: http://www.ti.com/lit/swru393

    The two main things to remember are to set up the attReadReq correctly, and to make a ATT_READ_RSP event case to catch the read value.
  • Hi Marie,

    if (events & SBC_ATT_READ_EVT)
    {
    events &= ~SBC_ATT_READ_EVT;
    Util_stopClock( &ATT_Read_Clock);

    attReadReq_t req;

    req.handle = CHAR_HANDLE; // Which handle i found for reading
    status = GATT_ReadCharValue(connHandle, &req, selfEntity);
    Util_startClock(&ATT_Read_Clock);
    }

    See this i created the event for reading. and in that one i wrote the function " GATT_ReadCharValue(connHandle, &req, selfEntity); ".
    But still i am not getting hits on the "GATT_MSG_EVENT" for checking the "ATT_FIND_INFO_RSP or ATT_ERROR_RSP".

    " set up the attReadReq correctly" what is means? After Above mentioned code i started clock for 3 sec.
    Is there any wrong in this coding. Please suggest me something.

    One more point i need to mention here is that I am not going with KEY pressing. I am going with the Advertising data, If data matched then Establish a link with a particular peripheral. 

     

    Thanks.
    Anil D.

  • Hi Marie,

    Really thanks for giving the suggestion. i got the data from the peripheral with your solution. I created one event for it and i am able to get the data in the "
    if ((pMsg->method == ATT_READ_RSP) ||
    ((pMsg->method == ATT_ERROR_RSP) &&
    (pMsg->msg.errorRsp.reqOpcode == ATT_READ_REQ)))
    {
    if (pMsg->method == ATT_ERROR_RSP)
    {
    Display_print1(dispHandle, 4, 0, "Read Error %d", pMsg->msg.errorRsp.errCode);
    }
    else
    {
    // After a successful read, display the read value
    Display_print1(dispHandle, 4, 0, "Read rsp: %d", pMsg->msg.readRsp.pValue[0]);
    memcpy(GAP_DATA,pMsg->msg.readRsp.pValue,pMsg->msg.readRsp.len);
    }

    "
    GAP_DATA i got the result.

    thanks .
    Anil D.
  • Hello marie,

    i wrote the one more event for Writing data into Peripheral.

    Code:

    if (events & SBC_ATT_WRITE_EVT)
    {
    unsigned char *RSSI_Update="#5,5,23-01-2018-16:00:00;";
    events &= ~SBC_ATT_WRITE_EVT;
    Util_stopClock( &ATT_Write_Clock);


    uint8_t status;
    // Do a write
    attWriteReq_t req;

    req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, 1, NULL);
    if ( req.pValue != NULL )
    {
    req.handle = 0x0024;
    req.len = 25;
    req.pValue = RSSI_Update;
    req.sig = 0;
    req.cmd = 0;

    status = GATT_WriteCharValue(connHandle, &req, selfEntity);
    if ( status != SUCCESS )
    {
    GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
    }
    }
    else
    {
    status = bleMemAllocError;
    }

    is this things are correct?

    Thanks 

    Anil D.