Skip to Content
📢 Breaking change: Applications using LedgerJS for transport implementation should migrate to the Device Management Kit (DMK). Learn more.
DocumentationDevice interactionMobile Native DMKBeginner's GuidesConnecting to a Device

Connecting to a Device

In this guide, you’ll learn how to discover and connect to Ledger devices using the Device Management Kit (DMK). By the end, you’ll be able to scan for nearby devices, establish connections, and properly disconnect.

What you’ll learn

  • How to discover available Ledger devices over USB or BLE
  • How to connect to a specific device
  • How to disconnect from a device

Prerequisites

Before starting, make sure you have initialized a DMK instance. If you haven’t done so yet, refer to the Initializing and Configuring the DMK guide.

Step 1: Discover devices

startDiscoveringDevices() returns a Flow<DiscoveryResult> that emits updates as nearby devices are found.

dmk.startDiscoveringDevices().collect { result -> when (result) { is DiscoveryResult.DevicesDiscovered -> { val devices: List<DiscoveryDevice> = result.devices // Present the list to the user and let them pick one } is DiscoveryResult.Ended -> { // Discovery stopped (timeout reached) } is DiscoveryResult.Failure.BluetoothDisabled -> { // Ask the user to enable Bluetooth } is DiscoveryResult.Failure.BluetoothPermissionNotGranted -> { // Request Bluetooth permissions } else -> { /* Handle other failure cases */ } } }

Each DiscoveryDevice exposes:

PropertyTypeDescription
uidStringUnique identifier for the device
nameStringHuman-readable device name
ledgerDeviceLedgerDeviceDevice model (e.g. NanoX, Stax, Flex)
connectivityTypeConnectivityTypeUSB or BLE

When discovery is no longer needed, stop it to free resources:

dmk.stopDiscoveringDevices()
ℹ️

Discovery stops automatically once discoveryTimeout is reached (default: 1 minute). You can configure this value when initializing the DMK.

Step 2: Connect to a device

Pass a DiscoveryDevice from the previous step to connectDevice():

val result: ConnectionResult = dmk.connectDevice(device) when (result) { is ConnectionResult.Connected -> { val connectedDevice: ConnectedDevice = result.device // Store connectedDevice.uid — you'll need it for all further interactions } is ConnectionResult.Disconnected -> { when (result.failure) { is ConnectionResult.Failure.PairingFailed -> { // BLE pairing failed — ask the user to retry } is ConnectionResult.Failure.ConnectionTimeout -> { // Connection attempt exceeded connectionTimeout } is ConnectionResult.Failure.PermissionNotGranted -> { // Required permissions were not granted } else -> { /* Handle other failures */ } } } }

Once connected, ConnectedDevice exposes:

PropertyTypeDescription
uidStringSession identifier — keep this to interact with the device
nameStringDevice name
ledgerDeviceLedgerDeviceDevice model
connectivityTypeConnectivityTypeUSB or BLE

Step 3: Disconnect from a device

When you are done using the device, disconnect to release the connection:

dmk.disconnectDevice(connectedDevice)

What you’ve learned

  • How to start and stop device discovery with startDiscoveringDevices() / stopDiscoveringDevices()
  • How to connect to a discovered device with connectDevice() and handle connection failures
  • How to disconnect with disconnectDevice()
  • How the ConnectedDevice.uid is the key identifier for all further device interactions
Last updated on
Ledger
Copyright © Ledger SAS. All rights reserved. Ledger, Ledger Stax, Ledger Flex, Ledger Nano, Ledger Nano S, Ledger OS, Ledger Wallet, [LEDGER] (logo), [L] (logo) are trademarks owned by Ledger SAS.