Enabling BLE notifications from code doesn't work.

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

cross mob
EdHa_4455331
Level 5
Level 5
25 replies posted 25 sign-ins 10 replies posted

I am trying to enable BLE notifications for a parameter from code (instead of from a paired client). I have read through some other posts on this topic and it seems like I'm doing what is needed to do this. Here is my code:

void SetPulseLevel( uint8_t newLevel )
{
cy_stc_ble_gatt_handle_value_pair_t gattHandle;
uint8 notificationEnable[1];
cy_en_ble_api_result_t status;
cy_en_ble_gatt_err_code_t ret;

UNUSED( status );
UNUSED( ret );

// Force notifications to be enabled for this characteristic.
notificationEnable[0] = 1;
gattHandle.attrHandle = CY_BLE_POC_SETTINGS_PULSE_LEVEL_CHAR_HANDLE;
gattHandle.value.len = 1;
gattHandle.value.val = notificationEnable;
ret = Cy_BLE_GATTS_WriteAttributeValuePeer( &appConnHandle, &gattHandle );

// Push out a BLE notification.
gattHandle.attrHandle = CY_BLE_POC_SETTINGS_PULSE_LEVEL_CHAR_HANDLE;
gattHandle.value.len = sizeof( newLevel );
gattHandle.value.val = &newLevel;
status = Cy_BLE_GATTS_SendNotification( &appConnHandle, &gattHandle );

}

ret is CY_BLE_GATT_ERR_NONE (which sounds right), but status is always CY_BLE_ERROR_NTF_DISABLED.

And I do have notifications enabled in the BLE GUI:

EdHa_4455331_0-1648063517289.png

EdHa_4455331_1-1648063653599.png

 

Any help will be gratefully appreciated.

Thanks,

Ed H.

0 Likes
1 Solution

My bad. I bungled the search for _DESC_ defines last time. You solution did the trick.

Very grateful,

Ed H.

View solution in original post

0 Likes
4 Replies
Yugandhar
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 5 likes given

Hello,

Client is responsible to change CCCD state and it is not recommended to enable the notifications at server side. If you need for some specific reason to do this, you can use Cy_BLE_GATTS_WriteAttributeValueCCCD() api for doing this.
Please refer to the below code.

*******************************************************************************************************
cy_stc_ble_gatt_handle_value_pair_t handle_value;
uint8 notificationEnable[1];

notificationEnable[0] = 1;
handle_value.attrHandle = CY_BLE_DRO_XAXIS_CLIENT_CHARACTERISTIC_CONFIGURATION_DESC_HANDLE;
handle_value.value.len = 1;
handle_value.value.val = notificationEnable;

cy_stc_ble_gatts_db_attr_val_info_t dbAttrVal =
{
.handleValuePair = handle_value,
.connHandle = cy_ble_connHandle[0],
.flags = CY_BLE_GATT_DB_LOCALLY_INITIATED,
.offset = 0u
};

gatt_error = Cy_BLE_GATTS_WriteAttributeValueCCCD(&dbAttrVal);

*******************************************************************************************************

Thanks,
P Yugandhar.

0 Likes
EdHa_4455331
Level 5
Level 5
25 replies posted 25 sign-ins 10 replies posted

My auto-generated code for the characteristic I need to enable does not define a  _DESC_ constant for that characteristic. Did you mean _DECL_?

Thanks, 

Ed H.

0 Likes

Hello,

You will get an attribute handle for your Client Characteristic Configuration descriptor. In PSoC Creator, Go to Generated source-> PSoC6-> BLE-> BLE_Config.h file you can find the list of attribute handles of the defined Custom Services and their characteristics.

Thanks,
P Yugandhar.

0 Likes

My bad. I bungled the search for _DESC_ defines last time. You solution did the trick.

Very grateful,

Ed H.

0 Likes