Nhà phát triển ứng dụng Android có thể sử dụng Home API để quản lý Thread Border Router (TBR).
GoogleBorderRouterDevice được triển khai bằng cách sử dụng 2 đặc điểm chính của thiết bị: ThreadNetworkCapabilities, cung cấp các thuộc tính chỉ đọc để kiểm tra các tính năng border router và ThreadNetworkManagement, xử lý các lệnh vòng đời mạng và chia sẻ thông tin đăng nhập bằng Khoá chia sẻ trước tạm thời cho Người uỷ quyền (ePSKc). Các chính sách truy cập Internet ở cấp cấu trúc được quản lý bằng đặc điểm ThreadNetworkSettings.
Luôn kiểm tra xem thiết bị có hỗ trợ thuộc tính và lệnh hay không trước khi sử dụng bất kỳ tính năng nào hoặc cố gắng cập nhật thuộc tính. Hãy xem bài viết Điều khiển thiết bị trênAndroid để biết thêm thông tin.
| Loại thiết bị Home API | Đặc điểm | Ứng dụng mẫu Kotlin | Trường hợp sử dụng |
|---|---|---|---|
|
Bộ định tuyến biên
|
Đặc điểm bắt buộc google ThreadNetworkCapabilities google ThreadNetworkManagement |
Bộ định tuyến biên |
Lấy thông tin cơ bản về một thiết bị
Được triển khai trong Ứng dụng mẫu cho Android
Đặc điểm BasicInformation bao gồm những thông tin như tên nhà cung cấp, mã nhận dạng nhà cung cấp, mã nhận dạng sản phẩm, tên sản phẩm (bao gồm cả thông tin về kiểu máy) và phiên bản phần mềm của thiết bị:
// 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}") }
Kiểm tra các chức năng của bộ định tuyến biên
Bạn có thể kiểm tra các chức năng chỉ đọc của border router (chẳng hạn như hỗ trợ ePSKc và cấu hình chế độ cài đặt Quyền truy cập Internet) bằng đặc điểm ThreadNetworkCapabilities.
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")
}
Quản lý hoạt động chia sẻ thông tin đăng nhập mạng Thread (ePSKc)
Tính năng chia sẻ thông tin đăng nhập của luồng được thực hiện bằng Khoá được chia sẻ trước tạm thời cho Người uỷ quyền (ePSKc). Chế độ ePSKc tạo ra một khoá truy cập tạm thời và an toàn mà các thiết bị bên ngoài hoặc người uỷ quyền có thể dùng để lấy tập dữ liệu mạng Thread một cách an toàn.
Kích hoạt chế độ ePSKc
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
}
}
Huỷ kích hoạt chế độ ePSKc
suspend fun stopEpskcSession(device: HomeDevice) {
val gtbrDevice = device.type(GoogleBorderRouterDevice).firstOrNull()
val mgmtTrait = gtbrDevice?.trait(ThreadNetworkManagement)
mgmtTrait?.deactivateEpskcMode()
println("ePSKc mode deactivated.")
}
Quan sát các sự kiện huỷ kích hoạt ePSKc
TBR sẽ phát ra các sự kiện khi một phiên ePSKc kết thúc (ví dụ: vì khoá đã được sử dụng, phiên đã hết hạn hoặc phiên đã bị huỷ theo cách thủ công).
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.")
}
}
}
Quản lý tư cách thành viên mạng Thread
Bạn có thể yêu cầu TBR tham gia một mạng Thread mới bằng cách cung cấp các TLV của Tập dữ liệu hoạt động đang hoạt động hoặc yêu cầu rời khỏi mạng hiện tại.
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}")
}
}
Định cấu hình quyền truy cập Internet ở cấp cấu trúc
Đặc điểm ThreadNetworkSettings là một đặc điểm có thể cập nhật được gắn vào Structure (đại diện cho một ngôi nhà hoặc toà nhà). Chính sách này cho phép nhà phát triển định cấu hình chính sách truy cập Internet trên toàn cấu trúc cho TBR.
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}")
}
}