Sending data between two BLE devices

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

cross mob
Anonymous
Not applicable

I working to send a simple Int between two BLE devices. I've been able to run the UART example in which one device is setup as a central and the other as peripheral. All works well there. This allows you to type in a serial window and it sends it to the other device. 

   

What I'm trying to do now is a simple Int programmatically between the two devices. I am able to get the data back and forth however it always comes out weird on the device that I'm sending it to. 

   

Here is how I send a piece of data from my peripheral device: 

   

void Send_Event_Code_To_Sensor(int eventCode)
{
    CYBLE_API_RESULT_T              bleApiResult;
    CYBLE_GATTS_HANDLE_VALUE_NTF_T     tempHandle5;
   
    tempHandle5.attrHandle = CYBLE_SERVER_UART_SERVER_UART_TX_DATA_CHAR_HANDLE;
    tempHandle5.value.val = (uint8*)eventCode;
    tempHandle5.value.len = sizeof(eventCode); 
    
     do
     {
        bleApiResult = CyBle_GattsNotification(cyBle_connHandle, &tempHandle5);
        CyBle_ProcessEvents();
     }
     while((CYBLE_ERROR_OK != bleApiResult)  && (CYBLE_STATE_CONNECTED == cyBle_state));
}

   

 

   

Here is how I receive it on the central device: 

   

void HandleUartRxTraffic(CYBLE_GATTC_HANDLE_VALUE_NTF_PARAM_T *uartRxDataNotification)
{
    if(uartRxDataNotification->handleValPair.attrHandle == txCharHandle)
    {
        unsigned int paramVal = *uartRxDataNotification->handleValPair.value.val;
    }
}

   

The problem is that when I receive the data it's always something weird. 

   

For instance, if I try to send the number 1 from my peripheral like this: 

   

Send_Event_Code_To_Sensor(1);

   

It decodes as 64 on my central device. What am I doing wrong here? Is it because I'm sending it as a pointer here: 

   

tempHandle5.value.val = (uint8*)eventCode;

   

Maybe? 

0 Likes
1 Solution
Anonymous
Not applicable

Hey!

   

The issue seems to be with the line: tempHandle5.value.val = (uint8*)eventCode;

   

Change it to tempHandle5.value.val = (uint8*)&eventCode;

   

tempHandle5.value.val is of type (uint8 *), so it is necessary that you pass the address and not the value. By making the change that I mentioned, you will be passing the address rather than the value. This should do the trick!

View solution in original post

0 Likes
6 Replies
Anonymous
Not applicable

Hello,

   

 Knowledge Basic:

   

int eventCode;

   

sizeof(eventCode)== 4;

   

sizeof(eventCode)/sizeof(int) == 1;

   

So you can do like this:

   

tempHandle5.value.len = sizeof(eventCode); ----->Wrong

   

tempHandle5.value.len = sizeof(eventCode)/sizeof(int); ----->Right

   

Or

   

void Send_Event_Code_To_Sensor(uint8_t eventCode)

0 Likes
Anonymous
Not applicable

By doing this I am still getting the same results a my original post. My thought is that the problem is either in the decoding of the value on my receiving side, or the fact that I'm trying to send a pointer? 

0 Likes
Anonymous
Not applicable

Maybe you can send your project to me,let me check it.This should be a very simple question.

0 Likes
Anonymous
Not applicable

Hey Helon, I really appreciate the help and sorry I can't send you the project. It's for a client and wrapped under a bunch of NDA and legal stuff so pretty sure I'd get in trouble 😞 . Anyways, adding the & in was the trick! 

0 Likes
Anonymous
Not applicable

Hey!

   

The issue seems to be with the line: tempHandle5.value.val = (uint8*)eventCode;

   

Change it to tempHandle5.value.val = (uint8*)&eventCode;

   

tempHandle5.value.val is of type (uint8 *), so it is necessary that you pass the address and not the value. By making the change that I mentioned, you will be passing the address rather than the value. This should do the trick!

0 Likes
Anonymous
Not applicable

Ah that's did it!!! Thank you very much for the help 🙂 

0 Likes