Matter: Build a Matter IoT app with SmartThings Home API


Objective

Learn how to create an IoT app to onboard, control, remove, and share Matter devices using SmartThings Home APIs.

Overview

Matter is an open-source connectivity standard for smart home and Internet of Things (IoT) devices. It is a secure, reliable, and seamless cross-platform protocol for connecting compatible devices and systems with one another.

SmartThings provides the Matter virtual device app and SmartThings Home APIs to help you quickly develop Matter devices and use the SmartThings ecosystem without needing to build your own IoT ecosystem.

You can use SmartThings Home APIs to onboard, control, remove, and share all Matter devices when building your application. Other IoT ecosystems can use the Matter devices onboarded on your IoT app through the Multi-Admin function.

For detailed information, see Matter in SmartThings Developers.

Set up your environment

You will need the following:

  • Host PC running on Windows 10 (or higher) or Ubuntu 20.04 (x64)
  • Android Studio (latest version recommended)
  • Java SE Development Kit (JDK) 11 or later
  • Devices connected on the same network:
    • Mobile device with Matter virtual device app installed
    • Mobile device with Developer Mode and USB Debugging enabled
    • Matter-enabled SmartThings Station onboarded with Samsung account used for SmartThings app

Sample Code

Start your project

After downloading the sample code containing the project files, open your Android Studio and click Open to open an existing project.

Locate the downloaded Android project from the directory and click OK.

Commission the device

You can onboard a Matter-compatible device to the IoT app by calling the commissionDevice API, which can lay the groundwork to control, remove, and share the device.

Go to app > java > com.samsung.android.matter.home.sample > feature > main.

In the MainViewModel.kt file, call the commissionDevice API to launch the onboarding activity and join the device to the SmarthThings fabric:

// =================================================================================
// CODELAB
// Step 1: Create an instance of Matter Commissioning client
// Step 2: Call the commissionDevice API to launch the Onboarding activity in Home Service.
// Step 3: Set _intentSender.value from return value of commissionDevice API
// TODO 1: Device commission and join a device to SmartThings fabric
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//val commissioningClient = Matter.getCommissioningClient()
//val intentSender = commissioningClient.commissionDevice(context)
//_intentSender.value = intentSender
// =================================================================================

Control the device

Capabilities are core to the SmartThings architecture. They abstract specific devices into their underlying functions, which allows the retrieval of the state of a device component or control of the device function.

Each device has its own appropriate capabilities. Therefore, each device has a different control API, and the more functions it supports, the more APIs it has to use.

In this step, select a device type that you want to onboard and control using necessary Home APIs. The level of modification complexity is assigned per each device.

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: ContactSensorViewModel.kt

