1# Peripheral Management Development 2 3 4## When to Use 5 6Peripheral devices (or simply peripherals) are auxiliary devices connected to a device through physical ports, such as handwriting tablets, printers, and scanners. Applications can query and bind peripherals by using the peripheral management capabilities, so that the device can use the customized capabilities provided by the peripheral drivers, such as the printer software. 7 8Currently, only tablets and specific devices are supported. 9 10 11## Available APIs 12 13The following table lists the open capabilities of peripheral management. For more information, see [Peripheral Management](../../reference/apis-driverdevelopment-kit/js-apis-driver-deviceManager.md). 14 15**Table 1** Open APIs for peripheral management 16 17| API | Description | 18| ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | 19| queryDevices(busType?: number): Array<Readonly<Device>> | Queries the peripheral list. | 20| bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>, callback: AsyncCallback<{deviceId: number, remote: rpc.IRemoteObject}>): void | Binds a peripheral device. This API uses an asynchronous callback to return the result. If the peripheral device is bound, the **IRemoteObject** of the device driver is returned for subsequent interaction with the device driver.| 21| bindDevice(deviceId: number, onDisconnect: AsyncCallback<number>): Promise<{deviceId: number, remote: rpc.IRemoteObject}> | Binds a peripheral device. This API uses a promise to return the result. | 22| unbindDevice(deviceId: number, callback: AsyncCallback<number>): void | Unbinds a peripheral device. This API uses an asynchronous callback to return the result. | 23| unbindDevice(deviceId: number): Promise<number> | Unbinds a peripheral device. This API uses a promise to return the result. | 24 25 26## How to Develop 27 28You can use the APIs to query and bind peripheral devices so as to use the customized driver capabilities of the peripherals. The development procedure is as follows: 29 30 311. Query the peripheral device list. 32 33 ```ts 34 import deviceManager from '@ohos.driver.deviceManager'; 35 import { BusinessError } from '@ohos.base'; 36 37 let matchDevice: deviceManager.USBDevice | null = null; 38 try { 39 let devices: Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB); 40 for (let item of devices) { 41 let device: deviceManager.USBDevice = item as deviceManager.USBDevice; 42 // Match the USB device based on productId and vendorId. 43 if (device.productId == 1234 && device.vendorId === 2345) { 44 matchDevice = device; 45 break; 46 } 47 } 48 } catch (error) { 49 let errCode = (error as BusinessError).code; 50 let message = (error as BusinessError).message; 51 console.error(`Failed to query device. Code is ${errCode}, message is ${message}`); 52 } 53 if (!matchDevice) { 54 console.error('No match device'); 55 } 56 ``` 57 58 59 602. Bind a peripheral device. 61 62 ```ts 63 import deviceManager from '@ohos.driver.deviceManager'; 64 import { BusinessError } from '@ohos.base'; 65 import rpc from '@ohos.rpc'; 66 67 interface DataType { 68 deviceId : number; 69 remote : rpc.IRemoteObject; 70 } 71 72 let remoteObject : rpc.IRemoteObject | null = null; 73 try { 74 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 75 deviceManager.bindDevice(12345678, (error : BusinessError, data : number) => { 76 console.error('Device is disconnected'); 77 }, (error : BusinessError, data : DataType) => { 78 if (error) { 79 console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`); 80 return; 81 } 82 console.info('bindDevice success'); 83 remoteObject = data.remote; 84 }); 85 } catch (error) { 86 let errCode = (error as BusinessError).code; 87 let message = (error as BusinessError).message; 88 console.error(`bindDevice fail. Code is ${errCode}, message is ${message}`); 89 } 90 if (!remoteObject) { 91 console.error('Bind device failed'); 92 } 93 ``` 94 95 96 973. Use the capabilities provided by the peripheral device driver. 98 99 ```ts 100 import { BusinessError } from '@ohos.base'; 101 import rpc from '@ohos.rpc'; 102 103 let option: rpc.MessageOption = new rpc.MessageOption(); 104 let data: rpc.MessageSequence = rpc.MessageSequence.create(); 105 let reply: rpc.MessageSequence = rpc.MessageSequence.create(); 106 data.writeString('hello'); 107 let code = 1; 108 // The remoteObject application can be obtained by binding the device. 109 let remoteObject : rpc.IRemoteObject | null = null; 110 // The code and data content varies depending on the interface provided by the driver. 111 if (remoteObject != null) { 112 (remoteObject as rpc.IRemoteObject).sendMessageRequest(code, data, reply, option) 113 .then(() => { 114 console.info('sendMessageRequest finish.'); 115 }).catch((error : BusinessError) => { 116 let errCode = (error as BusinessError).code; 117 console.error('sendMessageRequest fail. code:' + errCode); 118 }); 119 } 120 ``` 121 122 123 1244. Unbind the peripheral device after the device is used. 125 126 ```ts 127 import deviceManager from '@ohos.driver.deviceManager'; 128 import { BusinessError } from '@ohos.base'; 129 130 try { 131 // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId. 132 deviceManager.unbindDevice(12345678, (error : BusinessError, data : number) => { 133 if (error) { 134 let errCode = (error as BusinessError).code; 135 let message = (error as BusinessError).message; 136 console.error(`unbindDevice async fail. Code is ${errCode}, message is ${message}`); 137 return; 138 } 139 console.info(`unbindDevice success`); 140 }); 141 } catch (error) { 142 let errCode = (error as BusinessError).code; 143 let message = (error as BusinessError).message; 144 console.error(`unbindDevice fail. Code is ${errCode}, message is ${message}`); 145 } 146 ``` 147 148 149 150