# Device

## Activate

To activate the current device for data collection, the following API can be used.

#### Concrete Example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DeviceService()
service.activate(then: { result in
    switch result {
    case: .success(_)
        // activation successful
    case: .failure(_)
        // failure error
    }
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val service = DeviceService()
service.activate() { result: Result<Content?>? ->
    if (result?.value == null || result.throwable != null) {
        // failure
    } else {
        // Success
        val content = result.value
    }
})
```

{% endtab %}
{% endtabs %}

## Deactivate

Deactivate the current device for data collection. It may be desirable to deactivate a device if the host application's user logs out, or if the user becomes inactive.

To deactivate the current device for data collection, the following API can be used.

#### Concrete Example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DeviceService()
service.deactivate(then: { result in
    switch result {
    case: .success(_)
        // deactivation successful
    case: .failure(_)
        // failure error
    }
})
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val service = DeviceService()
service.deactivate() { result: Result<Content?>? ->
    if (result?.value == null || result.throwable != null) {
        // failure
    } else {
        // Success
        val content = result.value
    }
})
```

{% endtab %}
{% endtabs %}

## Fetch

In order to fetch the current`Device`'s model, the following API can be used.

#### Concrete Example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DeviceService()
service.fetch(then: { result in
    guard !result.value.isEmpty else {
        // error
        return
    }
    completionHandler(.success(result.value))
})
```

{% endtab %}
{% endtabs %}

## Verify And Re-Activate Device

To verify whether the current device is activated or to reactivate it if needed, please refer to the example below.

#### Example

{% tabs %}
{% tab title="Swift" %}

```swift
let service = DeviceService()
deviceService.fetch { result in
    switch result {
    case .success:
        guard let device = result.value else {
            // DEVICE DOES NOT EXIST -> IS NOT ACTIVATED -> Re-ACTIVATE
            deviceService.activate(then: { result in
                switch result {
                case: .success(_)
                    // activation successful
                case: .failure(_)
                    // failure error
                }
            })
            return
        }
        guard device.isDataCollectionEnabled ?? false else {
            // DEVICE IS PRESENT -> BUT NOT ACTIVATED -> Re-ACTIVATE
            deviceService.activate(then: { result in
                switch result {
                case: .success(_)
                    // activation successful
                case: .failure(_)
                    // failure error
                }
            })
            return
        }
        // DEVICE IS ACTIVATED
        completionHandler(.success(device))
    case .failure(let error):
        // HANDLE FAILURE
    }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
DeviceService().fetch { result ->
    val device = result.value
    if (result.throwable == null && device != null) {
        if(device.dataCollectionIsActive) {
            // DEVICE IS ACTIVATED
        } else {
            // DEVICE IS PRESENT -> BUT NOT ACTIVATED -> Re-ACTIVATE
            DeviceService().activate() { result: Result<Content?>? ->
                if (result?.value == null || result.throwable != null) {
                    // failure
                } else {
                    // Success
                    val content = result.value
                }
            })
        }
    } else {
        // DEVICE DOES NOT EXIST -> IS NOT ACTIVATED -> Re-ACTIVATE
        DeviceService().activate() { result: Result<Content?>? ->
            if (result?.value == null || result.throwable != null) {
                // failure
            } else {
                // Success
                val content = result.value
            }
        })
    }
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sdk.ims.tech/drivesync-portal/device.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
