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 hai đặc điểm chính của thiết bị:
ThreadNetworkCapabilities,
cung cấp các thuộc tính chỉ có thể đọc để kiểm tra các tính năng của border router và
ThreadNetworkManagement,
xử lý các lệnh trong vòng đời mạng và chia sẻ thông tin xác thực
bằng Khoá được chia sẻ trước tạm thời cho Người uỷ quyền (ePSKc). 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 khả năng hỗ trợ thuộc tính và lệnh cho một thiết bị 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ên Android để 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 |
Xem thông tin cơ bản về thiết bị
Được triển khai trong Ứng dụng mẫu cho Android
Đặc điểm BasicInformation bao gồm các thông tin như tên nhà cung cấp, mã nhà cung cấp, mã 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 cài đặt quyền truy cập Internet) bằng cách sử dụ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ý việc chia sẻ thông tin đăng nhập mạng Thread (ePSKc)
Việc chia sẻ thông tin xác thực Thread được thực hiện bằng cách sử dụng Khoá tạm thời được chia sẻ trước cho Uỷ viên (ePSKc). Chế độ ePSKc tạo một khoá truy cập tạm thời, an toàn mà các thiết bị bên ngoài hoặc uỷ viên có thể sử 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
}
}
Tắ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 phát ra sự kiện khi phiên ePSKc kết thúc (ví dụ: vì khoá đã được sử dụng, phiên đã hết hạn hoặc 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 trong mạng Thread
Bạn có thể ra lệnh cho TBR tham gia một mạng Thread mới bằng cách cung cấp TLV của Tập dữ liệu hoạt động đang hoạt động hoặc ra lệnh cho thiết bị 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ông trình
Đặc điểm ThreadNetworkSettings là một đặc điểm có thể cập nhật được đính kèm vào một Structure (đại diện cho một ngôi nhà hoặc toà nhà). Thuộc tính 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}")
}
}