Using MatterSupport framework to add Matter device to our system

1, I add an Matter extension named TRMatterExtension, using the Template in Xcode. 2, check the InfoPlist with the TRMatterExtension module, I see the NSExtension key with two key which are like this: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>NSExtension</key> <dict> <key>NSExtensionPointIdentifier</key> <string>com.apple.matter.support.extension.device-setup</string> <key>NSExtensionPrincipalClass</key> <string>$(PRODUCT_MODULE_NAME).RequestHandler</string> </dict> </dict> </plist>

3,The RequestHandler.swift file is like this: import os import MatterSupport let logger = Logger(subsystem: "com.airdroitech.commissionMtr2", category: "MatterExtension")

// The extension is launched in response to MatterAddDeviceRequest.perform() and this class is the entry point // for the extension operations. class RequestHandler: MatterAddDeviceExtensionRequestHandler { override init() { logger.log("QAWI3 - init") super.init() } override func validateDeviceCredential(_ deviceCredential: MatterAddDeviceExtensionRequestHandler.DeviceCredential) async throws { // Use this function to perform additional attestation checks if that is useful for your ecosystem. logger.log("QAWI3 - validateDeviceCredential") }

override func selectWiFiNetwork(from wifiScanResults: [MatterAddDeviceExtensionRequestHandler.WiFiScanResult]) async throws -> MatterAddDeviceExtensionRequestHandler.WiFiNetworkAssociation {
    // Use this function to select a Wi-Fi network for the device if your ecosystem has special requirements.
    // Or, return `.defaultSystemNetwork` to use the iOS device's current network.
    logger.log("QAWI3 - Returning default system network")

    return .defaultSystemNetwork
}

override func selectThreadNetwork(from threadScanResults: [MatterAddDeviceExtensionRequestHandler.ThreadScanResult]) async throws -> MatterAddDeviceExtensionRequestHandler.ThreadNetworkAssociation {
    // Use this function to select a Thread network for the device if your ecosystem has special requirements.
    // Or, return `.defaultSystemNetwork` to use the default Thread network.
    return .defaultSystemNetwork
}

override func commissionDevice(in home: MatterAddDeviceRequest.Home?, onboardingPayload: String, commissioningID: UUID) async throws {
    // Use this function to commission the device with your Matter stack.
    logger.log("QAWI3 - Starting commissioning flow")
}

override func rooms(in home: MatterAddDeviceRequest.Home?) async -> [MatterAddDeviceRequest.Room] {
    // Use this function to return the rooms your ecosystem manages.
    // If your ecosystem manages multiple homes, ensure you are returning rooms that belong to the provided home.
    return [.init(displayName: "Living Room")]
}

override func configureDevice(named name: String, in room: MatterAddDeviceRequest.Room?) async {
    // Use this function to configure the (now) commissioned device with the given name and room.
}

}

4, I add Privacy - Local Network Usage Description and Bonjour services [ <string>_matter._tcp</string> <string>_matterc._udp</string> <string>_matterd._udp</string> ] to the main target's InfoPlist.

5, click a button to call MatterAddDeviceRequest.perform(), the code is as the following:

let homes = [MatterAddDeviceRequest.Home(displayName: "TRHome")] let topology = MatterAddDeviceRequest.Topology(ecosystemName: "TR", homes: homes) let request = MatterAddDeviceRequest(topology: topology) do { try await request.perform() print("Successfully set up device!") } catch { print("Failed to set up device with error: (error)") }

6, Result: Most times, Matter devices can be added to the KeyChain and the commission window of the Matter device could be opened. But sometimes the MatterSupport window will show "Pairing Failed". The total error is: [1E1D8753] Failed to perform Matter device setup setup: Error Domain=HMErrorDomain Code=18 "Pairing Failed" UserInfo={HFErrorUserInfoOptionsKey={ HFErrorUserInfoOptionDescriptionKey = "Pairing Failed"; HFErrorUserInfoOptionTitleKey = "\U65e0\U6cd5\U6dfb\U52a0\U914d\U4ef6"; }, NSLocalizedDescription=Pairing Failed, NSUnderlyingError=0x3009e8240 {Error Domain=HAPErrorDomain Code=15 "Failed to pair Matter Accessory in time" UserInfo={NSLocalizedDescription=Failed to pair Matter Accessory in time, NSUnderlyingError=0x3009e86c0 {Error Domain=HAPErrorDomain Code=24 "(null)"}}}}. From the log I can see, the Function selectWiFiNetwork(from wifiScanResults: [MatterAddDeviceExtensionRequestHandler.WiFiScanResult]) async throws -> MatterAddDeviceExtensionRequestHandler.WiFiNetworkAssociation of the RequestHandler.swift file will be called and commissionDevice(in home: MatterAddDeviceRequest.Home?, onboardingPayload: String, commissioningID: UUID) will NOT called. Sometimes the RequestHandler class will not be called at all, this means the first function validateDeviceCredential will not be called. But ,when this happens, the iPhone will Continuously can NOT add device for a long time, sometimes it gets better inexplicably. Even under normal circumstances, the probability of successfully adding a device is less than 50% and very slowly. When using Apple Home app, the success rate is very high, almost 100%, and very fast.

