Le API Home per iOS utilizzano un hub Matter per commissionare un dispositivo su un tessuto. Durante la configurazione, l'app invia un comando all'SDK e poi all'hub.
Per configurare un dispositivo Matter:
Comunica al
Home APIs iOS SDKdi prepararsi alle richieste di attivazione di Matter constructure.prepareForMatterCommissioning(). Questo comando eseguirà le seguenti operazioni:- Verifica che l'autorizzazione sia stata concessa.
- Assicurati che l'hub sia online e raggiungibile.
- Assicurati che non sia in corso un'altra sessione di configurazione attiva.
do { try await structure.prepareForMatterCommissioning() } catch { // Failed to prepare for Matter Commissioning return }Crea una richiesta con
MatterAddDeviceRequest()per avviare il flusso di assistenza Matter di Apple.let topology = MatterAddDeviceRequest.Topology( ecosystemName: "Google Home", homes: [MatterAddDeviceRequest.Home(displayName: structure.name)] ) let request = MatterAddDeviceRequest(topology: topology)Esegui la richiesta con
perform(). Se si verifica un errore, annulla la richiesta di messa in servizio constructure.cancelMatterCommissioning().do { // Starting MatterAddDeviceRequest. try await request.perform() // Successfully completed MatterAddDeviceRequest. let commissionedDeviceIDs = try structure.completeMatterCommissioning() // Commissioned device IDs. } catch let error { structure.cancelMatterCommissioning() // Failed to complete MatterAddDeviceRequest. }Crea un
App Group IDin Apple Developer Console per consentire all'app di comunicare con l'estensioneMatterAddDevicedurante l'attivazione del dispositivo.Dovrai anche aggiornare l'identificatore del bundle dell'applicazione e i profili di provisioning per utilizzare questo ID gruppo.
Durante l'inizializzazione, configura l'istanza
Homein modo che utilizzi l'identificatore del gruppo.func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { Home.configure { $0.sharedAppGroup = "group.com.sample.app.commissioning" } return true }Implementa l'estensione per app Matter per iOS di Apple.
Il codice campione mostra un esempio di implementazione di una sottoclasse dell'API
MatterAddDeviceExtensionRequestHandlerdi Apple.Come minimo, aggiungi il framework
GoogleHomeMatterCommissionerSDKal target dell'estensione ed esegui l'override di tre metodi per chiamare le API Google Home platformHomeMatterCommissioner.commissionDeviceroomsconfigureDevice
import MatterSupport import GoogleHomeSDK 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 { // Commission Matter device with payload. 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() // Returning fetched rooms. return fetchedRooms } catch { // Failed to fetch rooms with 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 { // Configure Device name: room do { let homeMatterCommissioner = try HomeMatterCommissioner(appGroup: RequestHandler.appGroup) await homeMatterCommissioner.configureMatterDevice( deviceName: name, roomName: room?.displayName) } catch { // Configure Device failed with error } } }