電池通知
如果供應器包含多個元件,建議通知每個元件的電池電量。舉例來說,假設有人開啟耳機充電盒,搜尋器需要瞭解左右耳機和充電盒的電量。
為達成此目的,提供方可在廣告中加入其他資訊,以「廣告:不可供偵測」一節所述的快速配對帳戶資料為基礎。
除了帳戶資料外,供應商還應加入額外欄位指定電池值。封包應包含下列項目:
八位元 | 資料類型 | 說明 | 值 | 必要? |
---|---|---|---|---|
0 | uint8 |
旗幟 | 0x00 (所有位元保留供日後使用) |
必填 |
1 至 秒 | 帳戶金鑰資料 | 必填 | ||
s + 1 | uint8 |
電池電量與 0bLLLLTTT
|
0BLLTTT
|
選用 |
s + 2、s + 3、s + 4 | uint8 |
電池值 0bSVVVVVV
|
0bSVVVVV
|
選用 |
為避免竄改,則應稍微修改上述帳戶金鑰資料,在廣告中加入電池值時加入電池資訊。一般來說,建立帳戶鍵篩選器時,將帳戶金鑰與鹽字串結合後會產生 V 值。如果也宣告了電池資訊,則必須以下列方式建構 V 值:
- 產生 V 值,其中:
- 前 16 個位元組是 K。
- 下一個位元組是 Salt。
- 剩餘的位元組是電池資訊 (從 s + 1 到 s + 4,包括上表的長度和類型位元組)。
如上方的電池長度和類型欄位所述,類型可以是 0b0011
或 0b0100
。
- 0b0011 - 當提供者希望搜尋器在電池值 UI 顯示指示時使用;
- 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];
}
為防止追蹤,提供方不得一律在廣告中納入原始電池資料。在連線至 Seeker 時,可以改透過 RFCOMM 傳送。請參閱「訊息串流:裝置資訊」一文。