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 "@ohos.driver.deviceManager"; 13``` 14 15## deviceManager.queryDevices 16 17queryDevices(busType?: number): Array<Readonly<deviceManager.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. 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<[deviceManager.Device](#device)>> | List of peripheral devices obtained.| 36 37**Error codes** 38 39| ID| Error Message | 40| -------- | ---------------------------------------- | 41| 401 | The parameter check failed. | 42| 22900001 | ExternalDeviceManager service exception. | 43 44**Example** 45 46```ts 47import deviceManager from "@ohos.driver.deviceManager"; 48 49 50try { 51 let devices : Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB); 52 for (let item of devices) { 53 let device : deviceManager.USBDevice = item as deviceManager.USBDevice; 54 console.info(`Device id is ${device.deviceId}`) 55 } 56} catch (error) { 57 console.error(`Failed to query device. Code is ${error.code}, message is ${error.message}`); 58} 59``` 60 61## deviceManager.bindDevice 62 63bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>, 64 callback: AsyncCallback<{deviceId: number, remote: rpc.IRemoteObject}>): void; 65 66Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses an asynchronous callback to return the result. 67 68You need to use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 69 70**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 71 72**System capability**: SystemCapability.Driver.ExternalDevice 73 74**Parameters** 75 76| Name | Type | Mandatory| Description | 77| ------------ | ---------------------------------------------------------------------------------------------------- | ---- | -------------------------------------- | 78| deviceId | number | Yes | ID of the device to bind. It can be obtained by **queryDevices()**. | 79| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 80| callback | AsyncCallback<{deviceId: number, remote: [rpc.IRemoteObject](./js-apis-rpc.md#iremoteobject)}> | Yes | Callback invoked to return the communication object of the peripheral device bound.| 81 82**Error codes** 83 84| ID| Error Message | 85| -------- | ---------------------------------------- | 86| 401 | The parameter check failed. | 87| 22900001 | ExternalDeviceManager service exception. | 88 89**Example** 90 91```ts 92import deviceManager from "@ohos.driver.deviceManager"; 93import { BusinessError } from '@ohos.base'; 94 95try { 96 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 97 deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => { 98 console.error(`Device is disconnected`); 99 }, (error : BusinessError, data: { 100 deviceId : number; 101 remote : rpc.IRemoteObject; 102 }) => { 103 if (error) { 104 console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`); 105 return; 106 } 107 console.info(`bindDevice success`); 108 }); 109} catch (error) { 110 console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`); 111} 112``` 113 114## deviceManager.bindDevice 115 116bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>): Promise<{deviceId: number, 117 remote: rpc.IRemoteObject}>; 118 119Binds a peripheral device based on the device information returned by **queryDevices()**. This API uses a promise to return the result. 120 121You need to use [deviceManager.queryDevices](#devicemanagerquerydevices) to obtain the peripheral device information first. 122 123**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 124 125**System capability**: SystemCapability.Driver.ExternalDevice 126 127**Parameters** 128 129| Name | Type | Mandatory| Description | 130| ------------ | --------------------------- | ---- | ---------------------------- | 131| deviceId | number | Yes | ID of the device to bind. It can be obtained by **queryDevices()**.| 132| onDisconnect | AsyncCallback<number> | Yes | Callback to be invoked when the bound peripheral device is disconnected. | 133 134**Return value** 135 136| Type | Description | 137| ---------------------------------------------------------------------------------------------- | -------------------------------------------- | 138| Promise<{deviceId: number, remote: [rpc.IRemoteObject](./js-apis-rpc.md#iremoteobject)}> | Promise used to return the device ID and **IRemoteObject** object.| 139 140**Error codes** 141 142| ID| Error Message | 143| -------- | ---------------------------------------- | 144| 401 | The parameter check failed. | 145| 22900001 | ExternalDeviceManager service exception. | 146 147**Example** 148 149```ts 150import deviceManager from "@ohos.driver.deviceManager"; 151import { BusinessError } from '@ohos.base'; 152 153try { 154 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 155 deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => { 156 console.error(`Device is disconnected`); 157 }).then((data : { 158 deviceId : number; 159 remote : rpc.IRemoteObject; 160 }) => { 161 console.info(`bindDevice success`); 162 }, (error : BusinessError) => { 163 console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`); 164 }); 165} catch (error) { 166 console.error(`bindDevice fail. Code is ${error.code}, message is ${error.message}`); 167} 168``` 169 170## deviceManager.unbindDevice 171 172unbindDevice(deviceId: number, callback: AsyncCallback<number>): void 173 174Unbinds a peripheral device. This API uses an asynchronous callback to return the result. 175 176**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 177 178**System capability**: SystemCapability.Driver.ExternalDevice 179 180**Parameters** 181 182| Name | Type | Mandatory| Description | 183| -------- | --------------------------- | ---- | ------------------------------ | 184| deviceId | number | Yes | ID of the device to unbind. It can be obtained by **queryDevices()**.| 185| callback | AsyncCallback<number> | Yes | Callback invoked to return the result. | 186 187**Error codes** 188 189| ID| Error Message | 190| -------- | ---------------------------------------- | 191| 401 | The parameter check failed. | 192| 22900001 | ExternalDeviceManager service exception. | 193 194**Example** 195 196```ts 197import deviceManager from "@ohos.driver.deviceManager"; 198 199try { 200 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 201 deviceManager.unbindDevice(12345678, (error : BusinessError, data : number) => { 202 if (error) { 203 console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`); 204 return; 205 } 206 console.info(`unbindDevice success`); 207 }); 208} catch (error) { 209 console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`); 210} 211``` 212## deviceManager.unbindDevice 213 214unbindDevice(deviceId: number): Promise<number> 215 216Unbinds a peripheral device. This API uses a promise to return the result. 217 218**Required permissions**: ohos.permission.ACCESS_EXTENSIONAL_DEVICE_DRIVER 219 220**System capability**: SystemCapability.Driver.ExternalDevice 221 222**Parameters** 223 224| Name | Type | Mandatory| Description | 225| -------- | ------ | ---- | ------------------------------ | 226| deviceId | number | Yes | ID of the device to unbind. It can be obtained by **queryDevices()**.| 227 228**Error codes** 229 230| ID| Error Message | 231| -------- | ---------------------------------------- | 232| 401 | The parameter check failed. | 233| 22900001 | ExternalDeviceManager service exception. | 234 235**Return value** 236 237| Type | Description | 238| --------------------- | ------------------------- | 239| Promise<number> | Promise used to return the device ID.| 240 241**Example** 242 243```ts 244import deviceManager from "@ohos.driver.deviceManager"; 245import { BusinessError } from '@ohos.base'; 246 247try { 248 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 249 deviceManager.unbindDevice(12345678).then((data : number) => { 250 console.info(`unbindDevice success`); 251 }, (error : BusinessError) => { 252 console.error(`unbindDevice async fail. Code is ${error.code}, message is ${error.message}`); 253 }); 254} catch (error) { 255 console.error(`unbindDevice fail. Code is ${error.code}, message is ${error.message}`); 256} 257``` 258 259## Device 260 261Represents the peripheral device information. 262 263**System capability**: SystemCapability.Driver.ExternalDevice 264 265| Name | Type | Mandatory| Description | 266| ----------- | ------------------- | ---- | ---------- | 267| busType | [BusType](#bustype) | Yes | Bus type.| 268| deviceId | number | Yes | ID of the peripheral device. | 269| description | string | Yes | Description of the peripheral device.| 270 271## USBDevice 272 273Represents the USB device information. 274 275**System capability**: SystemCapability.Driver.ExternalDevice 276 277| Name | Type | Mandatory| Description | 278| --------- | ------ | ---- | ------------------- | 279| vendorId | number | Yes | Vendor ID of the USB device. | 280| productId | number | Yes | Product ID of the USB device.| 281 282## BusType 283 284Enumerates the device bus types. 285 286**System capability**: SystemCapability.Driver.ExternalDevice 287 288| Name| Value | Description | 289| ---- | --- | ------------- | 290| USB | 1 | USB bus.| 291