iOS 版 Home API 會使用 Matter 中樞將裝置委派至結構體。在調試期間,應用程式會先將指令傳送至 SDK,然後再傳送至中樞。
如要啟用 Matter 裝置,請按照下列步驟操作:
通知
Home APIs iOS SDK
,準備使用structure.prepareForMatterCommissioning()
啟用 Matter 要求。這個指令會執行以下操作:- 確認已授予權限。
- 確認中樞已連上網路且可供存取。
- 確認沒有其他正在進行的委派工作階段。
do { try await structure.prepareForMatterCommissioning() } catch { Logger.error("Failed to prepare for Matter Commissioning: \(error).") return }
使用
MatterAddDeviceRequest()
建立要求,啟動 Apple 的 Matter 支援流程。let topology = MatterAddDeviceRequest.Topology( ecosystemName: "Google Home", homes: [MatterAddDeviceRequest.Home(displayName: structure.name)] ) let request = MatterAddDeviceRequest(topology: topology)
使用
perform()
執行要求。如果發生錯誤,請使用structure.cancelMatterCommissioning()
取消委派要求。do { Logger.info("Starting MatterAddDeviceRequest.") try await request.perform() Logger.info("Successfully completed MatterAddDeviceRequest.") let commissionedDeviceIDs = try structure.completeMatterCommissioning() Logger.info("Commissioned device IDs: \(commissionedDeviceIDs).") } catch let error { structure.cancelMatterCommissioning() Logger.error("Failed to complete MatterAddDeviceRequest: \(error).") }
在 Apple Developer Console 中建立
App Group ID
,讓應用程式在裝置啟用時與MatterAddDevice
擴充功能通訊。您也需要更新應用程式套件 ID 和佈建設定檔,才能使用這個群組 ID。
在初始化時,請設定
Home
例項以使用群組 ID。func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { Home.configure { $0.sharedAppGroup = "group.com.sample.app.commissioning" } return true }
實作 Apple 的 iOS Matter 應用程式擴充功能。
範例程式碼顯示實作 Apple
MatterAddDeviceExtensionRequestHandler
API 子類別的範例。至少將
GoogleHomeMatterCommissionerSDK
架構新增至擴充功能目標,並覆寫三種方法來呼叫 Google Home platformHomeMatterCommissioner
API。commissionDevice
rooms
configureDevice
import MatterSupport import GoogleHomeMatterCommissionerSDK import OSLog final class RequestHandler: MatterAddDeviceExtensionRequestHandler { // The App Group ID defined by the application to share information between the extension and main app. private static var appGroup = "group.com.sample.app.commissioning" ... // MARK: - Home API commissioning handlers /// Commissions a device to the Google Home ecosystem. /// - Parameters: /// - home: The home that the device will be added to /// - onboardingPayload: The payload to be sent to the Matter Commissioning SDK to commission the device. /// - commissioningID: An identifier not used by the Home API SDK. override func commissionDevice(in home: MatterAddDeviceRequest.Home?, onboardingPayload: String, commissioningID: UUID) async throws { Logger.info("Commission Matter device with payload: '\(onboardingPayload)'.") var onboardingPayloadForHub = onboardingPayload let homeMatterCommissioner = try HomeMatterCommissioner(appGroup: RequestHandler.appGroup) try await homeMatterCommissioner.commissionMatterDevice( onboardingPayload: onboardingPayloadForHub) } /// Obtains rooms from the Home Ecosystem to present to the user during the commissioning flow. /// - Parameter home: The home that the device will be added to. /// - Returns: A list of rooms if obtained from the Google Home ecosystem or an empty list if there was an error in getting them. override func rooms(in home: MatterAddDeviceRequest.Home?) async -> [MatterAddDeviceRequest.Room] { do { let homeMatterCommissioner = try HomeMatterCommissioner(appGroup: RequestHandler.appGroup) let fetchedRooms = try homeMatterCommissioner.fetchRooms() Logger.info("Returning \(fetchedRooms.count) fetched rooms.") return fetchedRooms } catch { Logger.info("Failed to fetch rooms with error: \(error).") return [] } } /// Pushes the device's configurations to the Google Home Ecosystem. /// - Parameters: /// - name: The friendly name the user chose to set on the device. /// - room: The room identifier that the user chose to put the device in. override func configureDevice(named name: String, in room: MatterAddDeviceRequest.Room?) async { Logger.info("Configure Device name: '\(name)', room: \(room?.displayName ?? "").") do { let homeMatterCommissioner = try HomeMatterCommissioner(appGroup: RequestHandler.appGroup) await homeMatterCommissioner.configureMatterDevice( deviceName: name, roomName: room?.displayName) } catch { Logger.info("Configure Device failed with error: \(error).") } } }