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 check that a transport is usable before discovering
- 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: Check the transports availability
Discovery and connection need the transport to be usable: Bluetooth turned on, permissions granted, link supported by the
phone. observeTransportsAvailability() emits the TransportAvailability of every transport declared when the DMK was
built, each time one changes. Check it before discovering.
| Availability | Meaning |
|---|---|
Available | The transport can discover and connect |
PermissionsRequired(permissions) | Ask the user for the permissions listed |
Disabled(settings) | Ask the user to turn on the settings listed (for example Bluetooth) |
Unsupported | The phone does not have the link: nothing the user can do |
Kotlin:
dmk.observeTransportsAvailability().collect { availabilities ->
when (val ble = availabilities[BLE_TRANSPORT]) {
is TransportAvailability.Available -> { /* ready to discover */ }
is TransportAvailability.PermissionsRequired -> { /* request ble.permissions */ }
is TransportAvailability.Disabled -> { /* ask to turn on ble.settings */ }
is TransportAvailability.Unsupported -> { /* hide the Bluetooth option */ }
null -> { /* BLE is not declared in transports { } */ }
}
}Swift:
import LedgerDeviceManagementKit
for await availabilities in dmk.observeTransportsAvailability() {
let ble = availabilities[BleTransportIdentifierKt.BLE_TRANSPORT]
if ble is TransportAvailabilityDisabled {
// Ask the user to turn Bluetooth on
} else if ble is TransportAvailabilityPermissionsRequired {
// Ask the user to allow Bluetooth for the app in the iOS settings
} else if ble is TransportAvailabilityUnsupported {
// Hide the Bluetooth option
}
}Step 2: Discover devices
startDiscoveringDevices() returns a Flow<DiscoveryResult> that emits updates as nearby devices are found.
Kotlin:
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.TransportUnavailable -> {
// One transport cannot discover right now, the others keep discovering.
// result.availability tells what to do: grant permissions, turn on settings, or nothing (unsupported).
}
is DiscoveryResult.Failure.Unknown -> { /* Handle other failures */ }
}
}Each DiscoveryDevice exposes:
| Property | Type | Description |
|---|---|---|
uid | String | Unique identifier for the device |
name | String | Human-readable device name |
ledgerDevice | LedgerDevice | Device model (e.g. NanoX, Stax, Flex) |
transport | TransportIdentifier | Transport that found the device (e.g. BLE_TRANSPORT) |
rssi | Int? | Signal strength, when the transport measures one (BLE) |
Swift:
for await result in dmk.startDiscoveringDevices() {
switch onEnum(of: result) {
case .devicesDiscovered(let state):
let devices: [DiscoveryDevice] = state.devices
// Present the list to the user and let them pick one
case .ended:
// Discovery stopped (timeout reached)
break
case .failure(let failure):
// One transport failed (for example TransportUnavailable), the others keep discovering
print("Discovery failure: \(failure)")
}
}When discovery is no longer needed, stop it to free resources:
dmk.stopDiscoveringDevices()dmk.stopDiscoveringDevices()Discovery stops automatically once discoveryTimeout is reached (default: 1
minute). You can configure this value when initializing the DMK.
Step 3: Connect to a device
Pass a DiscoveryDevice from the previous step to connectDevice():
Kotlin:
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.TransportUnavailable -> {
// The transport needs permissions or settings turned on first
}
else -> { /* Handle other failures */ }
}
}
}Swift:
do {
let result = try await dmk.connectDevice(device: device)
switch onEnum(of: result) {
case .connected(let connected):
let connectedDevice = connected.device
// Store connectedDevice.uid: you'll need it for all further interactions
case .disconnected(let disconnected):
// disconnected.failure tells why: PairingFailed, ConnectionTimeout, TransportUnavailable...
print("Connection failed: \(disconnected.failure)")
}
} catch {
print("Exception during connection: \(error)")
}Once connected, ConnectedDevice exposes:
| Property | Type | Description |
|---|---|---|
uid | String | Session identifier — keep this to interact with the device |
name | String | Device name |
ledgerDevice | LedgerDevice | Device model |
transport | TransportIdentifier | Transport the device is connected through |
Step 4: Disconnect from a device
When you are done using the device, disconnect to release the connection:
dmk.disconnectDevice(connectedDevice)try await dmk.disconnectDevice(connectedDevice: connectedDevice)What you’ve learned
- How to check a transport with
observeTransportsAvailability()before discovering - 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.uidis the key identifier for all further device interactions