Skip to main content
PCong.1
Associate III
March 21, 2022
Question

How to have a READABLE Characteristic to a BlueNRG-M2SP

  • March 21, 2022
  • 7 replies
  • 3332 views

I try to read the data from BlueNRG-M2SP on an android application. For that with my STM32 I send to the UART a "u". On the application, I scan the services and Characteristic and I only find a write Characteristic but no read Characteristic, and I can't find in the documentation (https://www.st.com/resource/en/programming_manual/dm00294449-bluenrg-1-bluenrg-2-ble-stack-v2-x-programming-guidelines-stmicroelectronics.pdf ) how to have a read Characteristic on the custome service.0693W00000KdNpAQAV.png 

i use the code exemple BLE chat Server that can find on the BlueNRG-1 Navigator

0693W00000KdNolQAF.pngOn the code exemple i find some UUID but that doesn't work when i try to read data from the STM32

This topic has been closed for replies.

7 replies

Winfred LU
ST Employee
March 22, 2022

Please refer to BLE_SensorDemo sample project.

As for PM2057, please check section 3.2 Services and characteristic configuration

PCong.1
PCong.1Author
Associate III
March 22, 2022

Thanks for your reply @Winfred LU, I changed the feature property to notify and read, and added some code from the sensor demo, but I can't read the data from my STM32, and now when I try to read, it automatically disconnects.0693W00000KdWc5QAF.pngThere the code where i set my service and characteristic:

gatt_db.c

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "ble_const.h" 
#include "bluenrg1_stack.h"
#include "osal.h"
#include "app_state.h"
#include "SDK_EVAL_Config.h"
#include "chat.h"
 
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
 
uint16_t chatServHandle, TXCharHandle, RXCharHandle;
extern uint16_t connection_handle;
 
/* UUIDs */
Service_UUID_t service_uuid;
Char_UUID_t char_uuid;
 
/*******************************************************************************
* Function Name : Add_Chat_Service
* Description : Add the Chat service.
* Input : None
* Return : Status.
*******************************************************************************/
uint8_t Add_Chat_Service(void)
{
 uint8_t ret;
 
 /*
 UUIDs:
 D973F2E0-B19E-11E2-9E96-0800200C9A66
 D973F2E1-B19E-11E2-9E96-0800200C9A66
 D973F2E2-B19E-11E2-9E96-0800200C9A66
 */
 
 const uint8_t uuid[16] = {0x66,0x9a,0x0c,0x20,0x00,0x08,0x96,0x9e,0xe2,0x11,0x9e,0xb1,0xe0,0xf2,0x73,0xd9};
 const uint8_t charUuidTX[16] = {0x66,0x9a,0x0c,0x20,0x00,0x08,0x96,0x9e,0xe2,0x11,0x9e,0xb1,0xe1,0xf2,0x73,0xd9};
 const uint8_t charUuidRX[16] = {0x66,0x9a,0x0c,0x20,0x00,0x08,0x96,0x9e,0xe2,0x11,0x9e,0xb1,0xe2,0xf2,0x73,0xd9};
 
 Osal_MemCpy(&service_uuid.Service_UUID_128, uuid, 16);
 ret = aci_gatt_add_service(UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 6, &chatServHandle); 
 if (ret != BLE_STATUS_SUCCESS) goto fail; 
 
 Osal_MemCpy(&char_uuid.Char_UUID_128, charUuidTX, 16);
 ret = aci_gatt_add_char(chatServHandle, UUID_TYPE_128, &char_uuid, 20, CHAR_PROP_NOTIFY|CHAR_PROP_READ, ATTR_PERMISSION_NONE, GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP,
 16, 1, &TXCharHandle);
 if (ret != BLE_STATUS_SUCCESS) goto fail;
 
 Osal_MemCpy(&char_uuid.Char_UUID_128, charUuidRX, 16);
 ret = aci_gatt_add_char(chatServHandle, UUID_TYPE_128, &char_uuid, 20, CHAR_PROP_WRITE|CHAR_PROP_WRITE_WITHOUT_RESP, ATTR_PERMISSION_NONE, GATT_NOTIFY_ATTRIBUTE_WRITE,
 16, 1, &RXCharHandle);
 if (ret != BLE_STATUS_SUCCESS) goto fail;
 
 printf("Chat Service added.\nTX Char Handle %04X, RX Char Handle %04X\n", TXCharHandle, RXCharHandle);
 return BLE_STATUS_SUCCESS; 
 
fail:
 printf("Error while adding Chat service.\n");
 return BLE_STATUS_ERROR ;
}
 
/*******************************************************************************
* Function Name : Attribute_Modified_CB
* Description : Callback when RX/TX attribute handle is modified.
* Input : Handle of the characteristic modified. Handle of the attribute
* modified and data written
* Return : None.
*******************************************************************************/
void Attribute_Modified_CB(uint16_t handle, uint16_t data_length, uint8_t *att_data)
{
 if(handle == RXCharHandle + 1)
 {
 for(int i = 0; i < data_length; i++)
 printf("%c", att_data[i]);
 }
 else if(handle == TXCharHandle + 2)
 { 
 if(att_data[0] == 0x01)
 APP_FLAG_SET(NOTIFICATIONS_ENABLED);
 }
}
 
