基本概念

USB服务是应用访问底层的一种设备抽象概念。开发者根据提供的USB API,可以获取设备列表、控制设备访问权限、以及与连接的设备进行数据传输、控制命令传输等。

运作机制

USB服务系统包含USB API、USB Service、USB HAL。

图1 USB服务运作机制

● USB API:提供USB的基础API,主要包含查询USB设备列表、批量数据传输、控制命令传输、权限控制等。

● USB Service:主要实现HAL层数据的接收、解析、分发以及对设备的管理等。

● USB HAL层:提供给用户态可直接调用的驱动能力接口。

场景介绍

Host模式下,可以获取到已经连接的USB设备列表,并根据需要打开和关闭设备、控制设备权限、进行数据传输等。

接口说明

USB服务主要提供的功能有:查询USB设备列表、批量数据传输、控制命令传输、权限控制等。

USB类开放能力如下,具体请查阅API参考文档

表1 USB类的开放能力接口

接口名

描述

hasRight(deviceName: string): boolean

判断是否有权访问该设备

requestRight(deviceName: string): Promise<boolean>

请求软件包的临时权限以访问设备。使用Promise异步回调。

removeRight(deviceName: string): boolean

移除软件包对设备的访问权限。

connectDevice(device: USBDevice): Readonly<USBDevicePipe>

根据getDevices()返回的设备信息打开USB设备。

getDevices(): Array<Readonly<USBDevice>>

获取接入主设备的USB设备列表。如果没有设备接入,那么将会返回一个空的列表。

setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number

设置设备的配置。

setInterface(pipe: USBDevicePipe, iface: USBInterface): number

设置设备的接口。

claimInterface(pipe: USBDevicePipe, iface: USBInterface, force ?: boolean): number

注册通信接口。

bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, timeout ?: number): Promise<number>

批量传输。

closePipe(pipe: USBDevicePipe): number

关闭设备消息控制通道。

releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number

释放注册过的通信接口。

getFileDescriptor(pipe: USBDevicePipe): number

获取文件描述符。

getRawDescriptor(pipe: USBDevicePipe): Uint8Array

获取原始的USB描述符。

controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout ?: number): Promise<number>

控制传输。

开发步骤

USB设备可作为Host设备连接Device设备进行数据传输。开发示例如下:

1.  获取设备列表。

```ts
// 导入USB接口api包。
import usb from '@ohos.usbManager';
// 获取设备列表。
let deviceList : Array<usb.USBDevice> = usb.getDevices();
/
deviceList结构示例
[
{
name: "1-1",
serial: "",
manufacturerName: "",
productName: "",
version: "",
vendorId: 7531,
productId: 2,
clazz: 9,
subClass: 0,
protocol: 1,
devAddress: 1,
busNum: 1,
configs: [
{
id: 1,
attributes: 224,
isRemoteWakeup: true,
isSelfPowered: true,
maxPower: 0,
name: "1-1",
interfaces: [
{
id: 0,
protocol: 0,
clazz: 9,
subClass: 0,
alternateSetting: 0,
name: "1-1",
endpoints: [
{
address: 129,
attributes: 3,
interval: 12,
maxPacketSize: 4,
direction: 128,
number: 1,
type: 3,
interfaceId: 0,
}
]
}
]
}
]
}
]
/

2.  获取设备操作权限。

```ts
import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
let deviceName : string = deviceList[0].name;
// 申请操作指定的device的操作权限。
usb.requestRight(deviceName).then((hasRight : boolean) => {
console.info("usb device request right result: " + hasRight);
}).catch((error : BusinessError)=> {
console.info("usb device request right failed : " + error);
});
```

  

3.  打开Device设备。

```ts
// 打开设备,获取数据传输通道。
let interface1 = deviceList[0].configs[0].interfaces[0];
let interface1 : number = deviceList[0].configs[0].interfaces[0];
/
打开对应接口,在设备信息(deviceList)中选取对应的interface。
interface1为设备配置中的一个接口。
/
usb.claimInterface(pipe, interface1, true);

  

let pipe : USBDevicePipe = usb.connectDevice(deviceList[0]);

4.  数据传输。

import usb from '@ohos.usbManager';
import { BusinessError } from '@ohos.base';
/*
读取数据,在device信息中选取对应数据接收的endpoint来做数据传输
(endpoint.direction == 0x80);dataUint8Array是要读取的数据,类型为Uint8Array。
*/
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 : BusinessError) => {
console.info("usb readData error : " + JSON.stringify(error));
});
// 发送数据,在device信息中选取对应数据发送的endpoint来做数据传输。(endpoint.direction == 0)
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 : BusinessError) => {
console.info("usb writeData error : " + JSON.stringify(error));
});

  

let inEndpoint : USBEndpoint = interface1.endpoints[2];

5.  释放接口,关闭设备。

```ts
usb.releaseInterface(pipe, interface1);
usb.closePipe(pipe);
```

  

