Android 裝置的門鈴裝置指南

門鈴裝置類型是使用三種特徵實作:PushAvStreamTransport,使用推送通訊協定處理音訊和影片串流傳輸;WebRtcLiveView,提供控制直播和對講功能的能力;以及 CameraSnapshot,提供快取預覽快照和即時快照擷取。

使用任何功能或嘗試更新屬性前,請務必確認裝置是否支援該屬性和指令。詳情請參閱「在Android上控制裝置」。

Home API 裝置類型 特徵 Kotlin 範例應用程式 用途

門鈴

GoogleDoorbellDevice

home.matter.6006.types.0113

一種裝置,由門外的按鈕啟動,會發出聲音和/或視覺訊號,用於引起門內人員的注意。門鈴可能提供無障礙直播、雙向對講或偵測事件。

必要特徵
     google PushAvStreamTransport
     google WebRtcLiveView

門鈴

取得裝置的基本資訊

   在 Android 範例應用程式中導入   

BasicInformation 特性包含裝置的供應商名稱、供應商 ID、產品 ID、產品名稱 (包含型號資訊) 和軟體版本等資訊:

// Get device basic information. All general information traits are on the RootNodeDevice type.
    device.type(RootNodeDevice).first().standardTraits.basicInformation?.let { basicInformation ->
        println("vendorName ${basicInformation.vendorName}")
        println("vendorId ${basicInformation.vendorId}")
        println("productId ${basicInformation.productId}")
        println("productName ${basicInformation.productName}")
        println("softwareVersion ${basicInformation.softwareVersion}")
    }

取得序號

如要取得裝置序號,請使用 ExtendedBasicInformation 特性的 GetSerialNumber 指令。以下範例會將序號儲存在名為 serialNumber 的變數中:

val basicInfo: ExtendedBasicInformation = device.getTrait(ExtendedBasicInformation)
val serialNumber = basicInfo.getSerialNumber().serialNumber

應答短訊

「應答短訊」功能可讓使用者傳送預先定義的訊息給門鈴裝置。

這項功能僅適用於門鈴裝置。合作夥伴應用程式可取得預先定義的訊息清單,並向使用者顯示這些訊息供其選擇。使用者無法編輯預先定義的訊息。

應答短訊是透過 PresetMessage 特性實作。

播放預設訊息

如要播放預設訊息,請呼叫 playPresetMessage 方法,並傳遞 availablePhraseTypes 屬性中的其中一個字串值。

import com.google.home.HomeDevice
import com.google.home.HomeException
import com.google.home.google.GoogleDoorbellDevice
import com.google.home.google.PresetMessage
import kotlinx.coroutines.flow.first

suspend fun playDoorbellPresetMessage(device: HomeDevice, phraseTypeString: String) {
    try {
        // 1. Retrieve the first snapshot of the doorbell device type
        val doorbellDevice = device.type(GoogleDoorbellDevice).first()

        // 2. Extract the PresetMessage trait
        val presetMessageTrait = doorbellDevice.trait(PresetMessage)
        if (presetMessageTrait == null) {
            println("PresetMessage trait is not supported on this doorbell device.")
            return
        }

        // 3. (Optional) Check available phrase types supported by the device
        val availablePhrases = presetMessageTrait.availablePhraseTypes
        if (availablePhrases != null) {
            val phraseTypesList = availablePhrases.map { it.phraseType }
            println("Supported quick response phrases: $phraseTypesList")
        }

        // 4. Send the playPresetMessage command to the doorbell
        presetMessageTrait.playPresetMessage(phraseType = phraseTypeString)
        println("Preset message command sent successfully.")

    } catch (e: HomeException) {
        println("Home API error occurred: ${e.message}")
    } catch (e: Exception) {
        println("Unexpected failure: ${e.message}")
    }
}

設定使用的語言

使用 LocalizationConfiguration 特性的 setActiveLocale 方法,將裝置的有效口語語言設為特定語言代碼 (例如「en_US」)。

import java.util.Locale

// Convert underscore format (en_US) to Java Locale
fun String.toLocale(): Locale = Locale.forLanguageTag(this.replace('_', '-'))

// Setting the active language
val trait: LocalizationConfiguration = device.getTrait(LocalizationConfiguration)
val selectedLocale = "en_US" // Target locale string
trait.update {
    setActiveLocale(selectedLocale)
}

取得裝置最近一次與雲端通訊的時間

如要找出裝置最近一次與雲端通訊的時間,請使用 ExtendedGeneralDiagnostics 特性的 lastContactTimestamp 屬性:

fun getLastContactTimeStamp(trait: ExtendedGeneralDiagnostics): java.time.Instant {
  val timestamp = trait.lastContactTimestamp
  return Instant.ofEpochSecond(timestamp.toLong())
}

攝影機安裝類型設定

Mount 特性包含攝影機掛架設定和狀態資訊。您可以讀取掛接狀態、偵測類型和掛接類型名稱等屬性。此外,您可以使用 Mount 特徵覆寫預設的掛接類型設定。

// Get the Mount trait
val mountTrait: Mount = device.getTrait(Mount)

// Read the current mount state and detection type
val mountState = mountTrait.mountState
val mountDetectionType = mountTrait.mountDetectionType

// Read the current mount type name
val mountTypeName = mountTrait.mountTypeName

