js-apis-driver-deviceManager.md 9.6 KB
Newer Older
D
dongbinbin 已提交
1 2 3 4 5 6 7 8 9 10
# @ohos.driver.deviceManager (外设管理)

本模块主要提供管理外部设备的相关功能,包括查询设备列表、绑定设备和解除绑定设备。

>  **说明:**
> 
> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

## 导入模块

11
```ts
D
dongbinbin 已提交
12
import deviceManager from "@ohos.driver.deviceManager";
13
import { BusinessError } from '@ohos.base';
D
dongbinbin 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
```

## deviceManager.queryDevices

queryDevices(busType?: number): Array<Readonly<Device>>

获取接入主设备的外部设备列表。如果没有设备接入,那么将会返回一个空的列表。

**系统能力:**  SystemCapability.Driver.ExternalDevice

**参数:**

| 参数名  | 类型   | 必填 | 说明                                 |
| ------- | ------ | ---- | ------------------------------------ |
| busType | number | 否   | 设备总线类型,不填则查找所有类型设备 |

**返回值:**

| 类型                                           | 说明           |
| ---------------------------------------------- | -------------- |
| Array<Readonly<[Device](#device)>> | 设备信息列表。 |

**错误码:**

D
dongbinbin 已提交
38 39 40 41
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 401      | The parameter check failed.              |
| 22900001 | ExternalDeviceManager service exception. |
D
dongbinbin 已提交
42 43 44 45 46

**示例:**

```js
try {
47 48
  let devices : Array<Device> = deviceManager.queryDevices(deviceManager.BusType.USB);
  for (let item : Device of devices : Array<Device>) {
D
dongbinbin 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    console.info('Device id is ${item.deviceId}')
  }
} catch (error) {
  console.error('Failed to query device. Code is ${error.code}, message is ${error.message}');
}
```

## deviceManager.bindDevice

bindDevice(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;,
  callback: AsyncCallback&lt;{deviceId: number, remote: rpc.IRemoteObject}&gt;): void;

根据queryDevices()返回的设备信息绑定设备。

需要调用[deviceManager.queryDevices](#devicemanagerquerydevices)获取设备信息以及device。

**系统能力:**  SystemCapability.Driver.ExternalDevice

**参数:**

| 参数名       | 类型                                                                                                 | 必填 | 说明                                   |
| ------------ | ---------------------------------------------------------------------------------------------------- | ---- | -------------------------------------- |
D
dongbinbin 已提交
71
| deviceId     | number                                                                                               | 是   | 设备ID,通过queryDevices获得           |
D
dongbinbin 已提交
72 73 74 75 76
| onDisconnect | AsyncCallback&lt;number&gt;                                                                          | 是   | 绑定设备断开的回调                     |
| callback     | AsyncCallback&lt;{deviceId: number, remote: [rpc.IRemoteObject](./js-apis-rpc.md#iremoteobject)}&gt; | 是   | 绑定设备的回调,返回绑定设备的通信对象 |

**错误码:**

D
dongbinbin 已提交
77 78 79 80
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 401      | The parameter check failed.              |
| 22900001 | ExternalDeviceManager service exception. |
D
dongbinbin 已提交
81 82 83 84 85

**示例:**

```js
try {
D
dongbinbin 已提交
86
  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
87
  deviceManager.bindDevice(12345678, (error : BusinessError, data : MessageSequence) => {
D
dongbinbin 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    console.error('Device is disconnected');
  }, (error, data) => {
    if (error) {
      console.error('bindDevice async fail. Code is ${error.code}, message is ${error.message}');
      return;
    }
    console.info('bindDevice success');
  });
} catch (error) {
  console.error('bindDevice fail. Code is ${error.code}, message is ${error.message}');
}
```

## deviceManager.bindDevice

bindDevice(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;): Promise&lt;{deviceId: number,
  remote: rpc.IRemoteObject}&gt;;

根据queryDevices()返回的设备信息绑定设备。

需要调用[deviceManager.queryDevices](#devicemanagerquerydevices)获取设备信息以及device。

**系统能力:**  SystemCapability.Driver.ExternalDevice

**参数:**

| 参数名       | 类型                        | 必填 | 说明                         |
| ------------ | --------------------------- | ---- | ---------------------------- |
D
dongbinbin 已提交
116
| deviceId     | number                      | 是   | 设备ID,通过queryDevices获得 |
D
dongbinbin 已提交
117 118 119 120 121 122 123 124 125 126
| onDisconnect | AsyncCallback&lt;number&gt; | 是   | 绑定设备断开的回调           |

**返回值:** 

| 类型                                                                                           | 说明                                         |
| ---------------------------------------------------------------------------------------------- | -------------------------------------------- |
| Promise&lt;{deviceId: number, remote: [rpc.IRemoteObject](./js-apis-rpc.md#iremoteobject)}&gt; | Promise对象,返回设备ID和IRemoteObject对象。 |

**错误码:**

D
dongbinbin 已提交
127 128 129 130
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 401      | The parameter check failed.              |
| 22900001 | ExternalDeviceManager service exception. |
D
dongbinbin 已提交
131 132 133 134 135

**示例:**

```js
try {
D
dongbinbin 已提交
136
  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
D
dongbinbin 已提交
137
  deviceManager.bindDevice(12345678, (error, data) => {
D
dongbinbin 已提交
138 139 140
    console.error('Device is disconnected');
  }).then(data => {
    console.info('bindDevice success');
141
  }, (error : BusinessError) => {
D
dongbinbin 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    console.error('bindDevice async fail. Code is ${error.code}, message is ${error.message}');
  });
} catch (error) {
  console.error('bindDevice fail. Code is ${error.code}, message is ${error.message}');
}
```

## deviceManager.unbindDevice

unbindDevice(deviceId: number, callback: AsyncCallback&lt;number&gt;): void

解除设备绑定。

**系统能力:**  SystemCapability.Driver.ExternalDevice

**参数:**

| 参数名   | 类型                        | 必填 | 说明                           |
| -------- | --------------------------- | ---- | ------------------------------ |
D
dongbinbin 已提交
161
| deviceId | number                      | 是   | 设备ID,通过queryDevices获得。 |
D
dongbinbin 已提交
162 163 164 165
| callback | AsyncCallback&lt;number&gt; | 是   | 解绑完成的回调。               |

**错误码:**

D
dongbinbin 已提交
166 167 168 169
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 401      | The parameter check failed.              |
| 22900001 | ExternalDeviceManager service exception. |
D
dongbinbin 已提交
170 171 172 173 174

**示例:**

```js
try {
D
dongbinbin 已提交
175
  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
D
dongbinbin 已提交
176
  deviceManager.unbindDevice(12345678, (error, data) => {
D
dongbinbin 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
  if (error) {
    console.error('unbindDevice async fail. Code is ${error.code}, message is ${error.message}');
    return;
  }
  console.info('unbindDevice success');
  });
} catch (error) {
  console.error('unbindDevice fail. Code is ${error.code}, message is ${error.message}');
}
```
## deviceManager.unbindDevice

unbindDevice(deviceId: number): Promise&lt;number&gt;

解除设备绑定。

**系统能力:**  SystemCapability.Driver.ExternalDevice

**参数:**

| 参数名   | 类型   | 必填 | 说明                           |
| -------- | ------ | ---- | ------------------------------ |
D
dongbinbin 已提交
199
| deviceId | number | 是   | 设备ID,通过queryDevices获得。 |
D
dongbinbin 已提交
200 201 202

**错误码:**

D
dongbinbin 已提交
203 204 205 206
| 错误码ID | 错误信息                                 |
| -------- | ---------------------------------------- |
| 401      | The parameter check failed.              |
| 22900001 | ExternalDeviceManager service exception. |
D
dongbinbin 已提交
207 208 209 210 211 212 213 214 215 216 217

**返回值:** 

| 类型                  | 说明                      |
| --------------------- | ------------------------- |
| Promise&lt;number&gt; | Promise对象,返回设备ID。 |

**示例:**

```js
try {
D
dongbinbin 已提交
218
  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
D
dongbinbin 已提交
219
  deviceManager.unbindDevice(12345678).then(data => {
D
dongbinbin 已提交
220
    console.info('unbindDevice success');
221
  }, (error : BusinessError) => {
D
dongbinbin 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    console.error('unbindDevice async fail. Code is ${error.code}, message is ${error.message}');
  });
} catch (error) {
  console.error('unbindDevice fail. Code is ${error.code}, message is ${error.message}');
}
```

## Device

外设信息。

**系统能力:** SystemCapability.Driver.ExternalDevice

| 名称        | 类型                | 必填 | 说明       |
| ----------- | ------------------- | ---- | ---------- |
| busType     | [BusType](#bustype) | 是   | 总线类型。 |
| deviceId    | number              | 是   | 设备ID。   |
| description | string              | 是   | 设备描述。 |

## USBDevice

USB设备信息。

**系统能力:** SystemCapability.Driver.ExternalDevice

| 名称      | 类型   | 必填 | 说明                |
| --------- | ------ | ---- | ------------------- |
| vendorId  | number | 是   | USB设备Vendor ID。  |
| productId | number | 是   | USB设备Product ID。 |

## BusType

设备总线类型。

**系统能力:** SystemCapability.Driver.ExternalDevice

| 名称 | 值  | 说明          |
| ---- | --- | ------------- |
| USB  | 1   | USB总线类型。 |