How do I register to receive a callback when a characteristic is read by the client?

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

cross mob
Anonymous
Not applicable

SDK function legattdb_regWriteHandleCb() can be used to register to receive a callback when a characteristic is written to by the client.

Is there a similar function that can be called to register to receive a callback when a characteristic is read by the client?

If not, how can the application be made aware that a characteristic has been read by the client?

1 Solution
VictorZ_46
Employee
Employee
5 comments on blog 25 sign-ins 250 likes received

Unfortunately there is no read callback.  but you can do it yourself.  In the Create function register to process all GATT messages

lel2cap_regConnLessHandler(LEL2CAP_CID_LE_ATT, my_l2capHandler);

add a handler

void my_l2capHandler(LEL2CAP_HDR *l2capHdr)

{

LEATT_PDU_HDR *gattPtr = (LEATT_PDU_HDR*) (l2capHdr +1);

LEATT_PDU_READ_REQ_HDR *p_read_read = (LEATT_PDU_READ_REQ_HDR *)gattPtr;

int opcode = LEATT_OPCODE_MASK & gattPtr->attrCode ;

int handle = p_read_read->attrHandle;

ble_trace2("gatt handle:%04x opcode:%d", handle, opcode);

if ((opcode == LEATT_OPCODE_READ_REQ) && (handle == "handle of your attribute"))

{

  // do whatever you need

}

// call the standard handler where peer will grab value of the attribute from the GATT DB

leatt_l2capHandler(l2capHdr);

}

View solution in original post

4 Replies
VictorZ_46
Employee
Employee
5 comments on blog 25 sign-ins 250 likes received

Unfortunately there is no read callback.  but you can do it yourself.  In the Create function register to process all GATT messages

lel2cap_regConnLessHandler(LEL2CAP_CID_LE_ATT, my_l2capHandler);

add a handler

void my_l2capHandler(LEL2CAP_HDR *l2capHdr)

{

LEATT_PDU_HDR *gattPtr = (LEATT_PDU_HDR*) (l2capHdr +1);

LEATT_PDU_READ_REQ_HDR *p_read_read = (LEATT_PDU_READ_REQ_HDR *)gattPtr;

int opcode = LEATT_OPCODE_MASK & gattPtr->attrCode ;

int handle = p_read_read->attrHandle;

ble_trace2("gatt handle:%04x opcode:%d", handle, opcode);

if ((opcode == LEATT_OPCODE_READ_REQ) && (handle == "handle of your attribute"))

{

  // do whatever you need

}

// call the standard handler where peer will grab value of the attribute from the GATT DB

leatt_l2capHandler(l2capHdr);

}

Anonymous
Not applicable

Thanks, this solution appears to be working well.

BrRe_4481111
Level 1
Level 1
5 replies posted First reply posted Welcome!

Is this still true after all these years? No simple registration to get a callback when a client reads a characteristic? That is pretty important for supporting basic services like the Current Time Service and plenty of others that are now appearing in BT-SIG service/profile development. Reading the current time isn't the only dynamic feature a client may need to read!

0 Likes
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi @BrRe_4481111 ,

This post is several years old. Our development SDK has grown since then. Please check the latest examples of the device of your interest. We have callback events to indicate when the peripheral receives a read request from the peer device. Please start a new discussion if you have further questions for better reach.

Regards,
Bragadeesh
0 Likes