Apologies in advance for the long post. I'm new to HomeKit and Matter but not to development, I'm trying to write a SwiftUI app for my smart home to store all of my HomeKit and Matter setup barcodes along with other bits of information.
The intention is to scan the QR codes with my App and then save that QR payload in a simple Database along with other manually entered device details. Example payloads:
X-HM://00GWIN0B5PHPG <-- Eufy V120 HomeKit Camera
MT:GE.01-C-03FOPP6B110 <-- Moes GU10 Matter Bulb
I have it 99% working, my app is even able to discern the manual pairing code from the above payloads. However one of the key feature of this is that I want to open a device entry in my app and tap the HomeKit or Matter code displayed in my app and and either:
a) Ideally pass it off to the Apple Home app to initiate pairing just like the native Camera App can.
b) Create a custom flow in my app using the HomeKit or Matter API's to initiate paring from within my app.
So ideally just like the flow that happens when you scan a setup QR with the normal camera and tap "Open in Home". However I want to trigger this flow with just knowing the Payload and not with scanning it via the camera.
I was hoping there might be something as simple as a URL scheme that I could call with the payload as a variable and it then deep links and switches to the Home app, but I haven't found any info relating to this that actually works.
This is some code I have tried with the HomeKit API but this also results in an error:
import HomeKit
func startHomePairing(with setupCode: String) {
// Handle HomeKit setup
guard let payload = HMAccessorySetupPayload(url: URL(string: setupCode)!) else {
print("Invalid HomeKit setup code or format.")
return
}
let setupRequest = HMAccessorySetupRequest()
setupRequest.payload = payload
let setupManager = HMAccessorySetupManager()
// Perform the setup request and handle the result
setupManager.performAccessorySetup(using: setupRequest) { result, error in
if let error = error {
// Error handling: print the error details
print("Error starting setup: \(error.localizedDescription)")
// Print more details for debugging
print("Full Error: \(error)")
} else {
// Success: pairing was successful
print("Successfully launched Home app for HomeKit setup.")
}
}
}
But when passing in the QR payloads above it give the following ..
HomeKit Code
[0CAB3B05] Failed to perform accessory setup using request: Error Domain=HMErrorDomain Code=17 "(null)"
Matter Code
Failed to create HMSetupAccessoryPayload from setup payload URL MT:GE.01-C-03FOPP6B110: Error Domain=HMErrorDomain Code=3 "(null)"
I have added the "HomeKit" and "Matter Allow Setup Payload" capabilities to my app, I have also ensured I have these in the .plist ..
<key>NSHomeKitUsageDescription</key>
<string>Access required to HomeKit to initiate pairing for new accessories</string>
I also added a call to ensure my app appears in the Settings / Privacy / HomeKit section. I originally thought was a seemingly simple task, but I am really struggling with how to make it work!
Selecting any option will automatically load the page