tBleStatus Free_Fall_Notification(void){
	uint8_t val;
	tBleStatus ret;
	
	val = 0x01;
	ret = aci_gatt_update_char_value_ext(connection_handle, chatServHandle, TXCharHandle, 1, 6, 0, 6, &val);
	if(ret != BLE_STATUS_SUCCESS){
		PRINTF("Error while updating Free Fall characteristic: 0x%02X\n",ret);
		return BLE_STATUS_ERROR;
	}
	return BLE_STATUS_SUCCESS;
}
 
tBleStatus SensorUpdate(void){
	 uint8_t buff[6];
	 tBleStatus ret;
 
	 ret = aci_gatt_update_char_value_ext(connection_handle, chatServHandle , TXCharHandle, 1, 6, 0, 6, buff);
	 if (ret != BLE_STATUS_SUCCESS) {
		PRINTF("Error while updating Sensor characteristic: 0x%02X\n", ret);
		return BLE_STATUS_ERROR;
	}
	 return BLE_STATUS_SUCCESS;
}
 
 

Winfred LU
ST Employee
March 23, 2022

Please check for the return code and events on the BlueNRG-M2 side, which will provide more clues.

My guess would be that you set the GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP, but in the application you did not give permission to the read command.

Try to use GATT_DONT_NOTIFY_EVENTS instead.

PCong.1
PCong.1Author
Associate III
March 23, 2022

Ok now, the application doesn't disconnect when I read but I'm still not able to read what my STM32 sends, I get a long 0x00.0693W00000Kdgu4QAB.png 

Winfred LU
ST Employee
March 23, 2022

I tested with STEVAL-IDB007 which worked properly.

If you would like to read whatever was written previously, it is supposed to write to and read from the same attribute with both read and write permission.

PCong.1
PCong.1Author
Associate III
March 23, 2022

I don't want to read what has been read before, my STM32 will continuously send data and I only want to read this data that the BLE receives in real time to display it in my application.@Winfred LU (ST Employee)​ 

PCong.1
PCong.1Author
Associate III
March 23, 2022

I don't want to read what has been read before, my STM32 will continuously send data and I only want to read this data that the BLE receives in real time to display it in my application.@Winfred LU​ 

Winfred LU
ST Employee
March 25, 2022

Please clarify your environment, if i understand correctly :

  1. BlueNRG-M2SP is connected to STM32 (host) as a network coprocessor over UART interface
  2. The STM32 + BlueNRG-M2SP acts as a GATT server that will be connected with an Android phone
  3. in the STM32 application, you'd like to modify some attribute thru UART/ACI, that will be read remotely by that connected Android phone

If my understanding is correct, please modify the attribute with aci_gatt_update_char_value_ext() ACI, as suggested in the sample project. In your posted code, SensorUpdate() function.

PCong.1
PCong.1Author
Associate III
March 25, 2022

my environment is simple the next

0693W00000Lvj1HQAR.png 

I managed to get the "u" on the notification (the solution was to add the end character CR+LF).

The problem is that the BLE doesn't want to read an int without an end character and I can't do like a printf because it won't read the variable a and will return 0 and the end character

int a = 10; 
HAL_UART_Transmit_IT(&hlpuart1, (a, '\r\n') , sizeof((a, '\r\n')));

 0693W00000LvjhSQAR.png And what should I change in the attribute?

tBleStatus SensorUpdate(void){
	 uint8_t buff[6];
	 tBleStatus ret;
 
	 ret = aci_gatt_update_char_value_ext(connection_handle, chatServHandle , TXCharHandle, 1, 6, 0, 6, &buff);
	 if (ret != BLE_STATUS_SUCCESS) {
		PRINTF("Error while updating Sensor characteristic: 0x%02X\n", ret);
		return BLE_STATUS_ERROR;
	}
	 return BLE_STATUS_SUCCESS;
}

Winfred LU
ST Employee
March 28, 2022

That environment looks the same as a STEVAL-IDB008 EVB acts as a Chat server, where the BlueNRG-2 is connected to the onboard STM32L thru UART pin8 and pin11.

With an EVB, the STM32 serves as an USB to UART bridge, to receive messages from the USB virtual terminal and forward to BlueNRG-2 with UART.

The project works for an EVB. Confirm that '\r\n' is required without modification.

So, i suppose if you did not modify UART codes for BlueNRG-2, the issue could be on your Host uC.

Could you doubly check if the string was really transmitted over UART? by sniffing UART for example.

Regarding to your codes:

int a = 10; 
HAL_UART_Transmit_IT(&hlpuart1, (a, '\r\n') , sizeof((a, '\r\n')));

For the 2nd and 3rd parameter given to HAL_UART_Transmit_IT, I am not quite understand the syntax that joins an integer a and a string (or a character) '\r\n' ?

Regarding to the question,

> And what should I change in the attribute?

Change to whatever is sent from your Host uC. Isn't this what you were trying to do?

PCong.1
PCong.1Author
Associate III
March 28, 2022

I checked 4 times to make sure the string was transmitted and the string is sent.

About the code, I tried to do like the printf function which can turn an int into a string and put end characters

int a = 10;
printf("%d\r\n", a);

, but I found how to do it, I created a function int_to_char.

Regarding to the question,

> And what should I change in the attribute?

>>Change to whatever is sent from your Host uC. Isn't this what you were trying to do?

Yes, but I don't know what parameter I need to change inside. 

aci_gatt_update_char_value_ext(connection_handle, chatServHandle , TXCharHandle, 1, 6, 0, 6, &buff);

abach.1
Associate
May 15, 2023

hello, did it work finally, if so can you please share the code with me to know how it works

Many thanks

Anas