蓝牙BLE当前时间服务

当前时间服务(Current Time Service)

概念

BLE(蓝牙低功耗)中的Current Time Service (CTS) 是一个标准的GATT服务,用于同步设备之间的当前时间。

CTS定义了一系列特性和描述符,允许客户端设备(如智能手机)从服务器设备(如BLE模块)获取当前时间,并且服务器设备可以在时间发生变化时发送通知。

服务定义

  • 必须主服务
  • UUID=0x1805
  • 一台设备只能有一个服务实例

本地时间服务框图

服务特征

当前时间特征

  • 必选
  • UUID=0x2A2B
  • 必须支持可读和通知
  • 可写(可选)
数据结构体定义
/* current time characteristic */
struct date_time {
	/** year.
	 *  valid range : 1582 - 9999
	 *  0 means year not known
	 */
	uint16_t year;
	/** month.
	 *  valid range : 1(January) - 12(December)
	 *  0 means month not known
	 */
	uint8_t month;
	/** day.
	 *  valid range : 1 - 31
	 *  0 means day not known
	 */
	uint8_t day;
	/** hours.
	 *  valid range : 0 - 23
	 */
	uint8_t hours;
	/** minutes.
	 *  valid range : 0 - 59
	 */
	uint8_t minutes;
	/** seconds.
	 *  valid range : 0 - 59
	 */
	uint8_t seconds;
}__attribute__((packed));

struct day_date_time {
	struct date_time d_t;
	/** day_of_week.
	  valid range : 1(Monday) - 7(Sunday)
	 *  0 means day of week not known
	 */
	uint8_t day_of_week;
}__attribute__((packed));

struct exact_time_256 {
	struct day_date_time d_d_t;
	/** fractions_256.
	 *  the number of 1 / 256 fractions of second
	 *  valid range : 0 - 255
	 */
	uint8_t fractions_256;
}__attribute__((packed));

struct cts_curr_time {
	struct exact_time_256 et_256;
	/* This field represents reason(s) for adjusting time. */
	uint8_t adjust_reason;
}__attribute__((packed));
通知机制
/* adjust reason masks */
#define MANUAL_TIME_UPDATE_MASK (1 << 0)
#define EXTERNAL_REFERENCE_TIME_UPDATE_MASK (1 << 1)
#define CHANGE_OF_TIME_ZONE_MASK (1 << 2)
#define CHANGE_OF_DST_MASK (1 << 3)
具体描述
  • 参考时间更新导致的时间变化:
    • 条件1:新的时间信息与更新前的时间相差超过1分钟。
    • 条件2:参考时间更新是由客户端设备引起的(例如,通过与其他服务交互)。
    • 默认条件:在上次通知后的15分钟内,不发送新的时间更新通知。
  • 其他事件(如时区变化、夏令时调整或用户手动调整):
    • 立即发送通知:无论时间差是多少,也不管时间更新的原因是什么,都应立即发送通知。

本地时间信息特征

  • 可选
  • UUID=0x2A0F
  • 必须支持可读
  • 可写(可选)

数据结构体定义

/* local time information characteristic */
struct cts_local_time_info {
	/* This field represents the offset from UTC in number of 15-minute increments.
	 * Valid range from -48 to +56.
	 * A value of -128 means that the time zone offset is not known.
	 * All other values are Reserved for Future Use.
	 * The offset defined in this characteristic is constant regardless of whether daylight savings is in effect.
	 */
	int8_t timezone;
	/** dst_offset.
	 *  allowed values : 0, 2, 4, 8, 255
	 */
	uint8_t dst_offset;
}__attribute__((packed));

参考时间信息特征

  • 可选
  • UUID=0x2A14
  • 必须支持可读

数据结构体定义

/* reference time information */
struct cts_reference_time_info {
	/** time_source.
	 *  valid range : 0 - 253
	 *  255 means not known
	 */
	uint8_t time_source;
	/*
	 * This field represents accuracy (drift) of time information in steps of 1/8 of a second (125ms) compared to a reference time source.
	 * Valid range from 0 to 253 (0s to 31.625s).
	 * A value of 254 means drift is larger than 31.625s.
	 * A value of 255 means drift is unknown.
	 */
	uint8_t time_accuracy;
	/** days_since_update.
	 *  valid range : 0 - 254
	 *  A value of 255 is used when the time span is greater than or equal to 255 days
	 */
	uint8_t days_since_update;
	/** hours_since_update.
	 *  valid range : 0 - 23
	 *  A value of 255 is used when the time span is greater than or equal to 255 days
	 */
	uint8_t hours_since_update;
}__attribute__((packed));
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值