1e41f4b71Sopenharmony_ci# @ohos.driver.deviceManager (外设管理)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci本模块主要提供管理外部设备的相关功能,包括查询设备列表、绑定设备和解除绑定设备。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **说明:**
6e41f4b71Sopenharmony_ci> 
7e41f4b71Sopenharmony_ci> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## 导入模块
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```ts
12e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci## deviceManager.queryDevices
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciqueryDevices(busType?: number): Array<Readonly<Device>>
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci获取接入主设备的外部设备列表。如果没有设备接入,那么将会返回一个空的列表。
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci**参数:**
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci| 参数名  | 类型   | 必填 | 说明                                 |
28e41f4b71Sopenharmony_ci| ------- | ------ | ---- | ------------------------------------ |
29e41f4b71Sopenharmony_ci| busType | number | 否   | 设备总线类型,不填则查找所有类型设备。 |
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci**返回值:**
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci| 类型                                           | 说明           |
34e41f4b71Sopenharmony_ci| ---------------------------------------------- | -------------- |
35e41f4b71Sopenharmony_ci| Array<Readonly<[Device](#device)>> | 设备信息列表。 |
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci**错误码:**
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
40e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
41e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
42e41f4b71Sopenharmony_ci| 401      | The parameter check failed.              |
43e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**示例:**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci```ts
48e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_citry {
51e41f4b71Sopenharmony_ci  let devices : Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB);
52e41f4b71Sopenharmony_ci  for (let item of devices) {
53e41f4b71Sopenharmony_ci    let device : deviceManager.USBDevice = item as deviceManager.USBDevice;
54e41f4b71Sopenharmony_ci    console.info(`Device id is ${device.deviceId}`)
55e41f4b71Sopenharmony_ci  }
56e41f4b71Sopenharmony_ci} catch (error) {
57e41f4b71Sopenharmony_ci  console.error(`Failed to query device. Code is ${error.code}, message is ${error.message}`);
58e41f4b71Sopenharmony_ci}
59e41f4b71Sopenharmony_ci```
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci## deviceManager.bindDevice
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_cibindDevice(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;,
64e41f4b71Sopenharmony_ci  callback: AsyncCallback&lt;{deviceId: number; remote: rpc.IRemoteObject;}&gt;): void;
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci根据queryDevices()返回的设备信息绑定设备。
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci需要调用[deviceManager.queryDevices()](#devicemanagerquerydevices)获取设备信息以及device。
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**参数:**
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci| 参数名       | 类型                                                                                                 | 必填 | 说明                                   |
77e41f4b71Sopenharmony_ci| ------------ | ---------------------------------------------------------------------------------------------------- | ---- | -------------------------------------- |
78e41f4b71Sopenharmony_ci| deviceId     | number                                                                                               | 是   | 设备ID,通过queryDevices获得。           |
79e41f4b71Sopenharmony_ci| onDisconnect | AsyncCallback&lt;number&gt;                                                                          | 是   | 绑定设备断开的回调。                     |
80e41f4b71Sopenharmony_ci| callback     | AsyncCallback&lt;{deviceId: number; remote: [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject);}&gt; | 是   | 绑定设备的回调,返回绑定设备的通信对象。 |
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci**错误码:**
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
85e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
86e41f4b71Sopenharmony_ci| 201      | The permission check failed.              |
87e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
88e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci**示例:**
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci```ts
93e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
94e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
95e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit';
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ciinterface DataType {
98e41f4b71Sopenharmony_ci  deviceId : number;
99e41f4b71Sopenharmony_ci  remote : rpc.IRemoteObject;
100e41f4b71Sopenharmony_ci}
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_citry {
103e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
104e41f4b71Sopenharmony_ci  deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => {
105e41f4b71Sopenharmony_ci    console.error(`Device is disconnected`);
106e41f4b71Sopenharmony_ci  }, (error : BusinessError, data : DataType) => {
107e41f4b71Sopenharmony_ci    if (error) {
108e41f4b71Sopenharmony_ci      console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
109e41f4b71Sopenharmony_ci      return;
110e41f4b71Sopenharmony_ci    }
111e41f4b71Sopenharmony_ci    console.info(`bindDevice success`);
112e41f4b71Sopenharmony_ci  });
113e41f4b71Sopenharmony_ci} catch (error) {
114e41f4b71Sopenharmony_ci  console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`);
115e41f4b71Sopenharmony_ci}
116e41f4b71Sopenharmony_ci```
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci## deviceManager.bindDeviceDriver<sup>11+</sup>
119e41f4b71Sopenharmony_cibindDeviceDriver(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;,
120e41f4b71Sopenharmony_ci  callback: AsyncCallback&lt;RemoteDeviceDriver&gt;): void;
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci根据queryDevices()返回的设备信息绑定设备。
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ci需要调用[deviceManager.queryDevices()](#devicemanagerquerydevices)获取设备信息以及device。
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci**参数:**
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci| 参数名       | 类型                        | 必填 | 说明                         |
133e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | ---------------------------- |
134e41f4b71Sopenharmony_ci| deviceId     | number                      | 是   | 设备ID,通过queryDevices获得。 |
135e41f4b71Sopenharmony_ci| onDisconnect | AsyncCallback&lt;number&gt; | 是   | 绑定设备断开的回调。           |
136e41f4b71Sopenharmony_ci| callback     | AsyncCallback&lt;RemoteDeviceDriver&gt;| 是 | 指示绑定结果,包括设备 ID 和远程对象。 |
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci**错误码:**
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
141e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
142e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
143e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
144e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci**示例:**
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci```ts
149e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
150e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
151e41f4b71Sopenharmony_ciimport { rpc } from '@kit.IPCKit';
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_citry {
154e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
155e41f4b71Sopenharmony_ci  deviceManager.bindDeviceDriver(12345678, (error : BusinessError, data : number) => {
156e41f4b71Sopenharmony_ci    console.error(`Device is disconnected`);
157e41f4b71Sopenharmony_ci  }, (error : BusinessError, data : deviceManager.RemoteDeviceDriver) => {
158e41f4b71Sopenharmony_ci    if (error) {
159e41f4b71Sopenharmony_ci      console.error(`bindDeviceDriver async fail. Code is ${error.code}, message is ${error.message}`);
160e41f4b71Sopenharmony_ci      return;
161e41f4b71Sopenharmony_ci    }
162e41f4b71Sopenharmony_ci    console.info(`bindDeviceDriver success`);
163e41f4b71Sopenharmony_ci  });
164e41f4b71Sopenharmony_ci} catch (error) {
165e41f4b71Sopenharmony_ci  console.error(`bindDeviceDriver fail. Code is ${error.code}, message is ${error.message}`);
166e41f4b71Sopenharmony_ci}
167e41f4b71Sopenharmony_ci```
168e41f4b71Sopenharmony_ci
169e41f4b71Sopenharmony_ci## deviceManager.bindDevice
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_cibindDevice(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;): Promise&lt;{deviceId: number;
172e41f4b71Sopenharmony_ci  remote: rpc.IRemoteObject;}&gt;;
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci根据queryDevices()返回的设备信息绑定设备。
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci需要调用[deviceManager.queryDevices](#devicemanagerquerydevices)获取设备信息以及device。
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
179e41f4b71Sopenharmony_ci
180e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci**参数:**
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci| 参数名       | 类型                        | 必填 | 说明                         |
185e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | ---------------------------- |
186e41f4b71Sopenharmony_ci| deviceId     | number                      | 是   | 设备ID,通过queryDevices获得。 |
187e41f4b71Sopenharmony_ci| onDisconnect | AsyncCallback&lt;number&gt; | 是   | 绑定设备断开的回调。           |
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci**返回值:** 
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci| 类型                                                                                           | 说明                                         |
192e41f4b71Sopenharmony_ci| ---------------------------------------------------------------------------------------------- | -------------------------------------------- |
193e41f4b71Sopenharmony_ci| Promise&lt;{deviceId: number; remote: [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject);}&gt; | Promise对象,返回设备ID和IRemoteObject对象。 |
194e41f4b71Sopenharmony_ci
195e41f4b71Sopenharmony_ci**错误码:**
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
198e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
199e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
200e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
201e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci**示例:**
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci```ts
206e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
207e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
208e41f4b71Sopenharmony_ci
209e41f4b71Sopenharmony_citry {
210e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
211e41f4b71Sopenharmony_ci  deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => {
212e41f4b71Sopenharmony_ci    console.error(`Device is disconnected`);
213e41f4b71Sopenharmony_ci  }).then(data => {
214e41f4b71Sopenharmony_ci    console.info(`bindDevice success, Device_Id is ${data.deviceId}.
215e41f4b71Sopenharmony_ci    remote is ${data.remote != null ? data.remote.getDescriptor() : "null"}`);
216e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
217e41f4b71Sopenharmony_ci    console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
218e41f4b71Sopenharmony_ci  });
219e41f4b71Sopenharmony_ci} catch (error) {
220e41f4b71Sopenharmony_ci  console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`);
221e41f4b71Sopenharmony_ci}
222e41f4b71Sopenharmony_ci```
223e41f4b71Sopenharmony_ci## deviceManager.bindDeviceDriver<sup>11+</sup>
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_cibindDeviceDriver(deviceId: number, onDisconnect: AsyncCallback&lt;number&gt;): Promise&lt;RemoteDeviceDriver&gt;;
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ci根据queryDevices()返回的设备信息绑定设备。
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ci需要调用[deviceManager.queryDevices](#devicemanagerquerydevices)获取设备信息以及device。
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci**参数:**
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci| 参数名       | 类型                        | 必填 | 说明                         |
238e41f4b71Sopenharmony_ci| ------------ | --------------------------- | ---- | ---------------------------- |
239e41f4b71Sopenharmony_ci| deviceId     | number                      | 是   | 设备ID,通过queryDevices获得。 |
240e41f4b71Sopenharmony_ci| onDisconnect | AsyncCallback&lt;number&gt; | 是   | 绑定设备断开的回调。           |
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci**返回值:** 
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ci| 类型                              | 说明                                      |
245e41f4b71Sopenharmony_ci| --------------------------------- | -----------------------------------------|
246e41f4b71Sopenharmony_ci| Promise&lt;RemoteDeviceDriver&gt; | Promise对象,返回RemoteDeviceDriver对象。 |
247e41f4b71Sopenharmony_ci
248e41f4b71Sopenharmony_ci**错误码:**
249e41f4b71Sopenharmony_ci
250e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
251e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
252e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
253e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
254e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
255e41f4b71Sopenharmony_ci
256e41f4b71Sopenharmony_ci**示例:**
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ci```ts
259e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
260e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_citry {
263e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
264e41f4b71Sopenharmony_ci  deviceManager.bindDeviceDriver(12345678, (error : BusinessError, data : number) => {
265e41f4b71Sopenharmony_ci    console.error(`Device is disconnected`);
266e41f4b71Sopenharmony_ci  }).then((data: deviceManager.RemoteDeviceDriver) => {
267e41f4b71Sopenharmony_ci    console.info(`bindDeviceDriver success, Device_Id is ${data.deviceId}.
268e41f4b71Sopenharmony_ci    remote is ${data.remote != null ? data.remote.getDescriptor() : "null"}`);
269e41f4b71Sopenharmony_ci  }, (error: BusinessError) => {
270e41f4b71Sopenharmony_ci    console.error(`bindDeviceDriver async fail. Code is ${error.code}, message is ${error.message}`);
271e41f4b71Sopenharmony_ci  });
272e41f4b71Sopenharmony_ci} catch (error) {
273e41f4b71Sopenharmony_ci  console.error(`bindDeviceDriver fail. Code is ${error.code}, message is ${error.message}`);
274e41f4b71Sopenharmony_ci}
275e41f4b71Sopenharmony_ci```
276e41f4b71Sopenharmony_ci
277e41f4b71Sopenharmony_ci## deviceManager.unbindDevice
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ciunbindDevice(deviceId: number, callback: AsyncCallback&lt;number&gt;): void
280e41f4b71Sopenharmony_ci
281e41f4b71Sopenharmony_ci解除设备绑定。
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci**系统能力:**  SystemCapability.Driver.ExternalDevice
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci**参数:**
288e41f4b71Sopenharmony_ci
289e41f4b71Sopenharmony_ci| 参数名   | 类型                        | 必填 | 说明                           |
290e41f4b71Sopenharmony_ci| -------- | --------------------------- | ---- | ------------------------------ |
291e41f4b71Sopenharmony_ci| deviceId | number                      | 是   | 设备ID,通过queryDevices获得。 |
292e41f4b71Sopenharmony_ci| callback | AsyncCallback&lt;number&gt; | 是   | 解绑完成的回调。               |
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci**错误码:**
295e41f4b71Sopenharmony_ci
296e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
297e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
298e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
299e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. |
300e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci**示例:**
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci```ts
305e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
306e41f4b71Sopenharmony_ci
307e41f4b71Sopenharmony_citry {
308e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
309e41f4b71Sopenharmony_ci  deviceManager.unbindDevice(12345678, (error : BusinessError, data : number) => {
310e41f4b71Sopenharmony_ci    if (error) {
311e41f4b71Sopenharmony_ci      console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`);
312e41f4b71Sopenharmony_ci      return;
313e41f4b71Sopenharmony_ci    }
314e41f4b71Sopenharmony_ci    console.info(`unbindDevice success`);
315e41f4b71Sopenharmony_ci  });
316e41f4b71Sopenharmony_ci} catch (error) {
317e41f4b71Sopenharmony_ci  console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`);
318e41f4b71Sopenharmony_ci}
319e41f4b71Sopenharmony_ci```
320e41f4b71Sopenharmony_ci## deviceManager.unbindDevice
321e41f4b71Sopenharmony_ci
322e41f4b71Sopenharmony_ciunbindDevice(deviceId: number): Promise&lt;number&gt;
323e41f4b71Sopenharmony_ci
324e41f4b71Sopenharmony_ci解除设备绑定。
325e41f4b71Sopenharmony_ci
326e41f4b71Sopenharmony_ci**需要权限**:ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER
327e41f4b71Sopenharmony_ci
328e41f4b71Sopenharmony_ci**系统能力:**  SystemCapability.Driver.ExternalDevice
329e41f4b71Sopenharmony_ci
330e41f4b71Sopenharmony_ci**参数:**
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci| 参数名   | 类型   | 必填 | 说明                           |
333e41f4b71Sopenharmony_ci| -------- | ------ | ---- | ------------------------------ |
334e41f4b71Sopenharmony_ci| deviceId | number | 是   | 设备ID,通过queryDevices获得。 |
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci**错误码:**
337e41f4b71Sopenharmony_ci
338e41f4b71Sopenharmony_ci| 错误码ID | 错误信息                                 |
339e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- |
340e41f4b71Sopenharmony_ci| 201      | The permission check failed.             |
341e41f4b71Sopenharmony_ci| 401      | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. |
342e41f4b71Sopenharmony_ci| 22900001 | ExternalDeviceManager service exception. |
343e41f4b71Sopenharmony_ci
344e41f4b71Sopenharmony_ci**返回值:** 
345e41f4b71Sopenharmony_ci
346e41f4b71Sopenharmony_ci| 类型                  | 说明                      |
347e41f4b71Sopenharmony_ci| --------------------- | ------------------------- |
348e41f4b71Sopenharmony_ci| Promise&lt;number&gt; | Promise对象,返回设备ID。 |
349e41f4b71Sopenharmony_ci
350e41f4b71Sopenharmony_ci**示例:**
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci```ts
353e41f4b71Sopenharmony_ciimport { deviceManager } from '@kit.DriverDevelopmentKit';
354e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_citry {
357e41f4b71Sopenharmony_ci  // 12345678为示例deviceId,应用开发时可通过queryDevices查询到相应设备的deviceId作为入参
358e41f4b71Sopenharmony_ci  deviceManager.unbindDevice(12345678).then((data : number) => {
359e41f4b71Sopenharmony_ci    console.info(`unbindDevice success, Device_Id is ${data}.`);
360e41f4b71Sopenharmony_ci  }, (error : BusinessError) => {
361e41f4b71Sopenharmony_ci    console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`);
362e41f4b71Sopenharmony_ci  });
363e41f4b71Sopenharmony_ci} catch (error) {
364e41f4b71Sopenharmony_ci  console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`);
365e41f4b71Sopenharmony_ci}
366e41f4b71Sopenharmony_ci```
367e41f4b71Sopenharmony_ci
368e41f4b71Sopenharmony_ci## Device
369e41f4b71Sopenharmony_ci
370e41f4b71Sopenharmony_ci外设信息。
371e41f4b71Sopenharmony_ci
372e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
373e41f4b71Sopenharmony_ci
374e41f4b71Sopenharmony_ci| 名称        | 类型                | 必填 | 说明       |
375e41f4b71Sopenharmony_ci| ----------- | ------------------- | ---- | ---------- |
376e41f4b71Sopenharmony_ci| busType     | [BusType](#bustype) | 是   | 总线类型。 |
377e41f4b71Sopenharmony_ci| deviceId    | number              | 是   | 设备ID。   |
378e41f4b71Sopenharmony_ci| description | string              | 是   | 设备描述。 |
379e41f4b71Sopenharmony_ci
380e41f4b71Sopenharmony_ci## USBDevice
381e41f4b71Sopenharmony_ci
382e41f4b71Sopenharmony_ciUSB设备信息,继承自[Device](#device)。
383e41f4b71Sopenharmony_ci
384e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
385e41f4b71Sopenharmony_ci
386e41f4b71Sopenharmony_ci| 名称      | 类型   | 必填 | 说明                |
387e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------- |
388e41f4b71Sopenharmony_ci| vendorId  | number | 是   | USB设备Vendor ID。  |
389e41f4b71Sopenharmony_ci| productId | number | 是   | USB设备Product ID。 |
390e41f4b71Sopenharmony_ci
391e41f4b71Sopenharmony_ci## BusType
392e41f4b71Sopenharmony_ci
393e41f4b71Sopenharmony_ci设备总线类型。
394e41f4b71Sopenharmony_ci
395e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
396e41f4b71Sopenharmony_ci
397e41f4b71Sopenharmony_ci| 名称 | 值  | 说明          |
398e41f4b71Sopenharmony_ci| ---- | --- | ------------- |
399e41f4b71Sopenharmony_ci| USB  | 1   | USB总线类型。 |
400e41f4b71Sopenharmony_ci
401e41f4b71Sopenharmony_ci## RemoteDeviceDriver<sup>11+</sup>
402e41f4b71Sopenharmony_ci
403e41f4b71Sopenharmony_ci远程设备驱动。
404e41f4b71Sopenharmony_ci
405e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Driver.ExternalDevice
406e41f4b71Sopenharmony_ci
407e41f4b71Sopenharmony_ci| 名称      | 类型   | 必填 | 说明                |
408e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------- |
409e41f4b71Sopenharmony_ci| deviceId<sup>11+</sup>  | number | 是   | 设备ID。  |
410e41f4b71Sopenharmony_ci| remote<sup>11+</sup> | [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject) | 是   | 远程驱动程序对象。 |