适用于 iOS 的 Home API 使用 Matter 集线器将设备委托给 fabric。在调试期间,应用会向 SDK 发送命令,然后再将命令发送到集线器。
如需委托 Matter 设备,请执行以下操作:
使用
structure.prepareForMatterCommissioning()
通知Home APIs iOS SDK
为 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 开发者中心中创建
App Group ID
,以允许应用在配置设备时与MatterAddDevice
扩展程序通信。您还需要更新应用软件包标识符和配置文件,才能使用此组 ID。
在初始化时,将
Home
实例配置为使用组标识符。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).") } } }