Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
71241c19
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
71241c19
编写于
8月 30, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 30, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23449 guidance ArkTs sample code adaption
Merge pull request !23449 from dufresne_andy/master
上级
d6686d92
4f3de756
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
45 deletion
+71
-45
zh-cn/application-dev/device/externaldevice-guidelines.md
zh-cn/application-dev/device/externaldevice-guidelines.md
+48
-27
zh-cn/application-dev/device/usb-guidelines.md
zh-cn/application-dev/device/usb-guidelines.md
+23
-18
未找到文件。
zh-cn/application-dev/device/externaldevice-guidelines.md
浏览文件 @
71241c19
...
...
@@ -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}');
}
```
zh-cn/application-dev/device/usb-guidelines.md
浏览文件 @
71241c19
...
...
@@ -40,11 +40,11 @@ USB设备可作为Host设备连接Device设备进行数据传输。开发示例
1.
获取设备列表。
```
j
s
```
t
s
// 导入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设备。
```
j
s
```
t
s
// 打开设备,获取数据传输通道。
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.
释放接口,关闭设备。
```
j
s
```
t
s
usb
.
releaseInterface
(
pipe
,
interface1
);
usb
.
closePipe
(
pipe
);
```
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录