提交 4f3de756 编写于 作者: D dufresne_andy

Description: guidance ArkTs sample code adaption

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