• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8
9## Available APIs
10
11The following table lists the open capabilities of peripheral management. For more information, see [Peripheral Management](../reference/apis/js-apis-driver-deviceManager.md).
12
13**Table 1** Open APIs for peripheral management
14
15| API                                                                                                                                                     | Description                                                                                   |
16| ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
17| queryDevices(busType?: number): Array<Readonly<Device>>                                                                                         | Queries the peripheral list.                                                                       |
18| 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.|
19| 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.                                                                 |
20| unbindDevice(deviceId: number, callback: AsyncCallback<number>): void                                                                                 | Unbinds a peripheral device. This API uses an asynchronous callback to return the result.                                                                             |
21| unbindDevice(deviceId: number): Promise<number>                                                                                                       | Unbinds a peripheral device. This API uses a promise to return the result.                                                                             |
22
23
24## How to Develop
25
26You 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:
27
28
291. Query the peripheral device list.
30
31  ```ts
32  import deviceManager from '@ohos.driver.deviceManager';
33  import type { BusinessError } from '@ohos.base';
34
35  let matchDevice : deviceManager.USBDevice | null = null;
36  try {
37    let devices : Array<deviceManager.Device> = deviceManager.queryDevices(deviceManager.BusType.USB);
38    for (let item of devices) {
39      let device : deviceManager.USBDevice = item as deviceManager.USBDevice;
40      // Match the USB device based on productId and vendorId.
41      if (device.productId == 1234 && device.vendorId === 2345) {
42        matchDevice = device;
43        break;
44      }
45    }
46  } catch (error) {
47    let errCode = (error as BusinessError).code;
48    let message = (error as BusinessError).message;
49    console.error(`Failed to query device. Code is ${errCode}, message is ${message}`);
50  }
51  if (!matchDevice) {
52    console.error('No match device');
53  }
54  ```
55
562. Bind a peripheral device.
57
58  ```ts
59  import deviceManager from '@ohos.driver.deviceManager';
60  import type { BusinessError } from '@ohos.base';
61  import type rpc from '@ohos.rpc'
62
63  let remoteObject : rpc.IRemoteObject;
64  try {
65    // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
66    deviceManager.bindDevice(12345678, (error : BusinessError, data) => {
67      console.error('Device is disconnected');
68    }, (error : BusinessError, data) => {
69      if (error) {
70        console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
71        return;
72      }
73    console.info('bindDevice success');
74    remoteObject = data.remote;
75  });
76  } catch (error) {
77    let errCode = (error as BusinessError).code;
78    let message = (error as BusinessError).message;
79    console.error(`bindDevice fail. Code is ${errCode}, message is ${message}`);
80  }
81  if (!remoteObject) {
82    console.error('Bind device failed');
83  }
84   ```
85
863. Use the capabilities provided by the peripheral device driver.
87
88  ```ts
89  import type { BusinessError } from '@ohos.base';
90  import rpc from '@ohos.rpc'
91
92  let option : rpc.MessageOption = new rpc.MessageOption();
93  let data : rpc.MessageSequence = rpc.MessageSequence.create();
94  let reply : rpc.MessageSequence = rpc.MessageSequence.create();
95  data.writeString('hello');
96  let code = 1;
97  // The remoteObject application can be obtained by binding the device.
98  let remoteObject : rpc.IRemoteObject;
99  // The code and data content varies depending on the interface provided by the driver.
100  remoteObject.sendMessageRequest(code, data, reply, option)
101    .then(() => {
102      console.info('sendMessageRequest finish.');
103    }).catch((error : BusinessError) => {
104      let errCode = (error as BusinessError).code;
105      console.error('sendMessageRequest fail. code:' + errCode);
106    });
107  ```
108
1094. Unbind the peripheral device after the device is used.
110
111  ```ts
112  import deviceManager from '@ohos.driver.deviceManager';
113  import type { BusinessError } from '@ohos.base';
114
115  try {
116    // For example, deviceId is 12345678. You can use queryDevices() to obtain the deviceId.
117    deviceManager.unbindDevice(12345678, (error : BusinessError, data) => {
118      if (error) {
119        let errCode = (error as BusinessError).code;
120        let message = (error as BusinessError).message;
121        console.error(`unbindDevice async fail. Code is ${errCode}, message is ${message}`);
122        return;
123      }
124      console.info(`unbindDevice success`);
125  });
126  } catch (error) {
127    let errCode = (error as BusinessError).code;
128    let message = (error as BusinessError).message;
129    console.error(`unbindDevice fail. Code is ${errCode}, message is ${message}`);
130  }
131  ```
132