---
title: "Device Management Kit"
category: how-to
---

# Device Management Kit

The device management kit is the entry point for all the other libraries related to it.
As we wanted to make the project modular.

## Main Features

- Discovering and connecting to Ledger devices via USB, through [WebHID](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API).
- Discovering and connecting to Ledger devices via BLE, through [WebBLE](https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API).
- Observing the state of a connected device.
- Sending custom APDU commands to Ledger devices.
- Sending a set of pre-defined commands to Ledger devices.
  - Get OS version
  - Get app and version
  - Open app
  - Close app
  - Get battery status
- Execute a flow of commands with **DeviceAction**.

> \[!NOTE]
> At the moment we do not provide the possibility to distinguish two devices of the same model, via WebHID and to avoid connection to the same device twice.

## Connection Management

### Reconnect Device Session

The `reconnect` method allows you to refresh a device connection by disconnecting and reconnecting to the device. This is useful when you need to refresh the connection state or reconfigure session refresher options.

**Parameters**:

- `device`: ConnectedDevice - The connected device to reconnect (obtained from `getConnectedDevice`).
- `sessionRefresherOptions`: (optional) Configuration for session refreshing.
  - `isRefresherDisabled`: boolean - Whether the refresher is disabled.
  - `pollingInterval`: number - The refresh interval in milliseconds.

```typescript
// Get the connected device
const connectedDevice = dmk.getConnectedDevice({ sessionId });

// Reconnect the device session
// newSessionId = sessionId
const newSessionId = await dmk.reconnect({
  device: connectedDevice,
  sessionRefresherOptions: {
    isRefresherDisabled: false,
    pollingInterval: 3000,
  },
});
```

## Communicate with a Ledger device

The DMK is offering several ways to communicate with a Ledger device.

### Send APDU

> **Warning:** This method is not recommended for most of the use cases. We recommend using
> the _Command_ or _DeviceAction_ instead.

You can send APDU commands to the device using the `sendApdu` method of the Device Management Kit instance (here `dmk`) instance.

**Parameters**:

- `sessionId`: string - The session ID, identifier of the connection with a device.
- `apdu`: UInt8Array - bytes array of data to be send to the device.

```typescript
await dmk.sendApdu({ sessionId, apdu });
```

### Commands

Commands are pre-defined actions that you can send to the device.
You can use the `sendCommand` method of the `dmk` instance to send a command to the device.

**Parameters**:

- `sessionId`: string - The session ID, which an identifier of the connection with a device.
- `command`: Command - The command to be sent to the device.
- `abortTimeout`: number - (optional) The timeout to abort the command (in milliseconds).

```typescript
import { OpenAppCommand } from "@ledgerhq/device-management-kit";

const command = new OpenAppCommand("Bitcoin"); // Open the Bitcoin app
await dmk.sendCommand({ sessionId, command });

// You can also send the command with a timeout to abort the command if it takes too long (in milliseconds)
await dmk.sendCommand({ sessionId, command, abortTimeout: 10000 });

// If the command times out, sendCommand will throw a SendApduTimeoutError
```

### Device Actions

Device actions are a set of commands that are executed in a sequence.
You can use the `executeDeviceAction` method of the `dmk` instance to execute a device action.

It is returning an observable that will emit different states of the action execution.
A device action is cancellable, you can cancel it by calling the `cancel` function returned by the `executeDeviceAction` method.

**Parameters**:

- `sessionId`: string - The session ID, which an identifier of the connection with a device.
- `deviceAction`: DeviceAction - The DeviceAction to be sent to the device.

```typescript
const openAppDeviceAction = new OpenAppDeviceAction({ appName: "Bitcoin" });

const { observable, cancel } = await dmk.executeDeviceAction({
  sessionId,
  openAppDeviceAction,
});
```

## State Management

For each connected device, we are managing and providing a device state.

The different states are:

- `connected`: The device is connected.
- `locked`: The device is locked. User needs to unlock it to perform operations.
- `busy`: The device is busy, so not reachable.
- `disconnected`: The device is disconnected.