So, can you help me identify where the problem is and what areas need attention? Thank you very much!

Finally, I often encounter logs like this: nw_browser_dns_service_browse_callback [B3] not in ready or waiting state Do you know what it means and whether it will affect the equipment distribution network? My testing machine is iPhone 12, operating on iOS 18 and above 17.0. Thank you very much!

The Matter device's log maybe help, and the log is:

[ 9695][BLE][PROGR] using BTP fragment sizes rx 244 / tx 244.

[ 9814][DL][PROGR] CHIPoBLE connection established (ConnId: 0x00, GATT MTU)

[ 9816][-][PROGR] BLE connection established

[ 9876][EM][PROGR] >>> [E:40235r S:0 M:161254904] (U) Msg RX from 0:21F70A)

[ 9882][EM][PROGR] <<< [E:40235r S:0 M:120570087] (U) Msg TX to 0:00000000)

[ 9884][IN][PROGR] (U) Sending msg 120570087 to IP address 'BLE'

[ 10294][EM][PROGR] >>> [E:40235r S:0 M:161254905] (U) Msg RX from 0:21F70A)

[ 10334][DL][PROGR] wifiInterface_eventHeartbeat

[ 10531][EM][PROGR] <<< [E:40235r S:0 M:120570088] (U) Msg TX to 0:00000000)

[ 10534][IN][PROGR] (U) Sending msg 120570088 to IP address 'BLE'

[ 10537][DL][ERROR] Long dispatch time: 244 ms, for event type 16388

[ 10595][EM][PROGR] >>> [E:40235r S:0 M:161254906] (U) Msg RX from 0:21F70A)

[ 10600][EM][PROGR] <<< [E:40235r S:0 M:120570089] (U) Msg TX to 0:00000000)

[ 10602][IN][PROGR] (U) Sending msg 120570089 to IP address 'BLE'

[ 10609][SC][PROGR] SecureSession[0x260084c0, LSID:4176]: State change 'kEs'

[ 10610][SVR][PROGR] Commissioning completed session establishment step

[ 10612][DIS][PROGR] Updating services using commissioning mode 0

[ 10614][DIS][PROGR] CHIP minimal mDNS started advertising.

[ 10616][DIS][PROGR] Advertise commission parameter vendorID=5127 productID0

[ 10622][DIS][PROGR] CHIP minimal mDNS configured as 'Commissionable node d.

[ 10624][DIS][PROGR] mDNS service published: _matterc._udp

[ 10625][SVR][PROGR] Device completed Rendezvous process

[ 10657][EM][PROGR] >>> [E:40236r S:4176 M:187989483] (S) Msg RX from 0:FFF)

[ 10664][EM][PROGR] <<< [E:40236r S:4176 M:227606736] (S) Msg TX to 0:FFFFF)

[ 10666][IN][PROGR] (S) Sending msg 227606736 on secure session with LSID: 6

[ 10918][DL][ERROR] Long dispatch time: 257 ms, for event type 3

[ 10923][EM][PROGR] >>> [E:40237r S:4176 M:187989484] (S) Msg RX from 0:FFF)

[ 10931][EM][PROGR] <<< [E:40237r S:4176 M:227606737] (S) Msg TX to 0:FFFFF)

[ 10933][IN][PROGR] (S) Sending msg 227606737 on secure session with LSID: 6

[ 11183][DL][ERROR] Long dispatch time: 256 ms, for event type 3

[ 11187][EM][PROGR] >>> [E:40238r S:4176 M:187989485] (S) Msg RX from 0:FFF)

[ 11196][EM][PROGR] <<< [E:40238r S:4176 M:227606738] (S) Msg TX to 0:FFFFF)

[ 11198][IN][PROGR] (S) Sending msg 227606738 on secure session with LSID: 6

[ 11448][DL][ERROR] Long dispatch time: 255 ms, for event type 3

[ 11452][EM][PROGR] >>> [E:40239r S:4176 M:187989486] (S) Msg RX from 0:FFF)

[ 11460][EM][PROGR] <<< [E:40239r S:4176 M:227606739] (S) Msg TX to 0:FFFFF)

[ 11463][IN][PROGR] (S) Sending msg 227606739 on secure session with LSID: 6