// Update the mount type override
mountTrait.update {
  setMountTypeOverride(MountTrait.MountTypeOverrideEnum.Official)
}

檢查裝置的連線能力

系統實際上會檢查裝置類型的連線能力,因為某些裝置支援多種裝置類型。傳回的狀態是該裝置所有特性的連線狀態組合。

   在 Android 範例應用程式中導入   
val doorbellConnectivity = doorbellDevice.metadata.sourceConnectivity.connectivityState

在沒有網際網路連線的情況下,如果裝置類型混合,可能會觀察到 PARTIALLY_ONLINE 狀態。由於本機路由,Matter 標準特徵可能仍會上線,但雲端式特徵會離線。

取得裝置的 IP 位址

如要找出裝置的 IP 位址,請使用 GeneralDiagnostics 特性的 networkInterfaces 屬性。位址會以位元組陣列的形式傳回,您可以將其格式化為標準 IPv4 或 IPv6 字串:

val ipAddresses =
  trait.networkInterfaces?.flatMap { networkInterface ->
    (networkInterface.ipv4Addresses + networkInterface.ipv6Addresses).mapNotNull { bytes ->
      try {
        java.net.InetAddress.getByAddress(bytes).hostAddress
      } catch (e: java.net.UnknownHostException) {
        null
      }
    }
  } ?: emptyList()

開始直播

   在 Android 範例應用程式中導入   

如要開始直播,請將工作階段描述通訊協定 (SDP) 字串傳送至 WebRtcLiveView 特性的 startLiveView() 方法,該方法會傳回包含三個值的 WebRtcLiveViewTrait.StartLiveViewCommand.Response:

  • 工作階段的 SDP。
  • 工作階段持續時間 (以秒為單位)。
  • 工作階段 ID,可用於延長或終止工作階段。
suspend fun getWebRtcLiveViewTrait(cameraDevice: HomeDevice) {
 return cameraDevice.type(GoogleDoorbellDevice).trait(WebRtcLiveView).first {
    it?.metadata?.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE
  }

}

// Start the live view
suspend fun startCameraStream(trait: WebRtcLiveView, offerSdp: String) {
  val response = trait.startLiveView(offerSdp)
  // Response contains three fields (see below)
  return response
}
  ...

// This is used to manage the WebRTC connection
val peerConnection: RTCPeerConnection = ...

   ...

val startResponse = startCameraStream(sdp)
val answerSdp = startResponse?.answerSdp
val sessionDuration = startResponse?.liveSessionDurationSeconds
val mediaSessionId = startResponse?.mediaSessionId

peerConnection.setRemoteDescription(SessionDescription.Type.ANSWER,
                                    answerSdp)

延長直播

   在 Android 範例應用程式中導入   

直播影片有預設時間長度,超過就會失效。如要延長進行中直播的時間長度,請使用 WebRtcLiveView.extendLiveView() 方法發出延長要求:

// Assuming camera stream has just been started
suspend fun scheduleExtension(trait: WebRtcLiveView, mediaSessionId: String, liveSessionDurationSeconds: UShort ) {
  delay(liveSessionDurationSeconds - BUFFER_SECONDS * 1000)
  val response = trait.extendLiveView(mediaSessionId)
  // returns how long the session will be live for
  return response.liveSessionDurationSeconds
}

啟動及停止對講

   在 Android 範例應用程式中導入   

如要啟動對講功能,請呼叫 WebRtcLiveView 特性的 startTalkback() 方法。如要停止,請使用 stopTalkback()。

// Make sure camera stream is on
suspend fun setTalkback(isOn: Boolean, trait: WebRtcLiveView, mediaSessionId: String) {
  if(isOn) {
    trait.startTalkback(mediaSessionId)
  } else {
    trait.stopTalkback(mediaSessionId)
  }
}

啟用及停用錄製功能

如要啟用攝影機的錄影功能,請將 TransportStatusEnum.Active 傳遞至 PushAvStreamTransport 特性的 setTransportStatus() 方法。如要停用錄製功能,請傳遞 TransportStatusEnum.Inactive。在以下範例中,我們將這些呼叫包裝在單一呼叫中,並使用 Boolean 切換錄製功能:

// Start or stop recording for all connections.
suspend fun setCameraRecording(trait: PushAvStreamTransport, isOn: Boolean) {
  if(isOn) {
    trait.setTransportStatus(TransportStatusEnum.Active)
  } else {
    trait.setTransportStatus(TransportStatusEnum.Inactive)
  }
}

啟用或停用攝影機的錄影功能,等同於開啟或關閉攝影機影像。攝影機影像開啟時,就會錄影 (用於事件和相關短片)。

停用錄影功能 (攝影機影像關閉) 時:

檢查是否已啟用錄製功能

如要判斷攝影機是否啟用錄影功能,請檢查是否有任何連線處於啟用狀態。以下範例定義了兩個函式來執行這項操作:

// Get the on/off state
suspend fun onOffState(pushAvStreamTransport: PushAvStreamTransport) {
  return pushAvStreamTransport
    .currentConnections?.any { it.transportStatus == TransportStatusEnum.Active } ?: false
}

// Check if the camera's recording capability is enabled
fun PushAvStreamTransport.recordModeActive(): Boolean {
  return currentConnections?.any { it.transportStatus == TransportStatusEnum.Active } ?: false
}

