1# @ohos.driver.deviceManager (Peripheral Management) 2 3The **deviceManager** module provides APIs for managing peripheral devices, including querying the peripheral device list and binding or unbinding a peripheral device. 4 5> **Note:** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11```ts 12import { deviceManager } from '@kit.DriverDevelopmentKit'; 13``` 14 15## deviceManager.queryDevices 16 17queryDevices(busType?: number): Array<Readonly<Device>> 18 19Queries the list of peripheral devices. If the device has no peripheral device connected, an empty list is returned. 20 21**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 22 23**System capability**: SystemCapability.Driver.ExternalDevice 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| ------- | ------ | ---- | ------------------------------------ | 29| busType | number | No | Bus type of the peripheral device to query. If this parameter is left blank, all types of peripheral devices are queried.| 30 31**Return value** 32 33| Type | Description | 34| ---------------------------------------------- | -------------- | 35| Array<Readonly<[Device](#device)>> | List of peripheral devices obtained.| 36 37**Error codes** 38 39| ID| Error Message | 40| -------- | ---------------------------------------- | 41| 201 | The permission check failed. | 42| 22900001 | ExternalDeviceManager service exception or busType parameter error. | 43 44**Example** 45 46```ts 47import { deviceManager } from '@kit.DriverDevelopmentKit'; 48 49try { 50 let devices : Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB); 51 for (let item of devices) { 52 let device : deviceManager.USBDevice = item as deviceManager.USBDevice; 53 console.info(`Device id is ${device.deviceId}`) 54 } 55} catch (error) { 56 console.error(`Failed to query device. Code is ${error.code}, message is ${error.message}`); 57} 58``` 59 60## deviceManager.bindDevice 61 62bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>, 63 callback: AsyncCallback<{deviceId: number; remote: rpc.IRemoteObject;}>): void 64 65Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses an asynchronous callback to return the result. 66 67You can use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 68 69**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 70 71**System capability**: SystemCapability.Driver.ExternalDevice 72 73**Parameters** 74 75| Name | Type | Mandatory| Description | 76| ------------ | ---------------------------------------------------------------------------------------------------- | ---- | -------------------------------------- | 77| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**. | 78| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 79| callback | AsyncCallback<{deviceId: number; remote: [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject);}> | Yes | Callback invoked to return the communication object of the peripheral device bound.| 80 81**Error codes** 82 83| ID| Error Message | 84| -------- | ---------------------------------------- | 85| 201 | The permission check failed. | 86| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 87| 22900001 | ExternalDeviceManager service exception. | 88 89**Example** 90 91```ts 92import { deviceManager } from '@kit.DriverDevelopmentKit'; 93import { BusinessError } from '@kit.BasicServicesKit'; 94import { rpc } from '@kit.IPCKit'; 95 96interface DataType { 97 deviceId : number; 98 remote : rpc.IRemoteObject; 99} 100 101try { 102 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 103 deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => { 104 console.error(`Device is disconnected`); 105 }, (error : BusinessError, data : DataType) => { 106 if (error) { 107 console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`); 108 return; 109 } 110 console.info(`bindDevice success`); 111 }); 112} catch (error) { 113 console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`); 114} 115``` 116 117## deviceManager.bindDeviceDriver<sup>11+</sup> 118bindDeviceDriver(deviceId: number, onDisconnect: AsyncCallback<number>, 119 callback: AsyncCallback<RemoteDeviceDriver>): void 120 121Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses an asynchronous callback to return the result. 122 123You can use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 124 125**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 126 127**System capability**: SystemCapability.Driver.ExternalDevice 128 129**Parameters** 130 131| Name | Type | Mandatory| Description | 132| ------------ | --------------------------- | ---- | ---------------------------- | 133| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**.| 134| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 135| callback | AsyncCallback<RemoteDeviceDriver>| Yes| Binding result, including the device ID and remote object.| 136 137**Error codes** 138 139| ID| Error Message | 140| -------- | ---------------------------------------- | 141| 201 | The permission check failed. | 142| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 143| 22900001 | ExternalDeviceManager service exception. | 144 145**Example** 146 147```ts 148import { deviceManager } from '@kit.DriverDevelopmentKit'; 149import { BusinessError } from '@kit.BasicServicesKit'; 150import { rpc } from '@kit.IPCKit'; 151 152try { 153 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 154 deviceManager.bindDeviceDriver(12345678, (error : BusinessError, data : number) => { 155 console.error(`Device is disconnected`); 156 }, (error : BusinessError, data : deviceManager.RemoteDeviceDriver) => { 157 if (error) { 158 console.error(`bindDeviceDriver async fail. Code is ${error.code}, message is ${error.message}`); 159 return; 160 } 161 console.info(`bindDeviceDriver success`); 162 }); 163} catch (error) { 164 console.error(`bindDeviceDriver fail. Code is ${error.code}, message is ${error.message}`); 165} 166``` 167 168## deviceManager.bindDevice 169 170bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>): Promise<{deviceId: number; 171 remote: rpc.IRemoteObject;}>; 172 173Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses an asynchronous callback to return the result. 174 175You need to use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 176 177**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 178 179**System capability**: SystemCapability.Driver.ExternalDevice 180 181**Parameters** 182 183| Name | Type | Mandatory| Description | 184| ------------ | --------------------------- | ---- | ---------------------------- | 185| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**.| 186| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 187 188**Return value** 189 190| Type | Description | 191| ---------------------------------------------------------------------------------------------- | -------------------------------------------- | 192| Promise<{deviceId: number; remote: [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject);}> | Promise used to return the device ID and **IRemoteObject** object.| 193 194**Error codes** 195 196| ID| Error Message | 197| -------- | ---------------------------------------- | 198| 201 | The permission check failed. | 199| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 200| 22900001 | ExternalDeviceManager service exception. | 201 202**Example** 203 204```ts 205import { deviceManager } from '@kit.DriverDevelopmentKit'; 206import { BusinessError } from '@kit.BasicServicesKit'; 207 208try { 209 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 210 deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => { 211 console.error(`Device is disconnected`); 212 }).then(data => { 213 console.info(`bindDevice success, Device_Id is ${data.deviceId}. 214 remote is ${data.remote != null ? data.remote.getDescriptor() : "null"}`); 215 }, (error: BusinessError) => { 216 console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`); 217 }); 218} catch (error) { 219 console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`); 220} 221``` 222## deviceManager.bindDeviceDriver<sup>11+</sup> 223 224bindDeviceDriver(deviceId: number, onDisconnect: AsyncCallback<number>): Promise<RemoteDeviceDriver>; 225 226Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses an asynchronous callback to return the result. 227 228You need to use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 229 230**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 231 232**System capability**: SystemCapability.Driver.ExternalDevice 233 234**Parameters** 235 236| Name | Type | Mandatory| Description | 237| ------------ | --------------------------- | ---- | ---------------------------- | 238| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**.| 239| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 240 241**Return value** 242 243| Type | Description | 244| --------------------------------- | -----------------------------------------| 245| Promise<RemoteDeviceDriver> | Promise used to return a **RemoteDeviceDriver** object.| 246 247**Error codes** 248 249| ID| Error Message | 250| -------- | ---------------------------------------- | 251| 201 | The permission check failed. | 252| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 253| 22900001 | ExternalDeviceManager service exception. | 254 255**Example** 256 257```ts 258import { deviceManager } from '@kit.DriverDevelopmentKit'; 259import { BusinessError } from '@kit.BasicServicesKit'; 260 261try { 262 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 263 deviceManager.bindDeviceDriver(12345678, (error : BusinessError, data : number) => { 264 console.error(`Device is disconnected`); 265 }).then((data: deviceManager.RemoteDeviceDriver) => { 266 console.info(`bindDeviceDriver success, Device_Id is ${data.deviceId}. 267 remote is ${data.remote != null ? data.remote.getDescriptor() : "null"}`); 268 }, (error: BusinessError) => { 269 console.error(`bindDeviceDriver async fail. Code is ${error.code}, message is ${error.message}`); 270 }); 271} catch (error) { 272 console.error(`bindDeviceDriver fail. Code is ${error.code}, message is ${error.message}`); 273} 274``` 275 276## deviceManager.unbindDevice 277 278unbindDevice(deviceId: number, callback: AsyncCallback<number>): void 279 280Unbinds a peripheral device. This API uses an asynchronous callback to return the result. 281 282**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 283 284**System capability**: SystemCapability.Driver.ExternalDevice 285 286**Parameters** 287 288| Name | Type | Mandatory| Description | 289| -------- | --------------------------- | ---- | ------------------------------ | 290| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**.| 291| callback | AsyncCallback<number> | Yes | Callback invoked to return the result. | 292 293**Error codes** 294 295| ID| Error Message | 296| -------- | ---------------------------------------- | 297| 201 | The permission check failed. | 298| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. | 299| 22900001 | ExternalDeviceManager service exception. | 300 301**Example** 302 303```ts 304import { deviceManager } from '@kit.DriverDevelopmentKit'; 305import { BusinessError } from '@kit.BasicServicesKit'; 306 307try { 308 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 309 deviceManager.unbindDevice(12345678, (error : BusinessError, data : number) => { 310 if (error) { 311 console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`); 312 return; 313 } 314 console.info(`unbindDevice success`); 315 }); 316} catch (error) { 317 console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`); 318} 319``` 320## deviceManager.unbindDevice 321 322unbindDevice(deviceId: number): Promise<number> 323 324Unbinds a peripheral device. This API uses an asynchronous callback to return the result. 325 326**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 327 328**System capability**: SystemCapability.Driver.ExternalDevice 329 330**Parameters** 331 332| Name | Type | Mandatory| Description | 333| -------- | ------ | ---- | ------------------------------ | 334| deviceId | number | Yes | Device ID, which can be obtained by **queryDevices**.| 335 336**Error codes** 337 338| ID| Error Message | 339| -------- | ---------------------------------------- | 340| 201 | The permission check failed. | 341| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 342| 22900001 | ExternalDeviceManager service exception. | 343 344**Return value** 345 346| Type | Description | 347| --------------------- | ------------------------- | 348| Promise<number> | Promise used to return the device ID.| 349 350**Example** 351 352```ts 353import { deviceManager } from '@kit.DriverDevelopmentKit'; 354import { BusinessError } from '@kit.BasicServicesKit'; 355 356try { 357 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 358 deviceManager.unbindDevice(12345678).then((data : number) => { 359 console.info(`unbindDevice success, Device_Id is ${data}.`); 360 }, (error : BusinessError) => { 361 console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`); 362 }); 363} catch (error) { 364 console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`); 365} 366``` 367 368## Device 369 370Represents the peripheral device information. 371 372**System capability**: SystemCapability.Driver.ExternalDevice 373 374| Name | Type | Mandatory| Description | 375| ----------- | ------------------- | ---- | ---------- | 376| busType | [BusType](#bustype) | Yes | Bus type.| 377| deviceId | number | Yes | Device ID. | 378| description | string | Yes | Description of the peripheral device.| 379 380## USBDevice 381 382USB device information, which is inherited from [Device](#device). 383 384**System capability**: SystemCapability.Driver.ExternalDevice 385 386| Name | Type | Mandatory| Description | 387| --------- | ------ | ---- | ------------------- | 388| vendorId | number | Yes | Vendor ID of the USB device. | 389| productId | number | Yes | Product ID of the USB device.| 390 391## BusType 392 393Enumerates the device bus types. 394 395**System capability**: SystemCapability.Driver.ExternalDevice 396 397| Name| Value | Description | 398| ---- | --- | ------------- | 399| USB | 1 | USB bus.| 400 401## RemoteDeviceDriver<sup>11+</sup> 402 403Represents information about a remote device driver. 404 405**System capability**: SystemCapability.Driver.ExternalDevice 406 407| Name | Type | Mandatory| Description | 408| --------- | ------ | ---- | ------------------- | 409| deviceId<sup>11+</sup> | number | Yes | Device ID. | 410| remote<sup>11+</sup> | [rpc.IRemoteObject](../apis-ipc-kit/js-apis-rpc.md#iremoteobject) | Yes | Remote driver object.| 411