当前时间服务(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));
2567

被折叠的 条评论
为什么被折叠?



