Device Management
- 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.
- The APIs of this module are system APIs and cannot be called by third-party applications.
Modules to import:
import deviceManager from '@ohos.distributedHardware.deviceManager';
deviceManager.createDeviceManager
createDeviceManager(bundleName: string, callback: AsyncCallback<DeviceManager>): void
Creates a DeviceManager instance.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
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.
System capability: SystemCapability.DistributedHardware.DeviceManager
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 the device types.
System capability: SystemCapability.DistributedHardware.DeviceManager
Name | Default Value | Description |
---|---|---|
SPEAKER | 0x0A | Smart speaker. |
PHONE | 0x0E | Phone. |
TABLET | 0x11 | Tablet. |
WEARABLE | 0x6D | Wearable. |
TV | 0x9C | Smart TV. |
CAR | 0x83 | Car. |
UNKNOWN_TYPE | 0 | Unknown device type. |
DeviceInfo
Defines device information.
System capability: SystemCapability.DistributedHardware.DeviceManager
Name | Type | Mandatory | Description |
---|---|---|---|
deviceId | number | Yes | Unique device identifier. |
deviceName | string | Yes | Device name. |
deviceType | number | Yes | Device type. |
DeviceManager
Provides APIs to obtain information about trusted devices and local devices. Before calling any API 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.
System capability: SystemCapability.DistributedHardware.DeviceManager
- Example
dmInstance.release();
getTrustedDeviceListSync
getTrustedDeviceListSync(): Array<DeviceInfo>
Obtains all trusted devices synchronously.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
Return value
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.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
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.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
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 invoked to return the device information and state. -
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.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
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.
System capability: SystemCapability.DistributedHardware.DeviceManager
-
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"); } );