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.

CC2541: How to send the string command with CC2541 via bluetooth

Part Number: CC2541

Hi ,

how to send string command from CC2541 ? I want to send something like this:

(1,0,0,0,0,0,0,0,0) via Bluetooth.

Any idea how to do it. 

What I tried so far is

static void simpleProfileChangeCB( uint16 paramID )
{
   char Data[11]; 
   switch( paramID )
    {
       case SIMPLEPROFILE_CHAR1:
       SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &Data);
      printf("COMMAND: %s'\n'",Data);

   }

}

Any idea ?

  • When I tried to send this command(1,0,0,0,0,0,0,0,0). In print I got 508

  • Hey Yatin,

    Data is usually sent by either writing to a characteristics and sending notifications whenever the characteristic is updated.

    I highly recommend taking a look at our Simplelink Academy modules to understand the basics. These modules are specific to our newer generation parts (CC26x2 in this case) but should provide enough context. Then, read through the CC2540 Bluetooth Low Energy Software Developer's Guide for information specific to this device.

  • I suppose you can use GATT_WriteCharValue to write to a specific GATT characteristics to send a string.

  • okay. Let me test this one!

  • any sample code on this. How to use this command and on which file do I need to use it. 

  • Hi  and ,

    static void simpleProfileChangeCB( uint16 paramID)
    {
        char Data[11];
        switch( paramID )

    {
        case SIMPLEPROFILE_CHAR1:
        attWriteReq_t wrreq; 

       wrreq.pValue= Data;
       wrreq.sig = 0;
       wrreq.cmd = 0;
       GATT_WriteCharValue(paramID, &wrreq, SIMPLEPROFILE_CHAR1);
       printf("COMMAND: %s'\n'",Data);

    }

    }

    This will work ?? 

    I want to send (1,0,0,0,0,0,0,0,0) as string 

    Regards,

    Yatin Baluja 

  • I suppose you have GATT service/characteristic implemented on BLE peripheral so you should use GATT_WriteCharValue on BLE central to write characteristic to BLE peripheral.

  • I am only using BLEperipheral....I know App will send the string to the BLEPeripheral. It is possible to set GATT service in BLEPeripheral like simpleProfileChangeCB ?? I want to send the string from the App and BLEPeripheral will receive it and perform the operation but I don't know how to set the GATT service like simpleProfile so that I can receive the string command on CC2541

  • One more thing, it is possible to set the time in milliseconds in CC2541? If yes, how ?

  • 1. When APP write a characteristic and BLE peripheral receives it, peripheral would trigger simpleProfile_WriteAttrCB and your peripheral application can get what it writes in simpleProfile_WriteAttrCB.

    2. You can use osal_start_timerEx to create timer event in millisecond interval.

  • Hi

    I changed the length of the CHAR1 to 20 bytes and I send the command from the nrfConnect App and printed the command. I am attaching the screenshot what I'm getting after sending (1,0,0,0,0,0,0,0).

    I'm also attaching the  simpleProfile_WriteAttrCB code.

    /*********************************************************************
    * @fn simpleProfile_WriteAttrCB
    *
    * @brief Validate attribute data prior to a write operation
    *
    * @param connHandle - connection message was received on
    * @param pAttr - pointer to attribute
    * @param pValue - pointer to data to be written
    * @param len - length of data
    * @param offset - offset of the first octet to be written
    * @param method - type of write message
    *
    * @return SUCCESS, blePending or Failure
    */
    static bStatus_t simpleProfile_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
    uint8 *pValue, uint8 len, uint16 offset,
    uint8 method )
    {
    bStatus_t status = SUCCESS;
    uint8 notifyApp = 0xEE ;

    // If attribute permissions require authorization to write, return error
    if ( gattPermitAuthorWrite( pAttr->permissions ) )
    {
    // Insufficient authorization
    return ( ATT_ERR_INSUFFICIENT_AUTHOR );
    }

    if ( pAttr->type.len == ATT_BT_UUID_SIZE )
    {
    // 16-bit UUID
    uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
    switch ( uuid )
    {
    case SIMPLEPROFILE_CHAR1_UUID:
    case SIMPLEPROFILE_CHAR3_UUID:
    case SIMPLEPROFILE_CHAR4_UUID:
    case SIMPLEPROFILE_CHAR5_UUID:

    /*
    //Validate the value
    // Make sure it's not a blob oper
    if ( offset == 0 )
    {
    if ( len != 1 )
    {
    status = ATT_ERR_INVALID_VALUE_SIZE;
    }
    }
    else
    {
    status = ATT_ERR_ATTR_NOT_LONG;
    }
    */
    //Write the value
    if ( status == SUCCESS )
    {
    uint8 *pCurValue = (uint8 *)pAttr->pValue;
    /**pCurValue = pValue[0];
    *pCurValue++;
    *pCurValue = pValue[1];*/
    memcpy(pCurValue,pValue,len);
    bledatalen = len;

    if( pAttr->pValue == &simpleProfileChar1 )
    {
    notifyApp = SIMPLEPROFILE_CHAR1;
    }
    if( pAttr->pValue == &simpleProfileChar3 )
    {
    notifyApp = SIMPLEPROFILE_CHAR3;
    }
    if( pAttr->pValue == &simpleProfileChar4 )
    {
    notifyApp = SIMPLEPROFILE_CHAR4;
    }

    if( pAttr->pValue == &simpleProfileChar5 )
    {
    notifyApp = SIMPLEPROFILE_CHAR5;
    }
    }

    break;

    case GATT_CLIENT_CHAR_CFG_UUID:
    status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
    offset, GATT_CLIENT_CFG_NOTIFY );
    break;

    default:
    // Should never get here! (characteristics 2 and 4 do not have write permissions)
    status = ATT_ERR_ATTR_NOT_FOUND;
    break;
    }
    }
    else
    {
    // 128-bit UUID
    status = ATT_ERR_INVALID_HANDLE;
    }

    // If a charactersitic value changed then callback function to notify application of change
    if ( (notifyApp != 0xEE) && simpleProfile_AppCBs && simpleProfile_AppCBs->pfnSimpleProfileChange )
    {
    simpleProfile_AppCBs->pfnSimpleProfileChange( notifyApp );
    }

    return ( status );
    }

    Regards,

    Yatin Baluja 

  • Do you check if your simpleProfile_WriteAttrCB is triggered and if yes, what value is written over the air?

  • Hi ,

    If I'm sending only "1" command then simpleProfile_WriteAttrCB is triggering and providing me output as I'm using serial print to print the values but if I'm sending the command as string like 100000 then it is not printing anything.  

  • What do you mean "sending the command as string like 100000"? What is the data type of your written characteristic?

  • Hi ,

    /*********************************************************************
    * @fn simpleProfileChangeCB
    *
    * @brief Callback from SimpleBLEProfile indicating a value changesimpleProfileChangeCB
    *
    * @param paramID - parameter ID of the value that was changed.
    *
    * @return none
    */
    static void simpleProfileChangeCB( uint16 paramID )
    {
      uint8 newValue[15];
      switch( paramID )
    {
      case SIMPLEPROFILE_CHAR1:
      SimpleProfile_GetParameter( SIMPLEPROFILE_CHAR1, &newValue );
      printf("Command: %x\n",newValue);
    break;

    }

    }

    This is the code I'm testing and from the App I am testing sending "TEXT".

    So, for example if I'm sending "1" from nrfConnect APP to Characteristics 1, In the serial print output is printing as 1fb but when I'm sending string like 110000000 it is not printing anything. So, basically 110000000 are the status of the LED's for example 11 means LED1 and LED2 are ON and others are OFF. But this data will be send directly by the App and the firmware will pass this status to CC2541 using Bluetooth. 

    Regards,

    Yatin Baluja 

  • I am not familiar with nrfConnect APP, you might need to use sniffer to check what nrfConnect APP actually send out over the air when you said you send "1" from nrfConnect APP to Characteristics 1.

  • Hi ,

    How to send 20 bytes data with CC2541 on one Bluetooth UUID? Do I need array or string ? but how I can do using SimpleBLEPeripheral example.

    Regards,

    Yatin Baluja

  • I suppose you can use an array and GATT_Notification to send the 20 bytes.

  • This will work with the BLEPeripheral like on the server side ? I am not using BLECentral which is basically client

  • Basically, BLE peripheral need to have a GATT service to send notification.