Clearing a characteristic before writing new data to it

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

cross mob
Anonymous
Not applicable

Is there anyway to clear data in a characteristic before writing new data to it? I'm running into and issue where if I write 5 bytes of data to a characteristic and then 3 bytes of data, the first 3 bytes get overwritten like they should, but the remaining 2 bytes from the first write are still there. Is there anyway to clear previous data before writing new data to a characteristic?

0 Likes
1 Solution
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

swelan

UINT8 loop;

for(loop = 0; loop < SIZE_OF_CHAR; loop++)

     db_pdu.pdu[loop] = 0;

Jacob

View solution in original post

0 Likes
7 Replies
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

swelan

UINT8 loop;

for(loop = 0; loop < SIZE_OF_CHAR; loop++)

     db_pdu.pdu[loop] = 0;

Jacob

0 Likes
Anonymous
Not applicable

jakewtorres

What function would I put that in?

0 Likes
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

Put it immediately before your writeHandle() call, whatever function that may be in.

Jacob

0 Likes
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

Please ignore the last post, you need to clear it immediately prior to writing to it (which happens to be before writeHandle().

UINT8 loop;

for(loop = 0; loop < SIZE_OF_CHAR; loop++)

     db_pdu.pdu[loop] = 0;

db_pdu.pdu[5] = data;

bleprofile_WriteHandle(....

Jacob

0 Likes
Anonymous
Not applicable

jakewtorres

Right, I just don't know what function gets called in this case. I can put it in the store_in_... functions which call bleprofile_writeHandle at the end, but those functions don't get called when the android app changes the characteristic.

0 Likes
JacobT_81
Employee
Employee
250 replies posted 100 replies posted 50 replies posted

If you're doing this from the Android side, it will be easier to overwrite it from there. At the end of your 3 byte write, add two bytes zeroed out.

If this isn't an option and you need to work within the firmware, you need to register for a callback for when the client writes to your GATT database. You can then find out the length of the write, compare it to the size of the characteristic, and zero out the left over spaces. See hello_sensor for an example of registering for a callback to a client write. This callback will be passed the necessary parameters to execute what I described above.

Jacob

0 Likes
Anonymous
Not applicable

Thanks for all your help! I have access to the android side so I'll just zero the rest out from that side.

0 Likes