Home API cho iOS sử dụng một trung tâm Matter để đưa một thiết bị vào một mạng lưới. Trong quá trình thiết lập, ứng dụng sẽ gửi một lệnh đến SDK rồi đến trung tâm.
Cấu hình dự án Xcode
Trước khi triển khai Commissioning API, hãy nhớ thêm các chức năng, quyền và thuộc tính Info.plist bắt buộc vào các mục tiêu Xcode của bạn:
Quyền
Đưa các mục sau vào mục tiêu ứng dụng chính và tệp .entitlements của tiện ích:
Quản lý thông tin đăng nhập mạng Thread:
<key>com.apple.developer.networking.manage-thread-network-credentials</key> <true/>Để sử dụng quyền này trong bản dựng phân phối, bạn phải gửi biểu mẫu yêu cầu cấp quyền cho Apple. Apple yêu cầu bạn chứng minh tư cách thành viên trong Nhóm Thread và xác minh rằng Bộ định tuyến biên theo giao thức Thread của bạn được Nhóm Thread chứng nhận.
Thông tin về Wi-Fi:
<key>com.apple.developer.networking.wifi-info</key> <true/>Nhóm ứng dụng: Thêm một nhóm ứng dụng dùng chung để cho phép ứng dụng chính và MatterTiện ích bổ sung thiết bị giao tiếp (ví dụ:
group.com.yourdomain.appgroupname). Mã nhận dạng này do nhà phát triển tạo và đăng ký trong Apple Developer Console, đồng thời phải khớp với Nhóm ứng dụng được định cấu hình trong các chức năng mục tiêu trong Xcode. Google Home SDK sử dụng mã nhận dạng này ở cả hai mục tiêu để tự động đồng bộ hoá trạng thái thiết lập, thông tin đăng nhập của mạng và danh sách phòng bằng cách sử dụng một vùng chứaUserDefaultsdùng chung.
Chia sẻ chuỗi khoá (không bắt buộc)
Nếu tiện ích ứng dụng của bạn cần truy xuất thông tin đăng nhập hoặc mã truy cập do ứng dụng chính lưu trữ bằng cách sử dụng UserInfo.authorizationToken(), thì bạn phải định cấu hình một nhóm truy cập chuỗi khoá dùng chung cho cả mục tiêu ứng dụng chính và mục tiêu tiện ích ứng dụng trong plist quyền của chúng:
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)your.shared.keychain.group</string>
</array>
Thuộc tính Info.plist
Thêm các khoá nội dung mô tả sau vào Info.plist của mục tiêu ứng dụng chính:
Nội dung mô tả về mức sử dụng thông tin vị trí:
<key>NSLocationWhenInUseUsageDescription</key> <string>Your custom message explaining why location access is needed to read the current Wi-Fi SSID</string>Dịch vụ Bonjour: Trong
NSBonjourServices, hãy thêm các dịch vụ cần thiết cho tính năng phát hiện Matter và Thread cục bộ:<key>NSBonjourServices</key> <array> <string>_matter._tcp</string> <string>_matterc._udp</string> <string>_matterd._udp</string> <string>_meshcop._udp</string> </array>Nội dung mô tả về việc sử dụng mạng cục bộ: Thêm khoá
NSLocalNetworkUsageDescriptionkèm theo một thông báo giải thích các quyền khám phá mạng cục bộ.
Uỷ quyền thiết bị
Cách uỷ quyền cho thiết bị Matter:
Thông báo cho
Home APIs iOS SDKđể chuẩn bị cho các yêu cầu Matter về việc uỷ quyền bằngstructure.prepareForMatterCommissioning(). Lệnh này sẽ thực hiện những việc sau:- Xác minh rằng quyền đã được cấp.
- Đảm bảo trung tâm đang kết nối mạng và có thể truy cập được.
- Đảm bảo không có phiên thiết lập đang diễn ra nào khác.
do { try await structure.prepareForMatterCommissioning() } catch { // Failed to prepare for Matter Commissioning return }Tạo yêu cầu bằng
MatterAddDeviceRequest()để bắt đầu quy trình hỗ trợ Matter của Apple.let topology = MatterAddDeviceRequest.Topology( ecosystemName: "Google Home", homes: [MatterAddDeviceRequest.Home(displayName: structure.name)] ) let request = MatterAddDeviceRequest(topology: topology)Thực hiện yêu cầu bằng
perform(). Nếu xảy ra lỗi, hãy huỷ yêu cầu thiết lập vớistructure.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. }Tạo một
App Group IDtrong Apple Developer Console để cho phép ứng dụng giao tiếp với tiện íchMatterAddDevicekhi thiết lập thiết bị.Bạn cũng cần cập nhật mã nhận dạng gói ứng dụng và hồ sơ cung cấp để sử dụng mã nhận dạng nhóm này.
Khi khởi tạo, hãy định cấu hình thực thể
Homeđể sử dụng giá trị nhận dạng nhóm.func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { Home.configure { $0.sharedAppGroup = "group.com.sample.app.commissioning" } return true }Triển khai Tiện ích ứng dụng Matter iOS của Apple.
Mã mẫu cho thấy ví dụ về cách triển khai một lớp con của API
MatterAddDeviceExtensionRequestHandlercủa Apple.Tối thiểu, hãy thêm Khung
GoogleHomeMatterCommissionerSDKvào mục tiêu tiện ích và ghi đè 3 phương thức để gọi các 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 } } }