另一種檢查方式是使用 findTransport() 函式搭配述詞:

// Fetch the current connections
suspend fun queryRecordModeState(trait: PushAvStreamTransport) {
  return trait.findTransport().let {
      it.transportConfigurations.any { it.transportStatus == TransportStatusEnum.Active
    }
}

擷取攝影機快照

   在 Android 範例應用程式中導入   

使用 CameraSnapshot 特性,從攝影機擷取靜態圖片快照。這類快照適用於多種情境:

  • WebRTC 直播載入時,在 UI 中做為預留位置圖塊。
  • 以靜態預覽圖片顯示,使用者互動後會升級為直播。
  • 做為繪製及設定自訂活動區的背景畫布。

CameraSnapshot 特性提供兩種擷取快照的方式,取決於您需要快取快照以快速算繪,還是需要隨選即時快照。

取得快照預覽

預覽快照是快取圖片,SDK 無需喚醒相機即可擷取。這是擷取靜態圖片最快的方法,非常適合用於攝影機總覽頁面或資訊主頁格線。

如要取得預覽快照,請讀取 preview_image_url 特性屬性:

// Get a flow of the CameraSnapshot trait
val cameraSnapshotFlow: Flow<CameraSnapshot?> = cameraDevice
  .type(GoogleCameraDevice)
  .trait(CameraSnapshot)

// Get the current trait state
val cameraSnapshot = cameraSnapshotFlow.firstOrNull()
if (cameraSnapshot != null) {
  val previewImageUrl = cameraSnapshot.previewImageUrl
  // Load the preview image from the URL
} else {
  // CameraSnapshot trait not supported on this device
}

視攝影機設定和活動而定,預覽網址會指向根據近期影片片段 (最新影片片段的第一個 I 影格) 產生的快取圖片,或是攝影機定期上傳的快照。

為表示圖片的新鮮度,Google Home 雲端會在提供的 JPEG/PNG 圖片中嵌入 X-Home-Image-Captured-At 標頭,格式為世界標準時間。您可以在用戶端剖析這個標頭,在應用程式中顯示快照的時間。

取得隨選即時快照

如果使用者歷程需要攝影機目前的即時監控畫面 (例如設定活動區),可以要求即時快照。觸發即時快照會指示攝影機喚醒、擷取新影格,並上傳到雲端。

如要要求即時快照,請使用 getLiveSnapshot() 指令:

try {
  val response = cameraSnapshot.getLiveSnapshot()
  val snapshotUrl = response.snapshotUrl
  // Load the live snapshot image from the URL
} catch (e: Exception) {
  Log.e("CameraSnapshot", "Failed to get live snapshot: ${e.message}")
}

驗證快照要求

   在 Android 範例應用程式中導入   

所有快照網址都會指向安全媒體,且需要 Home API v2 範圍的 OAuth 2.0 權杖。如要下載及算繪圖片,必須將 OAuth 權杖做為不記名權杖,插入 HTTP 要求的 Authorization 標頭。

以下範例說明如何設定安全的網路要求,以便下載快照:

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

/**
 * Downloads a snapshot from a URL using an OAuth access token.
 */
suspend fun loadSnapshotImage(urlString: String, oauthToken: String): Bitmap? =
  withContext(Dispatchers.IO) {
    var connection: HttpURLConnection? = null
    try {
      val url = URL(urlString)
      connection = url.openConnection() as HttpURLConnection
      // 1. Configure the HTTP GET request with the Bearer token
      connection.setRequestProperty("Authorization", "Bearer $oauthToken")

      // 2. Fetch the image data
      if (connection.responseCode == HttpURLConnection.HTTP_OK) {
        connection.inputStream.use { inputStream ->
          BitmapFactory.decodeStream(inputStream)
        }
      } else {
        null
      }
    } catch (e: IOException) {
      e.printStackTrace()
      null
    } finally {
      connection?.disconnect()
    }
  }

管理直播

調整直播畫質,可根據用戶端的觀看情境,最佳化頻寬用量 (例如在顯示較小的預覽圖塊、格狀檢視或子母畫面模式時,切換至較低解析度)。

使用 WebRtcLiveView 特性動態變更畫質,可專門管理特定用戶端上進行中直播工作階段的解析度。這與直接在裝置上設定裝置層級的頻寬用量不同,後者會影響所有同時觀看者,以及儲存到雲端的歷史錄影畫質。

以下範例說明如何擷取及更新裝置的直播畫質:

  • 擷取支援的畫質選項:取得裝置支援的串流解析度。程式碼會查詢裝置類型流程中的 WebRtcLiveView 特性 supportedQualityHints 屬性,並將支援的畫質公開為包含 QualityHint 值清單 (例如 QUALITY_HINT_SD、QUALITY_HINT_HD、QUALITY_HINT_FHD、QUALITY_HINT_QHD 或 QUALITY_HINT_UHD) 的 StateFlow。

  • 變更直播畫質:套用所選 QualityHint,變更進行中直播的串流解析度 (例如從標準畫質切換為高畫質)。changeQuality 函式會解析裝置的直播特性,並使用有效的 mediaSessionId 和所選 QualityHint 設定呼叫 changeLiveViewQuality。

// Assuming you have a HomeDevice instance 'device'
val availableQualityHints: StateFlow<List> =
    device.type(GoogleDoorbellDevice)
        .trait(WebRtcLiveView)
        .map { trait -> 
            trait?.supportedQualityHints ?: emptyList() 
        }
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())

