UsbDevice.ets 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import usbManager from '@ohos.usbManager';
  2. import { BusinessError } from '@ohos.base';
  3. // 定义常用USB设备ID常量数组
  4. export const USBDeviceIds: Array<UsbDeviceInfo> = [
  5. {
  6. DeviceName: "Sony_Camera",
  7. VendorId: 0x054c,
  8. ProductId: 0x0caa
  9. },
  10. {
  11. DeviceName: "Scanning_Gun",
  12. VendorId: 0x23d0,
  13. ProductId: 0x0c80
  14. }
  15. ];
  16. // USB设备信息接口
  17. interface UsbDeviceInfo {
  18. DeviceName: string;
  19. VendorId: number;
  20. ProductId: number;
  21. }
  22. export
  23. class USBDeviceManager {
  24. private connectedDevices: Array<usbManager.USBDevice> = [];
  25. private pollTimer: number | null = null;
  26. private isPollingPaused: boolean = false;
  27. // 启动轮询(带暂停控制)
  28. public startPolling(interval: number, callback: (devices: usbManager.USBDevice[]) => void): void {
  29. this.stopPolling(); // 先停止已有轮询
  30. this.pollTimer = setInterval(async () => {
  31. if (this.isPollingPaused) return; // 暂停时跳过检测
  32. try {
  33. this.connectedDevices = await this.refreshDeviceList();
  34. callback(this.connectedDevices);
  35. } catch (error) {
  36. console.error('轮询失败:', (error as BusinessError).message);
  37. }
  38. }, interval) as number; // 鸿蒙中setInterval返回类型需转换
  39. }
  40. // 暂停轮询
  41. public pausePolling(): void {
  42. this.isPollingPaused = true;
  43. }
  44. // 恢复轮询
  45. public resumePolling(): void {
  46. this.isPollingPaused = false;
  47. }
  48. // 停止轮询(释放资源)
  49. public stopPolling(): void {
  50. if (this.pollTimer !== null) {
  51. clearInterval(this.pollTimer);
  52. this.pollTimer = null;
  53. }
  54. this.isPollingPaused = false;
  55. }
  56. /* 手动刷新设备列表(需主动调用)
  57. * @returns 返回刷新后的设备列表
  58. */
  59. public async refreshDeviceList(): Promise<Array<usbManager.USBDevice>> {
  60. try {
  61. this.connectedDevices = usbManager.getDevices();
  62. console.log('[USB] 当前连接的设备:', this.connectedDevices);
  63. return this.connectedDevices;
  64. } catch (error) {
  65. console.error('[USB] 获取USB设备失败:', (error as BusinessError).message);
  66. this.connectedDevices = [];
  67. return [];
  68. }
  69. }
  70. /* 检查是否有USB设备连接
  71. * @param forceRefresh 是否强制刷新设备列表
  72. */
  73. public async hasConnectedDevices(forceRefresh: boolean = false): Promise<boolean> {
  74. if (forceRefresh || this.connectedDevices.length === 0) {
  75. await this.refreshDeviceList();
  76. }
  77. return this.connectedDevices.length > 0;
  78. }
  79. /* 检查特定设备是否连接
  80. * @param vendorId 厂商ID
  81. * @param productId 产品ID
  82. * @param forceRefresh 是否强制刷新
  83. */
  84. public async isDeviceConnectedById(
  85. vendorId: number,
  86. productId: number,
  87. forceRefresh: boolean = false
  88. ): Promise<boolean> {
  89. if (forceRefresh) {
  90. await this.refreshDeviceList();
  91. }
  92. return this.connectedDevices.some(device =>
  93. device.vendorId === vendorId && device.productId === productId
  94. );
  95. }
  96. /* 检查预定义的设备是否连接
  97. * @param deviceName 设备名称(需在USBDeviceIds中定义)
  98. * @param forceRefresh 是否强制刷新
  99. */
  100. public async isDeviceConnectedByName(
  101. deviceName: string,
  102. forceRefresh: boolean = false
  103. ): Promise<boolean> {
  104. const deviceInfo = USBDeviceIds.find(item => item.DeviceName === deviceName);
  105. if (!deviceInfo) {
  106. console.warn(`[USB] 设备 ${deviceName} 不在预定义列表中`);
  107. return false;
  108. }
  109. return this.isDeviceConnectedById(deviceInfo.VendorId, deviceInfo.ProductId, forceRefresh);
  110. }
  111. /*
  112. * 获取当前缓存的设备列表(不主动刷新)
  113. */
  114. public getCachedDevices(): Array<usbManager.USBDevice> {
  115. return [...this.connectedDevices];
  116. }
  117. }