---
description: >-
  The User Device Management (UDM) module provides functionality to manage and
  track user devices, including registration, monitoring, and removal of
  devices.
---

# User Device Management (UDM)

### Table of Contents

1. registerDevice
2. getLinkDevices
3. getLinkDevice
4. updateLastOnline
5. updateDeviceName
6. removeDevice
7. getLinkDevicesHistory

### Methods

#### registerDevice

Registers a new device with the system.

Parameters:

* `body`: A `RegisterDeviceRequest` object containing:
  * `uniqueId`: The unique identifier of the device
  * `uniqueIdType`: The type of unique identifier ( "uuid", "imei")
  * `label`: The device label
  * `type`: The device type ("mobile", "cloud", "browser")
  * `customName`: The custom name of the device
  * `os`: The operating system version
  * `browserName`: The browser name (if applicable)

```kotlin
fun registerDevice(context: Context, username: String) {
    viewModelScope.launch {
        passkey?.authenticate(username)
            ?.flatMapLatest {
                udmRepository = UDMRepositoryImpl("https://trust-fabric.mpc-dev.cramiumtech.com", it.accessToken)
                udmRepository!!.registerDevice(context)
            }
            ?.collect { response ->
                Log.d("MainActivityViewModel", "Register device response: $response")
            }
    }
}
```

#### getLinkDevices

Retrieves a list of all linked devices for the current user.

```kotlin
fun getLinkDevices() {
    viewModelScope.launch {
        udmRepository?.getLinkDevices()
            ?.collect { devices ->
                Log.d("MainActivityViewModel", "Link devices response: $devices")
            }
    }
}
```

#### getLinkDevice

Retrieves details of a specific linked device.

Parameters:

* `uniqueId`: The unique identifier of the device

```kotlin
fun getLinkDevice(uniqueId: String) {
    viewModelScope.launch {
        udmRepository?.getLinkDevice(uniqueId)
            ?.collect { device ->
                Log.d("MainActivityViewModel", "Link device response: $device")
            }
    }
}
```

#### updateLastOnline

Updates the last online timestamp for a specific device.

Parameters:

* `uniqueId`: The unique identifier of the device

```kotlin
fun updateLastOnline(uniqueId: String) {
    viewModelScope.launch {
        udmRepository?.updateLastOnline(uniqueId)
            ?.collect {
                Log.d("MainActivityViewModel", "Last online updated for device: $uniqueId")
            }
    }
}
```

#### updateDeviceName

Updates the display name of a specific device.

Parameters:

* `uniqueId`: The unique identifier of the device
* `name`: The new name for the device

```kotlin
fun updateDeviceName(uniqueId: String, name: String) {
    viewModelScope.launch {
        udmRepository?.updateDeviceName(uniqueId, name)
            ?.collect {
                Log.d("MainActivityViewModel", "Device name updated for device: $uniqueId")
            }
    }
}
```

#### removeDevice

Removes a device from the linked devices list.

Parameters:

* `uniqueId`: The unique identifier of the device

```kotlin
fun unlink(username: String) {
    viewModelScope.launch {
        passkey?.authenticate(username)
            ?.flatMapLatest {
                udmRepository = UDMRepositoryImpl("https://trust-fabric.mpc-dev.cramiumtech.com", it.accessToken)
                udmRepository!!.getLinkDevices()
            }
            ?.flatMapLatest { devices ->
                udmRepository!!.removeDevice(devices.last().uniqueId)
            }
            ?.collect {
                Log.d("MainActivityViewModel", "Device removed successfully")
            }
    }
}
```

#### getLinkDevicesHistory

Retrieves the history of device linking and unlinking events.

```kotlin
fun getLinkDevicesHistory() {
    viewModelScope.launch {
        udmRepository?.getLinkDevicesHistory()
            ?.collect { history ->
                Log.d("MainActivityViewModel", "Link devices history response: $history")
            }
    }
}
```

### Complete Example

Here's a complete example of how to use the UDM features:

```kotlin
// Initialize the repository
val udmRepository = UDMRepositoryImpl(
    baseUrl = "https://trust-fabric.mpc-dev.cramiumtech.com",
    accessToken = "your_access_token"
)

// Register a new device
viewModelScope.launch {
    udmRepository.registerDevice(
        RegisterDeviceRequest(
            uniqueId = context.getDeviceUuid(),
            uniqueIdType = "uuid",
            label = Build.DEVICE,
            type = "mobile",
            customName = Build.MODEL,
            os = Build.VERSION.RELEASE,
            browserName = "browser"
        )
    ).collect { device ->
        // Handle the registered device
    }
}

// Get all linked devices
viewModelScope.launch {
    udmRepository.getLinkDevices().collect { devices ->
        // Handle the list of devices
    }
}

// Update device name
viewModelScope.launch {
    udmRepository.updateDeviceName(
        uniqueId = "device_id",
        name = "New Device Name"
    ).collect {
        // Handle the update result
    }
}

// Remove a device
viewModelScope.launch {
    udmRepository.removeDevice("device_id").collect {
        // Handle the removal result
    }
}
```
