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 GuidesInitializing and Configuring the DMK

Initializing and Configuring the DMK

In this guide, you’ll learn how to initialize and configure the Device Management Kit (DMK) for your application. By the end, you’ll have a working DMK instance ready to interface with Ledger devices.

What you’ll learn

  • How to initialize the DMK using the Kotlin DSL, from Kotlin or Swift
  • What configuration options are available, and how to declare transports
  • How to change the Manager API provider at runtime
  • How to maintain the DMK instance as a singleton

Step 1: Create the DMK instance

Use the deviceManagementKit builder DSL to obtain an instance of DeviceManagementKitApi. This should be done once in your application lifecycle.

Android (Kotlin):

import com.ledger.devicemanagement.DeviceManagementKitApi import com.ledger.devicemanagement.deviceManagementKit val dmk: DeviceManagementKitApi = deviceManagementKit { context = applicationContext enableLog = true }

iOS (Swift):

import LedgerDeviceManagementKit let dmk = deviceManagementKit(block: { builder in builder.enableLog = true })

Step 2: Configuration options

The builder DSL exposes the following parameters:

ParameterTypeDefaultDescription
contextAny?nullAndroid application context — required on Android
discoveryTimeoutDuration1.minutesHow long the SDK scans for nearby devices before stopping
connectionTimeoutDuration15.secondsHow long the SDK waits for a device connection before failing
enableLogBooleanfalseEnable SDK-wide logging

The builder also takes a transports { } block, where the app declares the transports it uses. Each transport comes from its own module (device-transport-kit-ble, device-transport-kit-usb) that the app adds to its dependencies. Without this block, BLE and USB are loaded on Android, and BLE only on iOS.

For development and tests, device-transport-kit-mockserver connects to the simulated devices of a Ledger device mock server instead of real devices: declare mockServer(url, token) in the block, with the token of the session of the server whose devices to reach.

Example with all options:

Android (Kotlin):

val dmk: DeviceManagementKitApi = deviceManagementKit { context = applicationContext discoveryTimeout = 2.minutes connectionTimeout = 30.seconds enableLog = true transports { usb() ble() } }

iOS (Swift):

ble() is the transport available on iOS. Declare it in the transports block of the builder:

import LedgerDeviceManagementKit let dmk = deviceManagementKit(block: { builder in builder.enableLog = true builder.transports { transports in transports.ble() } })

To develop or test without a real device, declare the mock server instead of ble():

let dmk = deviceManagementKit(block: { builder in builder.transports { transports in transports.mockServer( url: MockServerTransportIdentifierKt.DEFAULT_MOCK_SERVER_URL, token: "<token of a mock server session>" ) } })

A transports { } block that declares no transport fails when the SDK is built.

⚠️

Both discoveryTimeout and connectionTimeout must be strictly greater than zero, otherwise the builder will throw an exception.

Changing the provider at runtime

The provider is the Ledger distribution channel used by the Manager API requests. Besides the builder, it can be changed at any time on a running DMK, without resetting the SDK or disconnecting devices. The next Manager API requests use the new provider.

Kotlin:

dmk.setProvider(provider = 2) val current: Int = dmk.getProvider()

Swift:

dmk.setProvider(provider: 2) let current = dmk.getProvider()
⚠️

A provider lower than 1 is ignored by setProvider(), and the builder throws an IllegalStateException for it.

Step 3: Keep it as a singleton

🚨

Only one DMK can exist at a time. Attempting to create a second instance before the first one is destroyed will throw an exception. Always keep a single reference and share it across your application.

// Application.kt class MyApplication : Application() { lateinit var dmk: DeviceManagementKitApi override fun onCreate() { super.onCreate() dmk = deviceManagementKit { context = applicationContext enableLog = BuildConfig.DEBUG } } }

Step 4: Release resources

Call destroy() when your application closes to release all resources held by the DMK. It suspends until every device is disconnected and the transports are stopped. Launch it from a scope that outlives the caller, such as an application scope: lifecycleScope is already cancelled in onDestroy(), so a destroy() launched there never runs.

applicationScope.launch { ethereumSigner.destroy() // the signers built with the DMK first, as they keep using it dmk.destroy() }

From Swift, the destroy() of the DMK is a suspend function, called with await. The signers one is not:

Task { ethereumSigner.destroy() // the signers built with the DMK first try await dmk.destroy() }

Once it returns, any other call on the instance throws an IllegalStateException. A new DMK can then be created with deviceManagementKit { }, for example with other transports.

What you’ve learned

  • How to initialize the DMK with the deviceManagementKit { } Kotlin DSL
  • The available configuration parameters and their defaults, and how to declare transports on Android and iOS
  • How to change the provider with setProvider() and getProvider()
  • Why the DMK must be treated as a singleton
  • How to properly release resources on shutdown
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.