// =================================================================================
// CODELAB Level 1
// Step 1: Get ContactSensor capability from device instance
// Step 2: Get stream openClose value from ContactSensor capability
// Step 3: Set _openClose.value for UI updating
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(ContactSensor)?.openClose?.collect { openClose ->
//    _openClose.value = openClose
//}
// =================================================================================
// =================================================================================
// CODELAB Level 1
// Step 1: Get Battery capability from device instance
// Step 2: Get stream battery value from Battery capability
// Step 3: Set _batteryStatus.value for UI updating
// TODO 2: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(Battery)?.battery?.collect { battery ->
//    _batteryStatus.value = battery
//}
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: MotionSensorViewModel.kt

// =================================================================================
// CODELAB Level 1
// Step 1: Get MotionSensor capability from device instance
// Step 2: Get stream occupied value from MotionSensor capability
// Step 3: Set _motionOccupied.value for UI updating
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(MotionSensor)?.occupied?.collect { motionOccupied ->
//    _motionOccupied.value = motionOccupied
//    Timber.d("Occupied= $motionOccupied")
//}
// =================================================================================
// =================================================================================
// CODELAB Level 1
// Step 1: Get Battery capability from device instance
// Step 2: Get stream battery value from Battery capability
// Step 3: Set _batteryStatus.value for UI updating
// TODO 2: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(Battery)?.battery?.collect { battery ->
//    _batteryStatus.value = battery
//    Timber.d("battery= $battery")
//}
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: SwitchViewModel.kt

// =================================================================================
// CODELAB Level 2
// Step 1: Get Switch capability from device instance
// Step 2: Get stream onOff value from Switch capability
// Step 3: Set _onOff.value for UI updating
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(Switch)?.onOff?.collect { onOff ->
//    _onOff.value = onOff
//}
// =================================================================================
// =================================================================================
// CODELAB Level 2
// Step 1: Get Switch capability from device instance
// Step 2: Control the device using the APIs that supported by Switch capability
//         Toggle Switch state based on onOff value
//             onOff == true : call switch off
//             onOff == false : call switch on
// TODO 2: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//device.readCapability(Switch)?.let { switch ->
//    if (onOff == true) {
//        switch.off()
//    } else {
//        switch.on()
//    }
//}
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: SmartLockViewModel.kt

// =================================================================================
// CODELAB Level 3
// Step 1: Get Lock capability from device instance
// Step 2: Get stream lockUnlock value from Lock capability
// Step 3: Set _lockStatus.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 1: Copy code below
device.readCapability(Lock)?.lockUnlock?.collect { lockUnlock ->
    _lockStatus.value = lockUnlock
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get TamperAlert capability from device instance
// Step 2: Get stream tamperAlert value from TamperAlert capability
// Step 3: Set _tamperStatus.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 2: Copy code below
device.readCapability(TamperAlert)?.tamperAlert?.collect { tamperAlert ->
    _tamperStatus.value = tamperAlert
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get Battery capability from device instance
// Step 2: Get stream battery value from Battery capability
// Step 3: Set _batteryStatus.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 3: Copy code below
device.readCapability(Battery)?.battery?.collect { battery ->
    _batteryStatus.value = battery
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get lock capability from device instance
// Step 2: Control the device using the APIs that supported by lock capability
//         Toggle lock state based on lockUnlock value
//             lockUnlock == true : call lock
//             lockUnlock == false : call unlock
// ---------------------------------------------------------------------------------

// TODO 4: Copy code below
device.readCapability(Lock)?.let { lock ->
    if (lockUnlock == true) {
        lock.lock()
    } else {
        lock.unlock()
    }
}
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: BlindViewModel.kt

// =================================================================================
// CODELAB Level 3
// Step 1: Get WindowShade capability from device instance
// Step 2: Get stream windowShadeMode value from WindowShade capability
// Step 3: Set _windowShadeMode.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 1: Copy code below
device.readCapability(WindowShade)?.windowShadeMode?.collect { windowShadeMode ->
    Timber.d("windowShadeMode:$windowShadeMode")
    _windowShadeMode.value = windowShadeMode
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get WindowShadeLevel capability from device instance
// Step 2: Get stream shadeLevel value from WindowShadeLevel capability
// Step 3: Set _shadeLevel.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 2: Copy code below
device.readCapability(WindowShadeLevel)?.shadeLevel?.collect { shadeLevel ->
    Timber.d("shadeLevel:$shadeLevel")
    _shadeLevel.value = shadeLevel
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get Battery capability from device instance
// Step 2: Get stream battery value from Battery capability
// Step 3: Set _batteryStatus.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 3: Copy code below
device.readCapability(Battery)?.battery?.collect { battery ->
    _batteryStatus.value = battery
}
// =================================================================================
// =================================================================================
// CODELAB Level 3
// Step 1: Get WindowShade capability from device instance
// Step 2: Control the device using the APIs that supported by WindowShade capability
//         Send windowShade open/close/pause command based on controlCommand value
//             "open" : call windowShade open
//             "close" : call windowShade close
//             "pause" : call windowShade pause
// ---------------------------------------------------------------------------------

// TODO 4: Copy code below
device.readCapability(WindowShade)?.let { windowShade ->
  when(controlCommand){
      "open" ->  windowShade.open()
      "close" ->  windowShade.close()
      "pause" ->  windowShade.pause()
  }
}
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: LightViewModel.kt

// =================================================================================
// CODELAB Level 4
// Step 1: Get Switch capability from device instance
// Step 2: Get stream onOff value from Switch capability
// Step 3: Set _onOff.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 1: Copy code below
device.readCapability(Switch)?.onOff?.collect { onOff ->
    _onOff.value = onOff
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get SwitchLevel capability from device instance
// Step 2: Get stream level value from SwitchLevel capability
// Step 3: Set _level.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 2: Copy code below
device.readCapability(SwitchLevel)?.level?.collect { level ->
    _level.value = level
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get Switch capability from device instance
// Step 2: Control the device using the APIs that supported by Switch capability
//         Toggle Switch state based on onOff value
//             onOff == true : call switch off
//             onOff == false : call switch on
// ---------------------------------------------------------------------------------

// TODO 3: Copy code below
device.readCapability(Switch)?.let { switch ->
    if (onOff == true) {
        switch.off()
    } else {
        switch.on()
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get SwitchLevel capability from device instance
// Step 2: Control the device using the APIs that supported by SwitchLevel capability
//         Call SwitchLevel setLevel with percentage value
// ---------------------------------------------------------------------------------

// TODO 4: Copy code below
Timber.d("Target position = $percentage")
device.readCapability(SwitchLevel)?.setLevel(percentage)
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get ColorControl capability from device instance
// Step 2: Control the device using the APIs that supported by ColorControl capability
//         Call ColorControl setColor with hue,saturation value
// ---------------------------------------------------------------------------------

// TODO 5: Copy code below
Timber.d("hue:$hue,saturation:$saturation")
device.readCapability(ColorControl)?.setColor(hue.toDouble(), saturation.toDouble())
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get ColorTemperature capability from device instance
// Step 2: Control the device using the APIs that supported by ColorTemperature capability
//         Call ColorTemperature setColorTemperature with temperature
// ---------------------------------------------------------------------------------

// TODO 6: Copy code below
device.readCapability(ColorTemperature)?.setColorTemperature(temperature)
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: TelevisionViewModel.kt

// =================================================================================
// CODELAB Level 4
// Step 1: Get Switch capability from device instance
// Step 2: Get stream onOff value from Switch capability
// Step 3: Set _onOff.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 1: Copy code below
device.readCapability(Switch)?.onOff?.collect { onOff ->
    _onOff.value = onOff
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get MediaPlayback capability from device instance
// Step 2: Get stream mediaPlayBackState value from MediaPlayback capability
// Step 3: Set _mediaPlayBackState.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 2: Copy code below
device.readCapability(MediaPlayback)?.mediaPlayBackState?.collect { mediaPlayBackState ->
    _mediaPlayBackState.value = mediaPlayBackState
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get Switch capability from device instance
// Step 2: Control the device using the APIs that supported by Switch capability
//         Toggle Switch state based on onOff value
//             onOff == true : call switch off
//             onOff == false : call switch on
// ---------------------------------------------------------------------------------

// TODO 3: Copy code below
device.readCapability(Switch)?.let { switch ->
    if (onOff == true) {
        switch.off()
    } else {
        switch.on()
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get MediaPlayback capability from device instance
// Step 2: Control the device using the APIs that supported by MediaPlayback capability
//         Send MediaPlayback play/pause/stop/rewind/fastForward command based on controlCommand value
//             "play" : call mediaPlayback play and setPlaybackStatus with PlaybackStatus.PLAYING
//             "pause" : call mediaPlayback pause and setPlaybackStatus with PlaybackStatus.PAUSED
//             "stop" : call mediaPlayback stop and setPlaybackStatus with PlaybackStatus.STOPPED
//             "rewind" : call mediaPlayback rewind and setPlaybackStatus with PlaybackStatus.REWINDING
//             "fastForward" : call mediaPlayback fastForward and setPlaybackStatus with PlaybackStatus.FASTFORWARDING
// ---------------------------------------------------------------------------------

// TODO 4: Copy code below
device.readCapability(MediaPlayback)?.let { mediaPlayback ->
    when (controlCommand) {
        "play" -> {
            mediaPlayback.play()
            mediaPlayback.setPlaybackStatus(PlaybackStatus.PLAYING)
        }
        "pause" -> {
            mediaPlayback.pause()
            mediaPlayback.setPlaybackStatus(PlaybackStatus.PAUSED)
        }
        "stop" -> {
            mediaPlayback.stop()
            mediaPlayback.setPlaybackStatus(PlaybackStatus.STOPPED)
        }
        "rewind" -> {
            mediaPlayback.rewind()
            mediaPlayback.setPlaybackStatus(PlaybackStatus.REWINDING)
        }
        "fastForward" -> {
            mediaPlayback.fastForward()
            mediaPlayback.setPlaybackStatus(PlaybackStatus.FASTFORWARDING)
        }
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get MediaTrackControl capability from device instance
// Step 2: Control the device using the APIs that supported by MediaTrackControl capability
//         Send MediaTrack nextTrack/previousTrack command based on controlCommand value
//             "nextTrack" : call mediaTrack nextTrack
//             "previousTrack" : call mediaTrack previousTrack
// ---------------------------------------------------------------------------------

// TODO 5: Copy code below
device.readCapability(MediaTrackControl)?.let { mediaTrack ->
    when (controlCommand) {
        "nextTrack" -> mediaTrack.nextTrack()
        "previousTrack" -> mediaTrack.previousTrack()
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 4
// Step 1: Get KeypadInput capability from device instance
// Step 2: Control the device using the APIs that supported by KeypadInput capability
//         Call KeypadInput sendKey with key value
// ---------------------------------------------------------------------------------

// TODO 6: Copy code below
device.readCapability(KeypadInput)?.sendKey(key)
// =================================================================================

File path: app > java > com.samsung.android.matter.home.sample > feature > device
File name: ThermostatViewModel.kt

// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatMode capability from device instance
// Step 2: Get stream thermostatMode value from ThermostatMode capability
// Step 3: Set _systemMode.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 1: Copy code below
device.readCapability(ThermostatMode)?.thermostatMode?.collect { thermostatMode ->
    Timber.d("viewmodel:  thermostatMode $thermostatMode")
    _systemMode.value = thermostatMode
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatFanMode capability from device instance
// Step 2: Get stream thermostatFanMode value from ThermostatFanMode capability
// Step 3: Set _fanMode.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 2: Copy code below
device.readCapability(ThermostatFanMode)?.thermostatFanMode?.collect { fanMode ->
    _fanMode.value = fanMode
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get TemperatureMeasurement capability from device instance
// Step 2: Get stream temperature value from TemperatureMeasurement capability
// Step 3: Set _temperature.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 3: Copy code below
device.readCapability(TemperatureMeasurement)?.temperature?.collect { temperature ->
    _temperature.value = temperature
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatCoolingSetpointBattery capability from device instance
// Step 2: Get stream coolingSetpoint value from ThermostatCoolingSetpoint capability
// Step 3: Set _occupiedCoolingSetpoint.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 4: Copy code below
device.readCapability(ThermostatCoolingSetpoint)?.coolingSetpoint?.collect { coolingSetpoint ->
    _occupiedCoolingSetpoint.value = coolingSetpoint
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatHeatingSetpoint capability from device instance
// Step 2: Get stream heatingSetpoint value from ThermostatHeatingSetpoint capability
// Step 3: Set _occupiedHeatingSetpoint.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 5: Copy code below
device.readCapability(ThermostatHeatingSetpoint)?.heatingSetpoint?.collect { heatingSetpoint ->
    _occupiedHeatingSetpoint.value = heatingSetpoint
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get RelativeHumidityMeasurement capability from device instance
// Step 2: Get stream humidity value from RelativeHumidityMeasurement capability
// Step 3: Set _humidity.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 6: Copy code below
device.readCapability(RelativeHumidityMeasurement)?.humidity?.collect { humidity ->
    _humidity.value = humidity
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get Battery capability from device instance
// Step 2: Get stream battery value from Battery capability
// Step 3: Set _batteryStatus.value for UI updating
// ---------------------------------------------------------------------------------

// TODO 7: Copy code below
device.readCapability(Battery)?.battery?.collect { battery ->
    _batteryStatus.value = battery
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatMode capability from device instance
// Step 2: Control the device using the APIs that supported by ThermostatMode capability
//         Change ThermostatMode state based on systemMode value
//             ThermostatSystemMode.Off : call thermostatMode off
//             ThermostatSystemMode.Cool : call thermostatMode cool
//             ThermostatSystemMode.Heat : call thermostatMode heat
//             ThermostatSystemMode.Auto : call thermostatMode auto
// ---------------------------------------------------------------------------------

// TODO 8: Copy code below
device.readCapability(ThermostatMode)?.let { thermostatMode ->
    when (systemMode) {
        ThermostatSystemMode.Off -> thermostatMode.off()
        ThermostatSystemMode.Cool -> thermostatMode.cool()
        ThermostatSystemMode.Heat -> thermostatMode.heat()
        ThermostatSystemMode.Auto -> thermostatMode.auto()
    }
}
// =================================================================================
// =================================================================================
// Step 1: Get ThermostatFanMode capability from device instance
// Step 2: Control the device using the APIs that supported by ThermostatFanMode capability
//         Change ThermostatFanMode state based on fanMode value
//             ThermostatFanModeEnum.Auto : call thermostatFanMode fanAuto
//             ThermostatFanModeEnum.Circulate : call thermostatFanMode fanCirculate
//             ThermostatFanModeEnum.FollowSchedule : call thermostatFanMode setThermostatFanMode with followSchedule
//             ThermostatFanModeEnum.On : call thermostatFanMode fanOn
// ---------------------------------------------------------------------------------

// TODO 9: Copy code below
device.readCapability(ThermostatFanMode)?.let { thermostatFanMode ->
    when (fanMode) {
        ThermostatFanModeEnum.Auto -> thermostatFanMode.fanAuto()
        ThermostatFanModeEnum.Circulate -> thermostatFanMode.fanCirculate()
        ThermostatFanModeEnum.FollowSchedule -> thermostatFanMode.setThermostatFanMode("followSchedule")
        ThermostatFanModeEnum.On -> thermostatFanMode.fanOn()
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatHeatingSetpoint capability value from device instance
// Step 2: Increase _occupiedHeatingSetpoint.value that have current heating value
// Step 3: if new increased value is under VALUE_MAX(104),
//         call setHeatingSetpoint of ThermostatHeatingSetpoint capability with new increased value
// ---------------------------------------------------------------------------------

// TODO 10: Copy code below
device.readCapability(ThermostatHeatingSetpoint)?.let { heatingSetpoint ->
    val nextValue = _occupiedHeatingSetpoint.value!! + 1

    if (nextValue < VALUE_MAX) {
        heatingSetpoint.setHeatingSetpoint(nextValue)
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatHeatingSetpoint capability value from device instance
// Step 2: Decrease _occupiedHeatingSetpoint.value that have current heating value
// Step 3: if new decreased value is over VALUE_MIN(32),
//         call setHeatingSetpoint of ThermostatHeatingSetpoint capability with new decreased value
// ---------------------------------------------------------------------------------

// TODO 11: Copy code below
device.readCapability(ThermostatHeatingSetpoint)?.let { heatingSetpoint ->
    val nextValue = _occupiedHeatingSetpoint.value!! - 1

    if (nextValue > VALUE_MIN) {
        heatingSetpoint.setHeatingSetpoint(nextValue)
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatCoolingSetpoint capability value from device instance
// Step 2: Increase _occupiedCoolingSetpoint.value that have current cooling value
// Step 3: if new increased value is under VALUE_MAX(104),
//         call setCoolingSetpoint of ThermostatCoolingSetpoint capability with new increased value
// ---------------------------------------------------------------------------------

// TODO 12: Copy code below
device.readCapability(ThermostatCoolingSetpoint)?.let { coolingSetpoint ->
    val nextValue = _occupiedCoolingSetpoint.value!! + 1

    if (nextValue < VALUE_MAX) {
        coolingSetpoint.setCoolingSetpoint(nextValue)
    }
}
// =================================================================================
// =================================================================================
// CODELAB Level 5
// Step 1: Get ThermostatCoolingSetpoint capability value from device instance
// Step 2: Decrease _occupiedCoolingSetpoint.value that have current cooling value
// Step 3: if new decreased value is over VALUE_MIN(32),
//         call setCoolingSetpoint of ThermostatCoolingSetpoint capability with new decreased value
// ---------------------------------------------------------------------------------

// TODO 13: Copy code below
device.readCapability(ThermostatCoolingSetpoint)?.let { coolingSetpoint ->
    val nextValue = _occupiedCoolingSetpoint.value!! - 1

    if (nextValue > VALUE_MIN) {
        coolingSetpoint.setCoolingSetpoint(nextValue)
    }
}
// =================================================================================

Remove the device

Using removeDevice API, you can remove the device from your IoT app and the SmartThings fabric.

Go to app > java > com.samsung.android.matter.home.sample > feature > device > base.

In the BaseViewModel.kt file, call the removeDevice API to launch the RemoveDevice activity:

// =================================================================================
// [CODELAB] Device remove from SmartThings fabric
// Step 1: Create an instance of Matter Commissioning client
// Step 2: Call the removeDevice API to launch the RemoveDevice activity in Home Service
// Step 3: Set _intentSender.value from return value of removeDevice API
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//val commissioningClient = Matter.getCommissioningClient()
//val intentSender = commissioningClient.removeDevice(
//    context,
//    CommissioningClient.RemoveDeviceRequest(deviceId)
//)
//_intentSenderForRemoveDevice.value = intentSender
// =================================================================================

Share the device

Matter devices can be shared with other Matter-compatible IoT Platforms, such as Google Home, using Home APIs without resetting the device while connected to SmartThings. To perform this operation, it is necessary to enter the commissioning mode without resetting the device.

In the BaseViewModel.kt file, call the shareDevice API to launch the ShareDevice activity:

// =================================================================================
// [CODELAB] Device share to other platforms
// Step 1: Create an instance of Matter Commissioning client
// Step 2: Call the shareDevice API to launch the ShareDevice activity in Home Service
// Step 3: Set _intentSender.value from return value of shareDevice API
// TODO 1: Uncomment the following code blocks
// ---------------------------------------------------------------------------------
//val commissioningClient = Matter.getCommissioningClient()
//val intentSender = commissioningClient.shareDevice(
//    context,
//    CommissioningClient.ShareDeviceRequest(deviceId)
//)
//_intentSenderForShareDevice.value = intentSender
// =================================================================================

Build and run the IoT app

Run the sample app

To build and run your IoT app, follow these steps:

  1. Using a USB cable, connect your mobile device.
  2. Select the sample IoT app from the run configurations menu in the Android Studio.
  3. Then, select the connected device in the target device menu.
  4. Click Run.

Onboard

To onboard the virtual device:

  1. Go to the Matter virtual device app.
  2. Then, select and set up the virtual device type you want to control. Click Save.
  3. Click Start to show the QR Code for onboarding.

  1. Go to the sample IoT app and click the + button.
  2. Then, onboard the virtual device by scanning its QR Code.

Control by sample IoT app

In the sample IoT app, control the virtual device by using its various functions such as on and off for switch.

You're done!

Congratulations! You have successfully achieved the goal of this Code Lab topic. Now, you can create a Matter-compatible IoT application with SmartThings Home APIs by yourself!

Learn more by going to SmartThings Matter libraries.