js-apis-device-manager.md 6.4 KB
Newer Older
W
wusongqing 已提交
1
# Device Management
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4 5
> ![icon-note.gif](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.
> - The APIs of this module are system APIs and cannot be called by third-party applications.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to import:
Z
zengyawen 已提交
9 10 11 12 13

```
import deviceManager from '@ohos.distributedHardware.deviceManager';
```

W
wusongqing 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

## 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](#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
Z
zengyawen 已提交
43 44 45

Enumerates the device states.

W
wusongqing 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
**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
Z
zengyawen 已提交
74 75 76

Defines device information.

W
wusongqing 已提交
77
**System capability**: SystemCapability.DistributedHardware.DeviceManager
Z
zengyawen 已提交
78

W
wusongqing 已提交
79 80 81 82 83 84 85 86
| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| deviceId | number | Yes| Unique device identifier.|
| deviceName | string | Yes| Device name.|
| deviceType | number | Yes| Device type.|


## DeviceManager
Z
zengyawen 已提交
87

W
wusongqing 已提交
88
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**.
Z
zengyawen 已提交
89 90


W
wusongqing 已提交
91
### release
Z
zengyawen 已提交
92

W
wusongqing 已提交
93
release(): void
Z
zengyawen 已提交
94

W
wusongqing 已提交
95
Releases the **DeviceManager** instance that is no longer used.
Z
zengyawen 已提交
96

W
wusongqing 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109
**System capability**: SystemCapability.DistributedHardware.DeviceManager

- Example
  ```
  dmInstance.release();
  ```


### getTrustedDeviceListSync

getTrustedDeviceListSync(): Array<DeviceInfo>

Obtains all trusted devices synchronously.
Z
zengyawen 已提交
110

W
wusongqing 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
**System capability**: SystemCapability.DistributedHardware.DeviceManager

- Return value
  | Name| Description|
  | -------- | -------- |
  | Array<[DeviceInfo](#deviceinfo)> | List of trusted devices obtained.|

- Example
  ```
  var deviceInfoList = dmInstance.getTrustedDeviceListSync();
  ```


### on('deviceStateChange')

on(type: 'deviceStateChange',  callback: Callback<{ action: DeviceStateChangeAction, device: DeviceInfo }>): void
Z
zengyawen 已提交
127 128 129

Subscribes to changes in the device state.

W
wusongqing 已提交
130
**System capability**: SystemCapability.DistributedHardware.DeviceManager
Z
zengyawen 已提交
131

W
wusongqing 已提交
132 133 134 135 136
- **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](#devicestatechangeaction), device: [DeviceInfo](#deviceinfo) }> | Yes| Callback invoked to return the device information and state.|
Z
zengyawen 已提交
137

W
wusongqing 已提交
138 139 140 141
- Example
  ```
  dmInstance.on('deviceStateChange', (data) => {      
        console.info("deviceStateChange on:" + JSON.stringify(data));
Z
zengyawen 已提交
142
      }
W
wusongqing 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  );
  ```


### 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](#devicestatechangeaction), device: [DeviceInfo](#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
Z
zengyawen 已提交
200

W
wusongqing 已提交
201 202 203 204 205
- **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.|
Z
zengyawen 已提交
206

W
wusongqing 已提交
207 208 209 210 211 212 213
- Example
  ```
  dmInstance.off("serviceDie", () => {      
        console.info("serviceDie off");
    }
  );
  ```