提交 ff0de8b6 编写于 作者: D dufresne_andy

fixed 4f3de756 from https://gitee.com/dufresne__andy/docs/pulls/23449

Description: guidance ArkTs sample code adaption
Feature or Bugfix:Bugfix
Binary Source: No
Signed-off-by: Ndufresne_andy <zhaozhen30@huawei.com>
上级 70f21280
...@@ -30,20 +30,25 @@ ...@@ -30,20 +30,25 @@
1. 查询设备列表。 1. 查询设备列表。
```js ```ts
var matchDevice; import deviceManager from '@ohos.driver.deviceManager';
import { BusinessError } from '@ohos.base';
let matchDevice : deviceManager.USBDevice = null;
try { try {
let devices = deviceManager.queryDevices(deviceManager.BusType.USB); let devices : Array<Device> = deviceManager.queryDevices(deviceManager.BusType.USB);
for (let item of devices) { for (let item : Device of devices : Array<Device>) {
let device = item as deviceManager.USBDevice; let device : deviceManager.USBDevice = item as deviceManager.USBDevice;
// 通过productId和vendorId来匹配要使用的USB设备 // 通过productId和vendorId来匹配要使用的USB设备
if (device.productId == 1234 && device.vendorId === 2345) { if (device.productId == 1234 && device.vendorId === 2345) {
matchDevice = device; matchDevice = device;
break; break;
} }
} }
} catch (error) { } catch (error : BusinessError) {
console.error('Failed to query device. Code is ${error.code}, message is ${error.message}'); let errCode = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`Failed to query device. Code is ${errCode}, message is ${message}`);
} }
if (!matchDevice) { if (!matchDevice) {
console.error('No match device'); console.error('No match device');
...@@ -53,53 +58,69 @@ ...@@ -53,53 +58,69 @@
2. 绑定相应的设备。 2. 绑定相应的设备。
```js ```ts
var remoteObject; import deviceManager from '@ohos.driver.deviceManager';
import { BusinessError } from '@ohos.base';
let remoteObject : Object;
try { try {
deviceManager.bindDevice(matchDevice.deviceId, (error, data) => { deviceManager.bindDevice(matchDevice.deviceId, ((error : BusinessError), (data : Object)) => {
console.error('Device is disconnected'); console.error('Device is disconnected');
}, (error, data) => { }, ((error : BusinessError), (data : Object)) => {
if (error) { if (error) {
console.error('bindDevice async fail. Code is ${error.code}, message is ${error.message}'); console.error(`bindDevice async fail. Code is ${error.code}, message is ${error.message}`);
return; return;
} }
console.info('bindDevice success'); console.info('bindDevice success');
remoteObject = data.remote; remoteObject = data.remote;
}); });
} catch (error) { } catch (error : BusinessError) {
console.error('bindDevice fail. Code is ${error.code}, message is ${error.message}'); let errCode = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`bindDevice fail. Code is ${errCode}, message is ${message}`);
} }
``` ```
3. 绑定成功后使用设备驱动能力。 3. 绑定成功后使用设备驱动能力。
```js ```ts
let option = new rpc.MessageOption(); import deviceManager from '@ohos.driver.deviceManager';
let data = rpc.MessageSequence.create(); import { BusinessError } from '@ohos.base';
let repy = rpc.MessageSequence.create();
let option : MessageOption = new rpc.MessageOption();
let data : MessageSequence = rpc.MessageSequence.create();
let reply : MessageSequence = rpc.MessageSequence.create();
data.writeString('hello'); data.writeString('hello');
let code = 1; let code = 1;
// code和data内容取决于驱动提供的接口 // code和data内容取决于驱动提供的接口
remoteObject.sendMessageRequest(code, data, reply, option) remoteObject.sendMessageRequest(code, data, reply, option)
.then(result => { .then((result : string) => {
console.info('sendMessageRequest finish.'); console.info('sendMessageRequest finish.');
}).catch(error => { }).catch((error : BusinessError) => {
console.error('sendMessageRequest fail. code:' + error.code); let errCode = (error as BusinessError).code;
console.error('sendMessageRequest fail. code:' + errCode);
}); });
``` ```
4. 设备使用完成,解绑设备。 4. 设备使用完成,解绑设备。
```js ```ts
import deviceManager from '@ohos.driver.deviceManager';
import { BusinessError } from '@ohos.base';
try { try {
deviceManager.unbindDevice(matchDevice.deviceId, (error, data) => { deviceManager.unbindDevice(matchDevice.deviceId, (error : BusinessError, data : Object) => {
if (error) { if (error : BusinessError) {
console.error('unbindDevice async fail. Code is ${error.code}, message is ${error.message}'); let errCode = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`unbindDevice async fail. Code is ${errCode}, message is ${message}`);
return; return;
} }
console.info('unbindDevice success'); console.info('unbindDevice success');
}); });
} catch (error) { } catch (error : BusinessError) {
console.error('unbindDevice fail. Code is ${error.code}, message is ${error.message}'); let errCode = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error('unbindDevice fail. Code is ${errCode}, message is ${message}');
} }
``` ```
...@@ -40,11 +40,11 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例 ...@@ -40,11 +40,11 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例
1. 获取设备列表。 1. 获取设备列表。
```js ```ts
// 导入USB接口api包。 // 导入USB接口api包。
import usb from '@ohos.usbManager'; import usb from '@ohos.usbManager';
// 获取设备列表。 // 获取设备列表。
let deviceList = usb.getDevices(); let deviceList : Array<USBDevice> = usb.getDevices();
/* /*
deviceList结构示例 deviceList结构示例
[ [
...@@ -100,22 +100,25 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例 ...@@ -100,22 +100,25 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例
2. 获取设备操作权限。 2. 获取设备操作权限。
```js ```ts
let deviceName = deviceList[0].name; import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
let deviceName : string = deviceList[0].name;
// 申请操作指定的device的操作权限。 // 申请操作指定的device的操作权限。
usb.requestRight(deviceName).then(hasRight => { usb.requestRight(deviceName).then((hasRight : boolean) => {
console.info("usb device request right result: " + hasRight); console.info("usb device request right result: " + hasRight);
}).catch(error => { }).catch((error : BusinessError)=> {
console.info("usb device request right failed : " + error); console.info("usb device request right failed : " + error);
}); });
``` ```
3. 打开Device设备。 3. 打开Device设备。
```js ```ts
// 打开设备,获取数据传输通道。 // 打开设备,获取数据传输通道。
let pipe = usb.connectDevice(deviceList[0]); let pipe : USBDevicePipe = usb.connectDevice(deviceList[0]);
let interface1 = deviceList[0].configs[0].interfaces[0]; let interface1 : number = deviceList[0].configs[0].interfaces[0];
/* /*
打开对应接口,在设备信息(deviceList)中选取对应的interface。 打开对应接口,在设备信息(deviceList)中选取对应的interface。
interface1为设备配置中的一个接口。 interface1为设备配置中的一个接口。
...@@ -125,38 +128,40 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例 ...@@ -125,38 +128,40 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例
4. 数据传输。 4. 数据传输。
```js ```ts
import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
/* /*
读取数据,在device信息中选取对应数据接收的endpoint来做数据传输 读取数据,在device信息中选取对应数据接收的endpoint来做数据传输
(endpoint.direction == 0x80);dataUint8Array是要读取的数据,类型为Uint8Array。 (endpoint.direction == 0x80);dataUint8Array是要读取的数据,类型为Uint8Array。
*/ */
let inEndpoint = interface1.endpoints[2]; let inEndpoint : USBEndpoint = interface1.endpoints[2];
let outEndpoint = interface1.endpoints[1]; let outEndpoint : USBEndpoint = interface1.endpoints[1];
let dataUint8Array = new Uint8Array(1024); let dataUint8Array : Array<number> = new Uint8Array(1024);
usb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then(dataLength => { usb.bulkTransfer(pipe, inEndpoint, dataUint8Array, 15000).then((dataLength : number) => {
if (dataLength >= 0) { if (dataLength >= 0) {
console.info("usb readData result Length : " + dataLength); console.info("usb readData result Length : " + dataLength);
} else { } else {
console.info("usb readData failed : " + dataLength); console.info("usb readData failed : " + dataLength);
} }
}).catch(error => { }).catch((error : BusinessError) => {
console.info("usb readData error : " + JSON.stringify(error)); console.info("usb readData error : " + JSON.stringify(error));
}); });
// 发送数据,在device信息中选取对应数据发送的endpoint来做数据传输。(endpoint.direction == 0) // 发送数据,在device信息中选取对应数据发送的endpoint来做数据传输。(endpoint.direction == 0)
usb.bulkTransfer(pipe, outEndpoint, dataUint8Array, 15000).then(dataLength => { usb.bulkTransfer(pipe, outEndpoint, dataUint8Array, 15000).then((dataLength : number) => {
if (dataLength >= 0) { if (dataLength >= 0) {
console.info("usb writeData result write length : " + dataLength); console.info("usb writeData result write length : " + dataLength);
} else { } else {
console.info("writeData failed"); console.info("writeData failed");
} }
}).catch(error => { }).catch((error : BusinessError) => {
console.info("usb writeData error : " + JSON.stringify(error)); console.info("usb writeData error : " + JSON.stringify(error));
}); });
``` ```
5. 释放接口,关闭设备。 5. 释放接口,关闭设备。
```js ```ts
usb.releaseInterface(pipe, interface1); usb.releaseInterface(pipe, interface1);
usb.closePipe(pipe); usb.closePipe(pipe);
``` ```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册