# Device Management >![](../../public_sys-resources/icon-note.gif) **NOTE:** >The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. ## Modules to Import ``` import deviceManager from '@ohos.distributedHardware.deviceManager'; ``` ## deviceManager.createDeviceManager createDeviceManager\(bundleName: string, callback: AsyncCallback\): void Creates a **DeviceManager** instance. - Parameters

Name

Type

Mandatory

Description

bundleName

string

Yes

Bundle name of the application.

callback

AsyncCallback<DeviceManager>

Yes

Callback invoked to return the DeviceManager instance created.

- Example ``` deviceManager.createDeviceManager("ohos.samples.jshelloworld", (err, data) => { if (err) { console.info("createDeviceManager err:" + JSON.stringify(err)); return; } console.info("createDeviceManager success"); this.dmInstance = data; }); ``` ## DeviceStateChangeAction Enumerates the device states.

Name

Default Value

Description

ONLINE

0

The device is online.

READY

1

The device is ready, and the device information has been synchronized.

OFFLINE

2

The device is offline.

CHANGE

3

The device information is changed.

## DeviceType Enumerates device types.

Name

Default Value

Description

SPEAKER

0x0A

Smart speaker.

PHONE

0x0E

Phone.

TABLET

0x11

Tablet.

WEARABLE

0x6D

Wearable.

TV

0x9C

Smart TV.

## DeviceInfo Defines device information.

Name

Type

Mandatory

Description

deviceId

number

Yes

Unique device identifier.

deviceName

string

Yes

Device name.

deviceType

number

Yes

Device type.

## DeviceManager Creates a **DeviceManager** instance to obtain information about trusted devices and local devices. Before calling any method in **DeviceManager**, you must use **createDeviceManager** to create a **DeviceManager** instance, for example, **dmInstance**. ### release release\(\): void Releases the **DeviceManager** instance that is no longer used. - Example ``` dmInstance.release(); ``` ### getTrustedDeviceListSync getTrustedDeviceListSync\(\): Array Obtains all trusted devices synchronously. - Return values

Name

Description

Array<DeviceInfo>

List of trusted devices obtained.

- Example ``` var deviceInfoList = dmInstance.getTrustedDeviceListSync(); ``` ### on\('deviceStateChange'\) on\(type: 'deviceStateChange', callback: Callback<\{ action: DeviceStateChangeAction, device: DeviceInfo \}\>\): void Subscribes to changes in the device state. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value is deviceStateChange, which indicates a device state change event.

callback

Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>

Yes

Callback invoked to return the device information and state.

- Example ``` dmInstance.on('deviceStateChange', (data) => { console.info("deviceStateChange on:" + JSON.stringify(data)); } ); ``` ### off\('deviceStateChange'\) off\(type: 'deviceStateChange', callback?: Call back<\{ action: DeviceStateChangeAction, device: DeviceInfo \}\>\): void Unsubscribes from changes in the device state. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value deviceStateChange indicates an event of device state change.

callback

Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>

Yes

Callback used to return the device state changes.

- Example ``` dmInstance.off('deviceStateChange', (data) => { console.info('deviceStateChange' + JSON.stringify(data)); } ); ``` ### on\('serviceDie'\) on\(type: 'serviceDie', callback: \(\) =\> void\): void Subscribes to dead events of the **DeviceManager** service. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value serviceDie indicates an event reported when the DeviceManager service is terminated unexpectedly.

callback

() => void

Yes

Callback invoked when a dead event of the DeviceManager service occurs.

- Example ``` dmInstance.on("serviceDie", () => { console.info("serviceDie on"); } ); ``` ### off\('serviceDie'\) off\(type: 'serviceDie', callback?: \(\) =\> void\): void Unsubscribes from dead events of the **DeviceManager** service. - Parameters

Name

Type

Mandatory

Description

type

string

Yes

Event type. The value serviceDie indicates an event reported when the DeviceManager service is terminated unexpectedly.

callback

() => void

No

Callback used to return the dead event of the DeviceManager service.

- Example ``` dmInstance.off("serviceDie", () => { console.info("serviceDie off"); } ); ```