电池通知
当提供程序包含多个组件时,向查找者通知每个组件的电池电量很有用。例如,当入耳式耳机充电盒打开时,“搜寻者”需要了解每个耳机的电池和充电盒本身的电池情况。
为此,提供商可以在广告中添加额外的信息,这些信息基于通告:何时不可发现中所述的快速配对帐号数据构建而成。
除帐号数据外,提供商还应包含一个用于指定电池电量值的额外字段。该数据包应包含以下内容:
八位字节 | 数据类型 | 说明 | 值 | 是否必需? |
---|---|---|---|---|
0 | uint8 |
标志 | 0x00 (所有位保留供将来使用) |
强制 |
1 - 秒 | 帐号关键数据 | 强制 | ||
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 - 当提供程序希望探索器在界面中显示电池电量值指示时使用;
- 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];
}
为阻止跟踪,提供程序不应始终在广告中包含原始电池数据。而是可以在连接到搜寻器时通过 RFCOMM 发送,请参阅消息流:设备信息。