如何让Nrf connect、EFR connect直接显示特征值数据及其单位

效果如图:app直接显示了我的温度,并且有两位小数,还有温度单位。这是怎么做到的呢?

这次我们仍以TLS8258为例,当然如果是其他蓝牙芯片,配置方式也是大差不差,规则一样的。

#define GATT_CHARACTERISTIC_TYPE_TEMPERATURE (0x2B11) //(0x2A6E)只有2Bytes.(0x2B11) 4Bytes
static const u16 my_debug_ServiceUUID = 0xFFF0;

static const u16 my_debug_test_temperature_characteristic_uuid = GATT_CHARACTERISTIC_TYPE_TEMPERATURE;
static const u16 my_debug_test_temperature_characteristic_value_presentation_format_uuid = GATT_UUID_CHAR_PRESENT_FORMAT;
static const u8 my_debug_test_temperature_declaration[5] = {
	CHAR_PROP_READ | CHAR_PROP_NOTIFY,
	U16_LO(DebugTestTemperature_CharacteristicValueDeclarationHandle),U16_HI(DebugTestTemperature_CharacteristicValueDeclarationHandle),
	U16_LO(GATT_CHARACTERISTIC_TYPE_TEMPERATURE),U16_HI(GATT_CHARACTERISTIC_TYPE_TEMPERATURE)
};
static const u8 debug_test_temperature_characteristic_value_presentation_format[7]=
{
	0x10,//int32_t
	0xFE,//exponent
	0x2F,//unit LSB   2F:℃; AC:℉; 05:K
	0x27,//unit MSB
	0x01,//bluetooth BIG
	0x00,//none
	0x00//none
};
static const u8 debug_test_temperature_characteristic_user_description[] =
{
	'T','e','m','p','e','r','a','t','u','r','e','-','1'
};
_attribute_data_retention_ static u8 debug_test_temperature_client_characteristic_configuration[2] = {0,0};
_attribute_data_retention_ static u8 debug_test_temperature_characteristic_value[4] = {0x01,0x02,0x03,0x04};//LSB<--->MSB
ble_sts_t server_send_debug_test_temperature_to_client(int32_t temperature)
{
	debug_test_temperature_characteristic_value[0] = (temperature>>0)&0xFF;
	debug_test_temperature_characteristic_value[1] = (temperature>>8)&0xFF;
	debug_test_temperature_characteristic_value[2] = (temperature>>16)&0xFF;
	debug_test_temperature_characteristic_value[3] = (temperature>>24)&0xFF;
	if(blc_ll_getCurrentState() == BLS_LINK_STATE_ADV)
	{
		return 0xFF;
	}
	return blc_gatt_pushHandleValueNotify(BLS_CONN_HANDLE,
										  DebugTestTemperature_CharacteristicValueDeclarationHandle,
										  &debug_test_temperature_characteristic_value[0],
										  sizeof(debug_test_temperature_characteristic_value)
										  );
}