[ 11712][DL][ERROR] Long dispatch time: 256 ms, for event type 3

[ 11716][EM][PROGR] >>> [E:40240r S:4176 M:187989487] (S) Msg RX from 0:FFF)

[ 14667][DL][PROGR] wifiInterface_eventScanDone

[ 14668][DL][PROGR] No. rssi ssid

[ 14669][DL][PROGR] 0 -31 Niva

[ 14670][DL][PROGR] 1 -39 3R-ZY

[ 14671][DL][PROGR] 2 -39 SPEAKER_TEST

[ 14671][DL][PROGR] 3 -46 HUAWEI-HUBV3

[ 14672][DL][PROGR] 4 -49 3R-VPN-NEW

[ 14673][DL][PROGR] 5 -49 TEST_1q2w3e4r

[ 14674][DL][PROGR] 6 -51 ASUS-3R

[ 14674][DL][PROGR] 7 -54 3R-Software

[ 14675][DL][PROGR] 8 -56 3R-Dlink

[ 14676][DL][PROGR] 9 -56 3R-Zhen

[ 14677][DL][PROGR] 10 -57 3reality

[ 14677][DL][PROGR] 11 -58 SB_ASUS

[ 14678][DL][PROGR] 12 -60 GoogleWiFi

[ 14679][DL][PROGR] 13 -60 3R-VPN

[ 14679][DL][PROGR] 14 -62 ChinaNet-cGwf

[ 14680][DL][PROGR] 15 -66 3R-E1200

[ 14681][DL][PROGR] 16 -78 QK-GUEST

[ 14682][DL][PROGR] 17 -78 QK-CORP

[ 14682][DL][PROGR] 18 -78 QK-IOT

[ 14683][DL][PROGR] 19 -79 QK-DEV

[ 14684][DL][PROGR] 20 -83 3RE-SPK-00E93A85B963

[ 14685][DL][PROGR] ScheduleLambda OK

[ 14686][DL][ERROR] Long dispatch time: 2973 ms, for event type 16388

[ 14694][EM][PROGR] <<< [E:40240r S:4176 M:227606740] (S) Msg TX to 0:FFFFF)

[ 14696][IN][PROGR] (S) Sending msg 227606740 on secure session with LSID: 6

[ 15338][DL][PROGR] wifiInterface_eventHeartbeat

[ 20336][DL][PROGR] wifiInterface_eventHeartbeat

[ 23814][EM][PROGR] >>> [E:40241r S:4176 M:187989488] (S) Msg RX from 0:FFF)

[ 23822][EM][PROGR] <<< [E:40241r S:4176 M:227606741] (S) Msg TX to 0:FFFFF)

[ 23824][IN][PROGR] (S) Sending msg 227606741 on secure session with LSID: 6

[ 24070][DL][ERROR] Long dispatch time: 251 ms, for event type 3

[ 24074][EM][PROGR] >>> [E:40242r S:4176 M:187989489] (S) Msg RX from 0:FFF)

[ 24081][EM][PROGR] <<< [E:40242r S:4176 M:227606742] (S) Msg TX to 0:FFFFF)

[ 24083][IN][PROGR] (S) Sending msg 227606742 on secure session with LSID: 6

[ 24327][DL][ERROR] Long dispatch time: 248 ms, for event type 3

[ 25333][DL][PROGR] wifiInterface_eventHeartbeat

[ 30338][DL][PROGR] wifiInterface_eventHeartbeat

[ 30625][DIS][PROGR] Updating services using commissioning mode 0

[ 30627][DIS][PROGR] CHIP minimal mDNS started advertising.

[ 35335][DL][PROGR] wifiInterface_eventHeartbeat

[ 40333][DL][PROGR] wifiInterface_eventHeartbeat

[ 45331][DL][PROGR] wifiInterface_eventHeartbeat

[ 50335][DL][PROGR] wifiInterface_eventHeartbeat

[ 55333][DL][PROGR] wifiInterface_eventHeartbeat

[ 60331][DL][PROGR] wifiInterface_eventHeartbeat

[ 65335][DL][PROGR] wifiInterface_eventHeartbeat

[ 70333][DL][PROGR] wifiInterface_eventHeartbeat

[ 70625][FS][PROGR] Fail-safe timer expired

[ 70627][SVR][ERROR] Failsafe timer expired

[ 70628][SC][PROGR] SecureSession[0x260084c0, LSID:4176]: State change 'kAc'

[ 70629][SVR][ERROR] Commissioning failed (attempt 1): 32

when wait for more then 10s, then click "add to [ecosystem]", the process will success.BUt, when click the button quickly, the process will be fail

Anyone helps?

Using MatterSupport framework to add Matter device to our system
 
 
Q