// Assuming you have a HomeDevice instance 'device'
suspend fun changeQuality(mediaSessionId: String, qualityHint: QualityHint) {
    // Get the trait from the device
    val trait = device.type(GoogleDoorbellDevice).trait(WebRtcLiveView).first() ?: return
    try {
        trait.changeLiveViewQuality(mediaSessionId, qualityHint)
    } catch (e: Exception) {
        // Handle error
    }
}

電池設定

你可以透過 Home API 控制各種電池設定。

設定電池用量偏好設定

設定能源平衡,即可在裝置的電池續航力和效能之間取得平衡。您可以建立不同的電池設定檔,例如「延長」、「平衡」和「效能」,並在這些設定檔之間切換。

如要實作這項功能,請更新 EnergyPreference 特性的 currentEnergyBalance 屬性。這個屬性接受整數索引,對應到裝置 energyBalances 清單中定義的特定設定檔 (例如 0 對應 EXTENDED、1 對應 BALANCED、2 對應 PERFORMANCE)。

null 的 currentEnergyBalance 值表示裝置使用的是自訂設定檔。這是唯讀狀態。

以下範例說明 currentEnergyBalance 屬性會使用的結構,接著是使用該屬性的實際程式碼片段。

// Example energyBalances list
energy_balances: [
  {
    step: 0,
    label: "EXTENDED"
  },
  {
    step: 50,
    label: "BALANCED"
  },
  {
    step: 100,
    label: "PERFORMANCE"
  }
]
// The index parameter must be within the UByte range (0-255).
suspend fun setEnergyBalance(trait: EnergyPreference, index: Int) {
  trait.update { setCurrentEnergyBalance(index.toUByte()) }
}

// Setting the battery usage to more recording ie performance
setEnergyBalance(energyPreference, 2)

開啟自動省電模式

如要設定這項功能,請更新 EnergyPreference 特性的 currentLowPowerModeSensitivity 屬性。這項屬性會使用索引選取敏感度等級,其中 0 通常代表 Disabled,1 則代表 Enabled 或 Automatic。

suspend fun setAutomaticBatterySaver(enable: Boolean, trait: EnergyPreference) {
  // 0 is Disabled, 1 is Enabled
  val value = if (enable) 1.toUByte() else 0.toUByte()
  trait.update { setCurrentLowPowerModeSensitivity(value) }
}

取得電池充電狀態

如要取得裝置目前的充電狀態 (充電中、已充飽電或未充電),請使用 PowerSource 特性的 batChargeState 屬性。

// Get the battery charging state
val batteryChargeState = powerSource.batChargeState

when (batteryChargeState) {
    PowerSourceTrait.BatChargeStateEnum.IsCharging -> "Charging"
    PowerSourceTrait.BatChargeStateEnum.IsAtFullCharge -> "Full"
    PowerSourceTrait.BatChargeStateEnum.IsNotCharging -> "Not Charging"
    else -> "Unknown"
}

取得電池電量

如要取得目前的電池電量,請使用 PowerSource 特性的 batChargeLevel 屬性。等級可以是 OK、Warning (低) 或 Critical。

// Get the battery charge level
val batteryLevel = powerSourceTrait.batChargeLevel

when (batteryLevel) {
    PowerSourceTrait.BatChargeLevelEnum.OK -> "OK"
    PowerSourceTrait.BatChargeLevelEnum.Warning -> "Warning"
    PowerSourceTrait.BatChargeLevelEnum.Critical -> "Critical"
    else -> "Unknown"
}

取得電源

如要判斷裝置使用的電源,請使用 PowerSource 特性的 BatPresent 和 wiredPresent 屬性。

  val trait: PowerSource
  val isWired = trait.wiredPresent
  val hasBattery = trait.batPresent

檢查是否需要更換電池

batReplaceability 屬性會指出電池是否不可更換、可由使用者更換,或可由原廠更換。

PowerSource 特性的 batReplacementNeeded 屬性會指出是否需要更換電池。

// Get the battery replacement status
val batteryStatus = powerSourceTrait.batReplacementNeeded

if (batteryStatus) {
  println("Replace the battery.")
}

如果電池可由使用者更換,batReplacementDescription 會描述電池,包括外型規格、化學成分和偏好製造商等資訊。

音訊設定

你可以透過 Home API 控制各種音訊設定。

開啟或關閉麥克風

如要開啟或關閉裝置的麥克風,請使用內建的 setMicrophoneMuted Kotlin 函式,更新 CameraAvStreamManagement 特性的 microphoneMuted 屬性:

// Turn the device's microphone on or off
suspend fun turnOffMicrophone(disableMicrophone: Boolean, trait: CameraAvStreamManagement) {
  trait.update { setMicrophoneMuted(disableMicrophone) }
}

開啟或關閉錄音功能

如要開啟或關閉裝置的錄音功能,請使用內建的 setRecordingMicrophoneMuted Kotlin 函式,更新 CameraAvStreamManagement 特性的 recordingMicrophoneMuted 屬性:

