电池通知
当提供程序包含多个组件时,将每个组件的电池电量通知给跟踪者会很有用。例如,打开入耳式耳机充电盒时,Seeker 需要知道每个耳机的充电盒以及充电盒本身的电量。
为此,提供商可以在广告中添加其他信息,这些内容以广告:当未被发现中所述快速配对帐号数据为基础构建而成。
除了帐号数据之外,Provider 还应包含一个用于指定电池值的额外字段。数据包应包含以下内容:
八位字节 | 数据类型 | 说明 | 值 | 是否必须填写? |
---|---|---|---|---|
0 | uint8 |
标志 | 0x00 (所有位保留以供将来使用) |
强制 |
1 - s | 帐号密钥数据 | 强制 | ||
s + 1 | uint8 |
电池电量和类型 0bLLLLTTTT
|
0bLLLLTTTT
|
选填 |
s + 2、s + 3、s + 4 | uint8 |
电池值 0bSVVVVVVV
|
0bSVVVVVVV
|
选填 |
为了防止发生篡改,如果通告中包含电池电量值,则应对上述帐号密钥数据稍加修改,以包含电池信息。通常,在构建帐号密钥过滤器时,系统会将帐号密钥与盐相结合来生成值 V。相反,如果还通告了电池信息,则应按如下方式构造值 V:
- 生成一个值 V,其中:
- 前 16 个字节为 K。
- 后续字节是 Salt。
- 其余字节是电池信息(从 s + 1 到 s + 4,包括上表中的长度和类型字节)。
如上面的电池长度和类型字段中所述,类型可以是 0b0011
或 0b0100
。
- 0b0011 - 当提供程序希望 Seek 在界面中显示电池电量值时,使用此属性;
- 0b0100 - 当提供程序希望进度指示器隐藏已显示的指示项时使用。
一个常见的使用场景是,在充电盒打开时使用 0b0011
,在耳机从充电盒取出或再次关上时使用 0b0100
。
//The sample code demonstrates that the headset only reports the battery level.
#define FASTPAIR_ACCOUNT_KEY_SIZE 16
// In the sample code, the size of salt is 2 bytes.
#define SALT_SIZE 2
// 1st byte - Battery level length and type
// 2nd~4th bytes - Battery values
#define BAT_LEVEL_SIZE 3
uint8_t V[FASTPAIR_ACCOUNT_KEY_SIZE + SALT_SIZE + BAT_LEVEL_SIZE + 1] = {0};
int v_index = 0;
// The first 16 bytes are K.
uint8_t K[FASTPAIR_ACCOUNT_KEY_SIZE] = {0};
fastpair_get_account_key_by_index(keyIndex, K);
memcpy(V, K, FASTPAIR_ACCOUNT_KEY_SIZE);
v_index = v_index + FASTPAIR_ACCOUNT_KEY_SIZE;
// The next byte is the Salt.
uint8_t randomSalt = (uint8_t)rand();
V[v_index] = randomSalt;
v_index = v_index + SALT_SIZE;
// The remaining bytes are the battery information (from s + 1 to s + 4 including the length and type bytes).
uint8_t battery_level_len = 0;
uint8_t battery_level[BAT_LEVEL_SIZE] = {0};
fastpair_get_bat_level(&battery_level_len, battery_level);
// type = 0b0011 (show UI indication) or 0b0100 (hide UI indication)
V[v_index] = (battery_level_len << 4 | (is_show_ui ? 0x3 : 0x4));
v_index = v_index + 1;
for (int idx = 0; idx < battery_level_len; idx++) {
V[v_index++] = battery_level[idx];
}
为防止跟踪,Provider 应始终不在广告中包含原始电池数据。当连接到 Seek 时,可以通过 RFCOMM 发送该消息。请参阅消息流:设备信息。