Android için sınır yönlendirici cihaz kılavuzu

Android uygulama geliştiricileri, Home API'leri kullanarak Thread Border Router (TBR) yönetebilir.

GoogleBorderRouterDevice, iki temel cihaz özelliği kullanılarak uygulanır: border router özelliklerini incelemek için salt okunur özellikler sağlayan ThreadNetworkCapabilities ve Commissioner için Geçici Önceden Paylaşılan Anahtar (ePSKc) kullanarak ağ yaşam döngüsü komutlarını ve kimlik bilgisi paylaşımını yöneten ThreadNetworkManagement. Yapı düzeyindeki internet erişimi politikaları, ThreadNetworkSettings özelliği kullanılarak yönetilir.

Herhangi bir özelliği kullanmadan veya özellikleri güncellemeye çalışmadan önce her zaman cihazın özellik ve komut desteğini kontrol edin. Daha fazla bilgi için Androidcihazlarını kontrol etme başlıklı makaleyi inceleyin.

Home API'leri Cihaz Türü Özellikler Kotlin Örnek Uygulaması Kullanım Örneği

Sınır Yönlendirici

GoogleBorderRouterDevice

home.matter.6006.types.0161

Gerekli Özellikler
     google ThreadNetworkCapabilities
     google ThreadNetworkManagement

Sınır yönlendirici

Cihaz hakkında temel bilgileri alma

   Android için örnek uygulamada uygulanır   

BasicInformation özelliği; cihazın tedarikçi adı, tedarikçi kimliği, ürün kimliği, ürün adı (model bilgilerini içerir) ve yazılım sürümü gibi bilgileri içerir:

// 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}")
    }

Sınır yönlendirici özelliklerini inceleme

ThreadNetworkCapabilities özelliğini kullanarak bir border router cihazının salt okunur özelliklerini (ör. ePSKc desteği ve internet erişimi ayarı yapılandırması) inceleyebilirsiniz.

suspend fun checkBorderRouterCapabilities(device: HomeDevice) {
    // Filter for GoogleBorderRouterDevice device type
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    if (gtbrDevice == null) {
        println("Device is not a Google border router.")
        return
    }

    // Retrieve the ThreadNetworkCapabilities trait
    val capabilitiesTrait = gtbrDevice.trait(ThreadNetworkCapabilities)
    if (capabilitiesTrait == null) {
        println("ThreadNetworkCapabilities trait not found on device.")
        return
    }

    val isEpskcSupported = capabilitiesTrait.epskcSupported ?: false
    val internetAccessOption = capabilitiesTrait.internetAccessOption?.name ?: "None"
    val isIasSupported = !internetAccessOption.equals("None", ignoreCase = true)

    println("ePSKc Supported: $isEpskcSupported")
    println("Internet Access Setting Supported: $isIasSupported")
}

Thread kimlik bilgisi paylaşımını (ePSKc) yönetme

Thread kimlik bilgisi paylaşımı, Commissioner için Geçici Önceden Paylaşılan Anahtar (ePSKc) kullanılarak gerçekleştirilir. ePSKc modu, harici cihazların veya Commissioner'ların Thread ağ veri kümesini güvenli bir şekilde almak için kullanabileceği geçici ve güvenli bir geçiş anahtarı oluşturur.

ePSKc modunu etkinleştirme

suspend fun startEpskcSession(device: HomeDevice, durationSeconds: Short): ActivateEpskcModeCommand.Response? {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    if (mgmtTrait == null) {
        println("ThreadNetworkManagement trait not found.")
        return null
    }

    return try {
        val response = mgmtTrait.activateEpskcMode(
            optionalArgs = { this.requestedDurationSeconds = durationSeconds }
        )

        println("ePSKc Session Activated!")
        println("Status: ${response.status}")
        println("Ephemeral PSKc: ${response.epskc}")
        println("Valid Duration (s): ${response.validDurationSeconds}")

        response
    } catch (e: Exception) {
        println("Failed to activate ePSKc mode: ${e.message}")
        null
    }
}

ePSKc modunu devre dışı bırakma

suspend fun stopEpskcSession(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    mgmtTrait?.deactivateEpskcMode()
    println("ePSKc mode deactivated.")
}

ePSKc devre dışı bırakma etkinliklerini gözlemleme

TBR, bir ePSKc oturumu sona erdiğinde (örneğin, anahtar kullanıldığı, oturumun süresi dolduğu veya manuel olarak iptal edildiği için) etkinlikler yayınlar.

suspend fun observeEpskcEvents(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    mgmtTrait?.epskcModeDeactivatedEventFlow()?.collect { event ->
        println("ePSKc Session Ended. Reason: ${event.reason}")
        when (event.reason?.name) {
            "KeyUsed" -> println("Key was successfully used to commission a device.")
            "Expired" -> println("Session timed out before the key was used.")
            "Cancelled" -> println("Session was manually cancelled.")
        }
    }
}

Thread ağı üyeliğini yönetme

Etkin İşletim Veri Kümesi TLV'lerini sağlayarak TBR cihazına yeni bir Thread ağına katılma komutu verebilir veya mevcut ağından ayrılma komutu verebilirsiniz.

suspend fun joinNetwork(device: HomeDevice, datasetTlvs: ByteArray) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    try {
        val response = mgmtTrait?.joinNetwork(operationalDatasetTlvs = datasetTlvs)
        println("Join network command sent. Status: ${response?.status}")
    } catch (e: Exception) {
        println("Join network failed: ${e.message}")
    }
}

suspend fun leaveNetwork(device: HomeDevice) {
    val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
    val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)

    try {
        mgmtTrait?.leaveNetwork()
        println("Leave network command sent successfully.")
    } catch (e: Exception) {
        println("Leave network failed: ${e.message}")
    }
}

Yapı düzeyinde internet erişimini yapılandırma

ThreadNetworkSettings özelliği, bir Structure (ev veya binayı temsil eder) ile ilişkili güncellenebilir bir özelliktir. Geliştiricilerin, TBR'ler için yapı genelindeki internet erişimi politikasını yapılandırmasına olanak tanır.

suspend fun updateStructureInternetAccess(structure: Structure, enableInternetAccess: Boolean) {
    // Retrieve the ThreadNetworkSettings trait for the structure
    val settingsTrait = structure.trait(ThreadNetworkSettings).firstOrNull()
    if (settingsTrait == null) {
        println("ThreadNetworkSettings trait not found on structure.")
        return
    }

    val option = if (enableInternetAccess) {
        InternetAccessOption.InternetAccessOptionAll
    } else {
        InternetAccessOption.InternetAccessOptionNone
    }

    try {
        settingsTrait.update {
            setInternetAccessOption(option)
        }
        println("Successfully updated Thread internet access policy to: ${option.name}")
    } catch (e: Exception) {
        println("Failed to update Thread internet access policy: ${e.message}")
    }
}