// Turn audio recording on or off for the device
suspend fun turnOffAudioRecording(disableAudioRecording: Boolean, trait: CameraAvStreamManagement) {
  trait.update { setRecordingMicrophoneMuted(disableAudioRecording) }
}

調整揚聲器音量

如要調整裝置的喇叭音量,請使用內建的 setSpeakerVolumeLevel Kotlin 函式,更新 CameraAvStreamManagement 特性的 speakerVolumeLevel 屬性:

// Adjust the camera speaker volume
suspend fun adjustSpeakerVolume(volume: Int, trait: CameraAvStreamManagement) {
  trait.update { setSpeakerVolumeLevel(volume.toUbyte()) }
}

活動區設定

   在 Android 範例應用程式中導入   

ZoneManagement 特性提供介面,可用於管理攝影機和門鈴裝置上的自訂感興趣區域 (活動區)。這些區域可用來篩選事件偵測 (例如人物或車輛動作),只偵測裝置視野內的特定區域。

使用者可以在合作夥伴應用程式中設定活動區,在攝影機視野中的特定區域繪製活動區。接著,系統會將這些使用者定義的區域,轉譯為這個特徵使用的結構。如要進一步瞭解活動區的運作方式,請參閱「設定及使用活動區」一文。

活動區通常是以 2D 笛卡兒座標定義。這項特徵會提供頂點的 TwoDCartesianVertexStruct,以及區域定義 (名稱、頂點、顏色和用途) 的 TwoDCartesianZoneStruct。

查看活動區

如要顯示活動區,請檢查 ZoneManagement 特性的 zones 屬性。

// 1. Obtain the trait flow from the device
private val zoneManagementFlow: Flow =
  device.type(CAMERA_TYPE).flatMapLatest { it.trait(ZoneManagement) }

// 2. Map the flow to the list of zone structures
val activityZones: Flow<List<ZoneManagementTrait.ZoneInformationStruct>> =
  zoneManagementFlow.map { trait ->
    trait.zones ?: emptyList()
  }

新增活動區

如要建立新可用區,請使用 createTwoDCartesianZone 指令。這個指令會採用 TwoDCartesianZoneStruct,定義區域的名稱、頂點、顏色和用途。

以下範例說明如何建立名為「Front Porch」的區域,該區域有四個頂點,顏色為鮭魚色 (#F439A0),用於動作偵測。

import com.google.home.google.ZoneManagement
import com.google.home.google.ZoneManagementTrait
import com.google.home.matter.serialization.OptionalValue

/**
 * Creates a custom activity zone named "Front Porch" with a salmon color
 * configured for motion detection.
 */
suspend fun createFrontPorchZone(zoneManagement: ZoneManagement) {
  // 1. Define the vertices for the zone (2D Cartesian coordinates)
  // Values are typically scaled to a maximum defined by the device's twoDCartesianMax attribute.
  val vertices =
    listOf(
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 260u, y = 422u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 1049u, y = 0u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 2048u, y = 0u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 2048u, y = 950u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 1630u, y = 1349u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 880u, y = 2048u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 0u, y = 2048u),
      ZoneManagementTrait.TwoDCartesianVertexStruct(x = 638u, y = 1090u)
    )

  // 2. Define the zone structure
  val newZone =
    ZoneManagementTrait.TwoDCartesianZoneStruct(
      name = "Front Porch",
      vertices = vertices,
      // Usage defines what the zone filters (for example, Motion, Person, Vehicle)
      use = listOf(ZoneManagementTrait.ZoneUseEnum.Motion),
      // Color is typically a hex string (for example, Salmon/Pink)
      color = OptionalValue.present("#F439A0")
    )

  try {
    // 3. Execute the command to add the zone to the device
    zoneManagement.createTwoDCartesianZone(newZone)
    println("Successfully created activity zone.")
  } catch (e: Exception) {
    // Handle potential HomeException or Timeout
    println("Failed to create activity zone: ${e.message}")
  }
}

更新活動區

如要更新現有可用區,請使用 updateTwoDCartesianZone 指令。這個指令需要 zoneId 和更新後的 TwoDCartesianZoneStruct。

private suspend fun ZoneManagement.updateZone(
  zoneId: UShort,
  zone: ZoneManagementTrait.TwoDCartesianZoneStruct
) {
  // Execute the command to update the zone
  this.updateTwoDCartesianZone(zoneId = zoneId, zone = zone)
}

刪除活動區

如要移除區域,請使用 removeZone 指令,並指定 zoneId。

private suspend fun ZoneManagement.deleteZone(zoneId: UShort) {
  // Execute the command to remove the zone
  this.removeZone(zoneId = zoneId)
}

聲響偵測事件觸發條件

AvStreamAnalysis 特性提供介面,可用於管理攝影機和門鈴裝置上的事件偵測觸發條件。雖然影像觸發條件 (例如人或車輛) 可以限定在特定區域,但聲音相關觸發條件通常是裝置層級的設定。

EventTriggerTypeEnum 的聲響偵測功能提供下列觸發條件類型:

