Hub Activation API ช่วยให้คุณค้นหาและเปิดใช้งานฮับ Google Home โดยใช้โปรแกรมได้ ซึ่งจะมีประโยชน์อย่างยิ่งในกรณีที่ผู้ใช้ไม่มีวิธีอื่นในการเปิดใช้งานฮับ เช่น ในกรณีของฮับที่ไม่มีหน้าจอ
ใช้ Hub Activation API
การใช้ Hub Activation API ช่วยให้คุณสร้างแอปที่ค้นหาและ เปิดใช้งานฮับได้
รับการอ้างอิงถึง
HubManagementTraitใน โครงสร้างval hubManagementTrait = hubManagementTraitFlow.firstOrNull { it.metadata.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE } if (hubManagementTrait == null) { errorsEmitter.emit(HomeException.notFound("HubManagement trait isn't online")) }ระบุอุปกรณ์ที่ใช้ฮับได้ในเครือข่าย Wi-Fi โดยทำดังนี้
try { val unused = hubManagementTrait.discoverAvailableHubs() } catch (e: Exception) { Log.d(TAG_HUB_DISCOVERY, "Error discovering hubs $e") errorsEmitter.emit(e) } val hubManagementTraitFlow = structureFlow.flatMapLatest { it.trait(HubManagement) } val discoveredHubs = hubManagementTraitFlow .map { it.discoveredHubsList } .handleErrors() .flowOn(ioDispatcher) .stateIn( scope = CoroutineScope(viewModelScope.coroutineContext + ioDispatcher), started = SharingStarted.WhileSubscribed(), listOf(), )เปิดใช้งานอุปกรณ์ที่ใช้เป็นฮับได้
try { val unused = hubManagementTrait.activateHub(hub) } catch (e: Exception) { Log.d("Hub Activation", "Error activating hub $e") }