電池通知

如果「供應者」包含多個元件,通知「搜尋者」每個元件的電池電量可能很有用。舉例來說,如果開啟耳機充電盒,搜尋者需要知道各個耳機和充電盒本身的電量,

為達成此目的,供應商可以在廣告中加入額外資訊,這些資訊是以「廣告:無法探索時」一節所述的快速配對帳戶資料為基礎。

除了帳戶資料,供應商還應加入額外欄位,指定電池值。資料包應包含下列項目:

八位元 資料類型 說明 是否為必要屬性?
0 uint8 旗幟 0x00
(所有位元保留供日後使用)
必填
1 - s 帳戶金鑰資料 必填
s + 1 uint8 電池電量長度和類型
0bLLLLTTTT
  • L = 電池值數量
  • T = 類型
0bLLLLTTTT
  • 長度 = 0b0011 = 3 個電池值
  • type = 0b0011 (顯示 UI 指示) 或 0b0100 (隱藏 UI 指示)
選用
s + 2s + 3s + 4 uint8 電池值
0bSVVVVVVV
  • S = 狀態 (充電中或未充電)
  • V = 值
  • 電池值應依序為左耳機 (s + 2)、右耳機 (s + 3) 和充電盒 (s + 4)。
0bSVVVVVVV
  • status = 0b1 (充電中) 或 0b0 (未充電)
  • value = 電池電量,範圍為 0 到 100%,0bS1111111 表示不明。
選用

為防止遭人竄改,如果廣告中包含電池值,上述帳戶金鑰資料應稍作修改,納入電池資訊。一般來說,建構帳戶金鑰篩選器時,系統會將帳戶金鑰與鹽值合併,產生 V 值。如果同時宣傳電池資訊,V 值應建構如下:

  1. 產生值 V,其中:
    1. 前 16 個位元組是 K
    2. 接下來的位元組是「鹽」。
    3. 其餘位元組為電池資訊 (從 s + 1s + 4,包括上表中的長度和類型位元組)。

如上方的電池長度和類型欄位所述,類型可以是 0b00110b0100

  • 0b0011 - Use when the Provider wants the Seeker to show an indication in the UI of the battery values;
  • 0b0100 - Use when the Provider wants the Seeker to hide the indication if it is already showing.

常見的用途之一是,在充電盒開啟時使用 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];
  }

為避免追蹤,供應商不應在廣告中一律加入原始電池資料,而應在連線至搜尋者時,透過訊息串流傳送這類資料,請參閱「訊息串流:裝置資訊」。