模式 列舉值 說明
音效 Sound 一般聲響偵測。
有人說話 PersonTalking 偵測語音。
狗吠 DogBark 偵測狗叫聲。
玻璃碎裂 GlassBreak 偵測玻璃碎裂聲。
煙霧警報 SmokeAlarm 偵測煙霧警報器,通常會辨識 T3 音訊模式 (三次短嗶聲後暫停)。
一氧化碳警報 CoAlarm 偵測一氧化碳 (CO) 警報,通常會發出 T4 聲響模式 (四聲短嗶聲後暫停)。

查看聲響偵測狀態

如要向使用者顯示聲響偵測功能的目前狀態,請務必檢查裝置支援的功能,以及裝置硬體啟用的功能。要檢查的兩個屬性如下:

在 Android 開發作業中,使用 Kotlin 流程時,通常會觀察 HomeDevice 的 AvStreamAnalysis 特徵。

// Example structure to store the
data class EventTriggerAttribute(val type: EventTriggerTypeEnum, val enabled: Boolean)

// 1. Obtain the trait flow from the device
private val avStreamAnalysisFlow: Flow<AvStreamAnalysis> =
  device.traitFromType(AvStreamAnalysis, CAMERA_TYPES.first { device.has(it) })

// 2. Map the flow to a list of sound event attributes
val soundEventTriggersState: Flow<List<EventTriggerAttribute>> =
  avStreamAnalysisFlow.map { trait ->
    // Get raw lists from the trait attributes
    val supported = trait.supportedEventTriggers ?: emptyList()
    val enabled = trait.enabledEventTriggers ?: emptyList()

    // Define sound-specific triggers to filter for
    val soundTypes = setOf(
      EventTriggerTypeEnum.Sound,
      EventTriggerTypeEnum.PersonTalking,
      EventTriggerTypeEnum.DogBark,
      EventTriggerTypeEnum.GlassBreak,
      EventTriggerTypeEnum.SmokeAlarm,
      EventTriggerTypeEnum.CoAlarm,
    )

    // Filter and associate status
    supported
      .filter { soundTypes.contains(it) }
      .map { type ->
        EventTriggerAttribute(
          type = type,
          enabled = enabled.contains(type)
        )
      }
  }

更新已啟用的觸發條件組合

如要更新已啟用的觸發條件組合,請使用 SetOrUpdateEventDetectionTriggers 指令,該指令會採用 EventTriggerEnablement 結構清單。

private suspend fun AvStreamAnalysis.updateEventTriggers(
  eventTriggers: List<EventTriggerAttribute>
) {
  val toUpdate = eventTriggers.map {
    EventTriggerEnablement(
      eventTriggerType = it.type,
      enablementStatus = if (it.enabled) {
        EnablementStatusEnum.Enabled
      } else {
        EnablementStatusEnum.Disabled
      },
    )
  }

  // Execute the command on the device
  setOrUpdateEventDetectionTriggers(toUpdate)
}

錄製模式

   在 Android 範例應用程式中導入   

RecordingMode 特性提供介面,可用於管理攝影機和門鈴裝置的影片和圖片錄製行為。使用者可以選擇連續錄影、事件錄影,或完全停用錄影 (僅限即時監控)。

RecordingModeEnum 定義可用的錄製策略:

