鸿蒙原生应用元服务开发-设备管理USB服务开发步骤

鸿蒙时代
发布于 2024-6-6 15:44
浏览
0收藏

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

1.获取设备列表。

// 导入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,
              }
            ]
          }
        ]
      }
    ]
  }
]
*/

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.

2.获取设备操作权限。

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);
});

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

3.打开Device设备。

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

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));
});

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

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

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

  • 1.
  • 2.
  • 3.

本文引用参考HarmonyOS官方API9。

分类
收藏
回复
举报


回复
    相关推荐