HarmonyOS 设备管理开发:USB 服务开发指导的更多相关文章

  1. HarmonyOS USB DDK助你轻松实现USB驱动开发

    HDF(Hardware Driver Foundation)驱动框架是HarmonyOS硬件生态开放的基础,为开发者提供了驱动加载.驱动服务管理和驱动消息机制等驱动能力,让开发者能精准且高效地开发驱 ...

  2. Windows下USB磁盘开发系列二:枚举系统中所有USB设备

    上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...

  3. Windows下USB磁盘开发系列三:枚举系统中U盘、并获取其设备信息

    前面我们介绍了枚举系统中的U盘盘符(见<Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>).以及获取USB设备的信息(见<Windows下USB磁盘开发系列二:枚举系统中 ...

  4. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  5. Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲

    Java生鲜电商平台-SpringCloud微服务开发中的数据架构设计实战精讲 Java生鲜电商平台:   微服务是当前非常流行的技术框架,通过服务的小型化.原子化以及分布式架构的弹性伸缩和高可用性, ...

  6. 史上最全USB HID开发资料

    史上最全USB HID开发资料 史上最全USB HID开发资料,悉心整理一个月,亲自测试. 涉及STM32 C51 8051F例子都有源码,VC上位机例子以及源码,USB协议,HID协议,USB抓包工 ...

  7. USB入门开发的八个问题&USB枚举『转』

    USB 基本知识 USB的重要关键字: 1.端点:位于USB设备或主机上的一个数据缓冲区,用来存放和发送USB的各种数据,每一个端点都有惟一的确定地址,有不同的传输特性(如输入端点.输出端点.配置端点 ...

  8. 【新书推荐】《ASP.NET Core微服务实战:在云环境中开发、测试和部署跨平台服务》 带你走近微服务开发

    <ASP.NET Core 微服务实战>译者序:https://blog.jijiechen.com/post/aspnetcore-microservices-preface-by-tr ...

  9. Linux单设备多路USB串口的实现方法介绍

    某设备需要提供多路USB串口的功能给主机端使用,比如一路用作业务1通信功能,一路用作业务2通信功能,一路用作debug抓log用途,诸如此类.如下图所示. 要实现上述设备功能,可以参考如下步骤. 1) ...

  10. 地理围栏API服务开发

    地理围栏API服务开发 要使用华为地理围栏服务API,需要确保设备已经下载并安装了HMS Core(APK),并将Location Kit的SDK集成到项目中. 指定应用权限 如果需要使用地理围栏服务 ...

随机推荐

  1. 【Azure API 管理】APIM不能连接到 App Service (APIM cannot connect to APP service)

    问题描述 APIM 无法正确连接到App Service,返回500错误: { "statusCode": 500, "message": "Inte ...

  2. 【应用服务 App Service】App Service For Linux 中如何挂载一个共享文件夹呢? Mount Azure Storage Account File Share

    问题描述 使用Linux作为服务器运行Web App时,如何将 Storage Account 作为本地共享装载到 App Service for  Linux / Container 中的应用呢? ...

  3. vscode 合并分支 举例 master merge dev

    举例 将 dev 开发线 合并到 master 1 确定你在dev线,将dev代码改动全部提交 2 切换master,确定是最新代码,不确定就pull下,选择合并分支,见上图 3 在下拉的提示框中选择 ...

  4. 替换掉Chrome浏览器的新标签页【已做好自己的插件,并提供下载】

    https://jingyan.baidu.com/article/fc07f9891b256312ffe5190a.html 我做好了自己的 Chrome 新插件 https://files.cnb ...

  5. 个性化的单芯片的回声消除(AEC)解决方案

    概述   这些年随着智能化产品的广泛应用,各种新型音频产品也层出不穷,在这个古老的领域,传统的回声消除方案一般是功耗高,成本非常高,集成性差.无法满足新产品新市场对回声消除的低成本低功耗个性化需求等特 ...

  6. [模板]01trie,维护异或最大值

    // 查询异或最大值,每次插入和查询时间都是log(C) template<class T> class trie01 { vector<vector<T>> tr ...

  7. Mysql范式

    什么是范式? "范式(NF)"是"符合某一种级别的关系模式的集合,表示一个关系内部各属性之间的联系的合理化程度".很晦涩吧?实际上你可以把它粗略地理解为一张数据 ...

  8. 3D渲染速度慢,花重金买显卡还是用云渲染更划算

    3D渲染对建筑师和设计师来说并不陌生,3D渲染的过程中出现渲染卡顿.特殊材质难以渲染,或者本地配置不足.本地渲染资源不够时,常常会影响工作效率.本文比较了3D渲染时,为提高工作效率,买显卡还是用云渲染 ...

  9. Retrofit源码分析

    目录介绍 1.首先回顾Retrofit简单使用方法 2.Retrofit的创建流程源码分析 2.1 Retrofit对象调用Builder()源码解析 2.2 Retrofit对象调用baseUrl( ...

  10. Button按钮Effect的用法

    教大家写一个好看的Button按钮 代码简单粗暴 <Grid > <Border Width="200" Height="30" Margin ...