模式 列舉值 說明
已停用 Disabled 完全停用錄製功能。主要由舊版裝置使用。
CVR (連續錄影) Cvr 全天候錄影。須訂閱 (例如 Google Home Premium。
EBR (事件錄影) Ebr 錄影功能是由事件 (人物、動作) 觸發。 影片長度取決於事件時間長度和訂閱方案。
ETR (事件觸發錄影) Etr 由事件觸發的短暫預覽錄影 (例如 10 秒)。
實景 LiveView 錄製功能已停用,但使用者仍可觀看直播。
靜態圖片 Images 發生事件時,系統會拍攝快照,而非錄製影像。

查看錄製模式

如要顯示目前的錄製設定,請檢查 RecordingMode 特性的屬性:

// 1. Obtain the trait flow from the device
private val recordingModeTraitFlow: Flow =
    device.traitFromType(RecordingMode, CAMERA_TYPES.first { device.has(it) })

// 2. Map the flow to recording mode options
data class RecordingModeOptions(
    val recordingMode: RecordingModeTrait.RecordingModeEnum,
    val index: Int,
    val available: Boolean,
    val readableString: String,
)

private val recordingModeOptions: Flow<List> =
    recordingModeTraitFlow.map { trait ->
        val supported = trait.supportedRecordingModes?.map { it.recordingMode } ?: emptyList()
        val available = trait.availableRecordingModes?.map { it.toInt() } ?: emptyList()

        supported.withIndex().map { (index, mode) ->
            RecordingModeOptions(
                recordingMode = mode,
                index = index,
                available = available.contains(index),
                readableString = mode.toReadableString(),
            )
        }
    }

變更錄製模式

更新前,請確認 availableRecordingModes 屬性中包含從 supportedRecordingModes 屬性選擇的索引。

如要更新所選模式,請使用 setSelectedRecordingMode 函式,並傳遞所選模式的索引:

private suspend fun RecordingMode.updateRecordingMode(index: Int) {
    // Execute the command to update the selected mode
    this.setSelectedRecordingMode(index.toUByte())
}

其他設定

你可以透過 Home API 控制其他多項設定。

開啟或關閉夜視功能

如要開啟或關閉攝影機的夜視功能,請使用 TriStateAutoEnum,透過內建的 setNightVision Kotlin 函式更新 CameraAvStreamManagement 特性的 nightVision 屬性:

// Turn night vision on
cameraAvStreamManagement.update {
  setNightVision(CameraAvStreamManagementTrait.TriStateAutoEnum.On)
}

// Turn night vision off
CameraAvStreamManagement.update {
  setNightVision(CameraAvStreamManagementTrait.TriStateAutoEnum.Off)
}

變更狀態 LED 燈的亮度

如要變更狀態 LED 的亮度,請使用 ThreeLevelAutoEnum,透過內建的 setStatusLightBrightness Kotlin 函式更新 CameraAvStreamManagement 特性的 statusLightBrightness 屬性:

// Set the LED brightness to high
cameraAvStreamManagement.update {
  setStatusLightBrightness(CameraAvStreamManagementTrait.ThreeLevelAutoEnum.High)
}

// Set the LED brightness to low
cameraAvStreamManagement.update {
  setStatusLightBrightness(CameraAvStreamManagementTrait.ThreeLevelAutoEnum.Low)
}

變更攝影機檢視區

攝影機可視區域與「縮放加強 Nest 攝影機影像」支援文章所述的「縮放及裁剪」功能相同。

可視區域定義在 ViewportStruct 中,其中包含四個值,做為可視區域的座標。座標定義如下:

(x1,y1) -- (x2,y1)
   |          |
(x1,y2) -- (x2,y2)

ViewportStruct 的值取決於應用程式的 UI 和相機實作。在最基本的層級,如要設定攝影機影片的檢視區塊,請使用內建的 setViewport Kotlin 函式,以 ViewportStruct 更新 CameraAvStreamManagement 特性的 viewport 屬性:

cameraAvStreamManagement
  .update { setViewport(
    CameraAvStreamManagementTrait.ViewportStruct(
      x1 = horizontalRange.rangeStart.roundToInt().toUShort(),
      x2 = horizontalRange.rangeEnd.roundToInt().toUShort(),
      y1 = verticalRange.rangeStart.roundToInt().toUShort(),
      y2 = verticalRange.rangeEnd.roundToInt().toUShort(),
    )
) }

啟用或停用數據分析

每部裝置都可以個別選擇加入,將詳細數據分析資料傳送至 Google Home 雲端 (請參閱「Home API 的 Cloud Monitoring」)。

如要啟用裝置的數據分析功能,請將 ExtendedGeneralDiagnosticsTrait 的 analyticsEnabled 屬性設為 true。設定 analyticsEnabled 時,另一個屬性 logUploadEnabled 會自動設為 true,以便將數據分析記錄檔上傳至 Google Home 雲端。

// Enable analytics
extendedGeneralDiagnostics.update {
  setAnalyticsEnabled(true)
}

// Disable analytics
extendedGeneralDiagnostics.update {
  setAnalyticsEnabled(false)
}

傳輸和錄製設定

本節說明與攝影機串流影像畫質和事件觸發相關的設定。這些設定是由 PushAvStreamTransport 特性管理。

讀取傳輸設定

本節說明如何從攝影機或門鈴裝置擷取目前的設定。它會擷取 PushAvStreamTransport 特性,找出用於錄製的特定連線,然後擷取頻寬品質、喚醒靈敏度和事件長度上限的目前值。

val trait: PushAvStreamTransport = device.getTrait(PushAvStreamTransport)
val connections = trait.findTransport().transportConfigurations

// Locate the connection designated for recording
val recordingConnection = connections.firstOrNull {
    it.transportOptions.getOrNull()?.streamUsage == StreamUsageEnum.Recording
}

val options = recordingConnection?.transportOptions?.getOrNull()

// 1. Bandwidth Quality (Video Stream ID)
val videoStreamId = options?.videoStreamId?.getOrNull()

// 2. Wake-up Sensitivity (Motion Sensitivity)
val wakeUpSensitivity = options?.triggerOptions?.motionSensitivity?.getOrNull()

// 3. Max Event Length (Motion Trigger Time Control)
val maxEventLength = options?.triggerOptions?.motionTimeControl?.getOrNull()?.maxDuration

更新傳輸設定

本節說明如何變更傳輸設定。它會建立包含新值的新 TransportOptionsStruct,然後使用 modifyPushTransport 指令將這些更新後的設定傳回裝置,並套用至上一步找到的錄製連線。

如要修改這些設定,請使用 modifyPushTransport 指令搭配 TransportOptionsStruct。

val toUpdate = TransportOptionsStruct(
    videoStreamId = OptionalValue.present(2u), // e.g., Max Quality
    triggerOptions = TransportTriggerOptionsStruct(
        motionSensitivity = OptionalValue.present(5u), // e.g., Medium
        motionTimeControl = OptionalValue.present(
            TransportMotionTriggerTimeControlStruct(maxDuration = 30u)
        )
    )
)

if (recordingConnection != null) {
    trait.modifyPushTransport(
        connectionId = recordingConnection.connectionId,
        transportOptions = toUpdate
    )
}

判斷頻寬品質

TransportOptionsStruct 的 videoStreamId 屬性對應至特定影片串流設定。

如要取得支援的影片串流,請參閱 allocatedVideoStreams 屬性,這是 VideoStreamStructs 的清單。CameraAvStreamManagement。

調整裝置喚醒靈敏度

TransportTriggerOptionsStruct 的 motionSensitivity 屬性對應下列值:

標籤 值 (UByte)
低 1u
中 5u
高 10u

調整事件錄影時間上限

TransportMotionTriggerTimeControlStruct 的 maxDuration 屬性對應下列時間長度 (以秒為單位):

  • 10u、15u、30u、60u、120u、180u

電鈴設定

你可以透過 Home API 控制各種門鈴鈴聲設定。

變更提示音

如要變更門鈴鈴聲,請先使用 Chime 特性的 installedChimeSounds 屬性,取得裝置上安裝的鈴聲清單:

// Get a list of chimes and identify the currently selected one
private val doorbellChimeTraitFlow: Flow =
    device.traitFromType(Chime, GoogleDoorbellDevice)
val chimeSounds = doorbellChimeTraitFlow.first().installedChimeSounds ?: emptyList()

接著,使用內建的 setSelectedChime Kotlin 函式更新 Chime 特性的 selectedChime 屬性:

// Set the chime using the chimeId from the installed list
chimeSounds.firstOrNull { it.name == name }?.let { setSelectedChime(it.chimeId) }

使用外接電鈴

   在 Android 範例應用程式中導入   

你可以將門鈴設為使用外部電鈴,例如安裝在住家內部的機械式電鈴。請務必在安裝門鈴時完成這項設定,以免損壞外接電鈴。

如要指出安裝的外接電鈴類型,請使用 ExternalChimeType,透過內建的 setExternalChime Kotlin 函式更新 Chime 特性的 externalChime 屬性:

// Indicate the external chime is mechanical
chime.update {
  setExternalChime(ChimeTrait.ExternalChimeType.Mechanical)
}

變更外接電鈴時長

你可以透過 Home API 設定外部電鈴響鈴的持續時間 (以秒為單位)。如果外接電鈴支援電鈴持續時間,使用者可能需要設定這項功能。

這裡設定的值取決於外接電鈴本身的規格,以及建議的電鈴時長。

如要變更外部電鈴持續時間,請使用內建的 setExternalChimeDurationSeconds Kotlin 函式,更新 Chime 特性的 externalChimeDurationSeconds 屬性:

// Change the external chime duration
chime.update {
  setExternalChimeDurationSeconds(newDuration.toUShort())
}

啟用鈴聲主題

   在 Android 範例應用程式中導入   

部分門鈴的鈴聲可能僅限時提供。例如節慶專屬鈴聲。 這些稱為「提示音主題」。

如要查看使用者可用的鈴聲主題,請建立時間盒篩選器,並用來篩選 ChimeThemes 特性的 getAvailableThemes() 指令結果。這會傳回可用主題清單,包括主題名稱。

以下範例說明如何篩選清單。如果目前時間介於主題的開始和結束時間 (分別為 startTimeSeconds 和 endTimeSeconds 值) 之間,系統就會將主題視為有效。如未設定開始時間,則視為從一開始就啟用。如未設定結束時間,則會無限期持續啟用。如果兩者皆未設定,主題就會一律啟用。

// Get themes from the ChimeThemes trait
fun List<ChimeThemesTrait.ThemeStruct>.filterTimeboxedThemes():
    List<ChimeThemesTrait.ThemeStruct> {
  val now = timeSource.instant().epochSecond.toULong()
  return filter { chimeStruct: ChimeThemesTrait.ThemeStruct ->
    val startTime: ULong = chimeStruct.startTimeSeconds.getOrNull() ?: 0UL
    val endTime: ULong = chimeStruct.endTimeSeconds.getOrNull() ?: MAX_VALUE
    startTime <= now && now <= endTime
  }
}

val availableThemes =
  doorbellChimeThemesTraitFlow
    .first()
    .getAvailableThemes()
    .themes
    .filterTimeboxedThemes()

取得所需主題的名稱 (例如 Christmas) 後,您可以使用 ChimeThemes 特徵上的 setSelectedTimeboxedThemeName() 函式選取該主題:

// Select a theme using the ChimeThemes trait
val themeToSelect = "Christmas"
if (themeToSelect in availableThemeNames) {
  doorbellChimeThemesTraitFlow.first().setSelectedTimeboxedThemeName(themeToSelect)
}

訪客通報設定

您可以使用 Home API 的 VisitorAnnouncement 特性,查詢及管理門鈴的訪客通報設定。這項特性能控制是否要在有人按門鈴時,透過 Google 智慧音箱或螢幕通報訪客到來。

以下範例說明如何檢查是否已啟用訪客通報功能,以及如何更新這項設定:

// Read the current visitor announcements state
val isEnabled = visitorAnnouncementTrait.visitorAnnouncementsEnabled

// Toggle the visitor announcements setting
visitorAnnouncementTrait.update {
    setVisitorAnnouncementsEnabled(true)
}

停用裝置

如要從結構體中移除門鈴,請呼叫 device.decommissionDevice()。停用時,系統會向使用者顯示 Google 管理的確認畫面,警告所有記錄的全天候錄影記錄、事件記錄、剪輯片段和熟面孔資料庫資料,都會從住家中永久刪除。

如需詳細操作說明和程式碼範例,請參閱「移除裝置」。