//characteristic 2
static const u16 my_debug_test_temperature2_characteristic_uuid = 0xFFF1;
static const u16 my_debug_test_temperature2_characteristic_value_presentation_format_uuid = GATT_UUID_CHAR_PRESENT_FORMAT;
static const u8 my_debug_test_temperature2_declaration[5] = {
	CHAR_PROP_READ | CHAR_PROP_NOTIFY,
	U16_LO(DebugTestTemperature2_CharacteristicValueDeclarationHandle),U16_HI(DebugTestTemperature2_CharacteristicValueDeclarationHandle),
	U16_LO(GATT_CHARACTERISTIC_TYPE_TEMPERATURE),U16_HI(GATT_CHARACTERISTIC_TYPE_TEMPERATURE)
};
static const u8 debug_test_temperature2_characteristic_value_presentation_format[7]=
{
	0x10,//int32_t
	0xFE,//exponent
	0x2F,//unit LSB   2F:℃; AC:℉; 05:K
	0x27,//unit MSB
	0x01,//bluetooth BIG
	0x00,//none
	0x00//none
};
static const u8 debug_test_temperature2_characteristic_user_description[] =
{
	'T','e','m','p','e','r','a','t','u','r','e','-','2'
};
_attribute_data_retention_ static u8 debug_test_temperature2_client_characteristic_configuration[2] = {0,0};
_attribute_data_retention_ static u8 debug_test_temperature2_characteristic_value[4] = {0x01,0x02,0x03,0x04};//LSB<--->MSB
ble_sts_t server_send_debug_test_temperature2_to_client(int32_t temperature)
{
	debug_test_temperature2_characteristic_value[0] = (temperature>>0)&0xFF;
	debug_test_temperature2_characteristic_value[1] = (temperature>>8)&0xFF;
	debug_test_temperature2_characteristic_value[2] = (temperature>>16)&0xFF;
	debug_test_temperature2_characteristic_value[3] = (temperature>>24)&0xFF;
	if(blc_ll_getCurrentState() == BLS_LINK_STATE_ADV)
	{
		return 0xFF;
	}
	return blc_gatt_pushHandleValueNotify(BLS_CONN_HANDLE,
										  DebugTestTemperature2_CharacteristicValueDeclarationHandle,
										  &debug_test_temperature2_characteristic_value[0],
										  sizeof(debug_test_temperature2_characteristic_value)
										  );
}


static const attribute_t my_Attributes[] = {

	{ATT_END_H - 1, 0,0,0,0,0,0,0},	// total num of attribute


	// 0x0001 - 0x0007  GAP
	{7,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), 	(u8*)(&my_gapServiceUUID), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_devNameCharVal),(u8*)(&my_characterUUID), (u8*)(my_devNameCharVal), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_devName), (u8*)(&my_devNameUUID), (u8*)(my_devName), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_appearanceCharVal),(u8*)(&my_characterUUID), (u8*)(my_appearanceCharVal), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof (my_appearance), (u8*)(&my_appearanceUUID), 	(u8*)(&my_appearance), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_periConnParamCharVal),(u8*)(&my_characterUUID), (u8*)(my_periConnParamCharVal), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof (my_periConnParameters),(u8*)(&my_periConnParamUUID), 	(u8*)(&my_periConnParameters), 0, 0},


	// 0x0008 - 0x000B GATT
	{4,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), 	(u8*)(&my_gattServiceUUID), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_serviceChangeCharVal),(u8*)(&my_characterUUID), 		(u8*)(my_serviceChangeCharVal), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof (serviceChangeVal), (u8*)(&serviceChangeUUID), 	(u8*)(&serviceChangeVal), 0, 0},
	{0,ATT_PERMISSIONS_RDWR,2,sizeof (serviceChangeCCC),(u8*)(&clientCharacterCfgUUID), (u8*)(serviceChangeCCC), 0, 0},


	// Battery Service /
	// 0x002A - 0x002D
	{4,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), 	(u8*)(&my_batServiceUUID), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_batCharVal),(u8*)(&my_characterUUID), (u8*)(my_batCharVal), 0, 0},				//prop
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_batVal),(u8*)(&my_batCharUUID), 	(u8*)(my_batVal), 0, 0},	//value
	{0,ATT_PERMISSIONS_RDWR,2,sizeof(batteryValueInCCC),(u8*)(&clientCharacterCfgUUID), 	(u8*)(batteryValueInCCC), 0, 0},	//value

#if (BLE_OTA_SERVER_ENABLE)
	// OTA /
	// 0x002E - 0x0032
	{5,ATT_PERMISSIONS_READ, 2,16,(u8*)(&my_primaryServiceUUID), 	(u8*)(&my_OtaServiceUUID), 0, 0},
	{0,ATT_PERMISSIONS_READ, 2, sizeof(my_OtaCharVal),(u8*)(&my_characterUUID), (u8*)(my_OtaCharVal), 0, 0},				//prop
	{0,ATT_PERMISSIONS_RDWR,16,sizeof(my_OtaData),(u8*)(&my_OtaUUID),	(&my_OtaData), &otaWrite, NULL},				//value
	{0,ATT_PERMISSIONS_RDWR,2,sizeof(otaDataCCC),(u8*)(&clientCharacterCfgUUID), 	(u8*)(otaDataCCC), 0, 0},				//value
	{0,ATT_PERMISSIONS_READ, 2,sizeof (my_OtaName),(u8*)(&userdesc_UUID), (u8*)(my_OtaName), 0, 0},
#endif

#if(DEBUG_INTERFACE_TO_GATT)
	{11,ATT_PERMISSIONS_READ,2,2,(u8*)(&my_primaryServiceUUID), 	(u8*)(&my_debug_ServiceUUID), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(my_debug_test_temperature_declaration),(u8*)(&my_characterUUID),(u8*)(my_debug_test_temperature_declaration), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature_characteristic_value),(u8*)(&my_debug_test_temperature_characteristic_uuid), (u8*)(debug_test_temperature_characteristic_value), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature_characteristic_value_presentation_format), (u8*)(&my_debug_test_temperature_characteristic_value_presentation_format_uuid), (u8*)(&debug_test_temperature_characteristic_value_presentation_format), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature_characteristic_user_description), (u8*)(&userdesc_UUID), (u8*)(&debug_test_temperature_characteristic_user_description), 0, 0},
	{0,ATT_PERMISSIONS_RDWR,2,sizeof(debug_test_temperature_client_characteristic_configuration),(u8*)(&clientCharacterCfgUUID), (u8*)(debug_test_temperature_client_characteristic_configuration), 0, 0},

	{0,ATT_PERMISSIONS_READ,2,sizeof(my_debug_test_temperature2_declaration),(u8*)(&my_characterUUID),(u8*)(my_debug_test_temperature2_declaration), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature2_characteristic_value),(u8*)(&my_debug_test_temperature2_characteristic_uuid), (u8*)(debug_test_temperature2_characteristic_value), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature2_characteristic_value_presentation_format), (u8*)(&my_debug_test_temperature2_characteristic_value_presentation_format_uuid), (u8*)(&debug_test_temperature2_characteristic_value_presentation_format), 0, 0},
	{0,ATT_PERMISSIONS_READ,2,sizeof(debug_test_temperature2_characteristic_user_description), (u8*)(&userdesc_UUID), (u8*)(&debug_test_temperature2_characteristic_user_description), 0, 0},
	{0,ATT_PERMISSIONS_RDWR,2,sizeof(debug_test_temperature2_client_characteristic_configuration),(u8*)(&clientCharacterCfgUUID), (u8*)(debug_test_temperature2_client_characteristic_configuration), 0, 0},
#endif
};

disclaimer:

本 文 仅 代 表 作 者 个 人 观 点 限 制 在 修 改 日 期 前 , 不 代 表 现 在 观 点 。 其 原 创 性 以 及 文 中 陈 述 文 字 和 内 容 未 经 学 术 机 构 或 标 准 组 织 证 实 , 对 本 文 以 及 其 中 全 部 或 者 部 分 内 容 、 文 字 的 真 实 性 、 完 整 生 及 时 性 本 人 不 作 任 何 保 证 或 承 诺 , 请 读 者 仅 作 参 考 , 并 请 自 行 核 实 相 关 内 容 。 仅 供 学 习 参 考 ! 如 有 侵 权 或 交 流 , 请 邮 件 flourishinggarden@outlook.com  , 谢 谢 !

copyright:

引 清 注 明 出 处 , 转 载 或 者 引 用 需 注 明 " 转 自 〔 或 引 用 自 〕 FlourishingMind" 字 样 。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值