Expo 和第三方库上的 USB 串行连接

问题描述 投票:0回答:1

我有一个项目需要Android和CDC串口设备之间的通信。 我尝试了几种方法但未能实现。

我尝试过的方法:

  1. 实现了 react-native-usb-serialport-for-android

const handlePressSave = async (data) => {
    try {
      const devices = await UsbSerialManager.list();
      devices.forEach(device => {
        console.log('Device ID:', device.deviceId, 'Product ID:', device.productId, 'Vendor ID:', device.vendorId);
        // Log other properties if needed
        console.log('-----------------------------------');
      });
      await UsbSerialManager.tryRequestPermission(5023);
      const usbSerialport = await UsbSerialManager.open(5023, { baudRate: 9600, parity: Parity.None, dataBits: 8, stopBits: 1 });

      const sub = usbSerialport.onReceived((event) => {
        console.log(event.deviceId, event.data);
      });
      // unsubscribe
      // sub.remove();

     const jsonData = { "tranType": "ping-test" };

      // Convert JSON object to JSON string
      const jsonString = JSON.stringify(jsonData);

      // Convert each character in the JSON string to its hex representation
      const hexData = Array.from(jsonString)
        .map(char => char.charCodeAt(0).toString(16).padStart(2, '0'))
        .join('');

      // Send the hex data using the serial port
      await usbSerialport.send(hexData);

      usbSerialport.close();
    } catch (error) {
      console.error('Error opening serial port:', error);
    }
  }
No Driver for Device

我遇到错误“没有设备驱动程序”,但无法解决。 因为作者正在基于 mik3y/usb-serial-for-android 库构建这个模块/库 我怀疑问题是:

  1. 我的 CDC 设备未注册,我无法手动将我的设备添加到代码中

  2. 我使用的库无法自动检测设备,因为与 v3.7.0 相比,v3.4.3 版本已过时。我看到那里note说v3.5及以上可以检测CDC 通过USB接口的设备。 (为什么我知道我的 CDC 设备可以工作是因为我使用 simpleusbterminal 测试了我的设备,该设备来自 mik3y/usb-serial-for-android 的同一作者 设备列表检测到我的设备为 CDC device list 并且能够从 Android ping 到 CDC 设备)

  3. 我还尝试了另一个库,react-native-serialport,但它已经过时了,无法 npm install

所以我试图找到其他本地方式来使用JAVA库。

  1. Expo Prebuild,我可以访问 andoird 文件夹,尝试导入依赖项并尝试使用该库,但我对 Java/Kotlin 完全陌生,所以我不知道如何通过我的 expo 应用程序与该库进行交互.

  2. Expo Modules API - 包装第三方本机库,它创建一个模块文件夹,其中还包含 android 文件夹。 但是当我按照 mik3y/usb-serial-for-android 的快速入门指南进行安装/使用时,模块文件夹没有 build.gradle 文件供我放置依赖项,我陷入了困境。

我也对 Expo Prebuild 和 Expo 模块 API 感到困惑。

我如何使用expo从android与CDC设备进行通信 请告诉我任何可能的解决方案,我可以学习或研究,以便在 expo/android 与 USB 串行设备之间建立串行连接!🙏🏻

react-native expo usbserial
1个回答
0
投票

你可以使用这个fork作为npm包https://github.com/SHO-Companies/react-native-usb-serialport-for-android。 我遇到了同样的问题并使用了那把叉子,所以问题就消失了。 在你的 package.json 中:

    "dependencies": {
...
    "react-native-usb-serialport-for-android": "github:SHO-Companies/react-native-usb-serialport-for-android",
...
  },
© www.soinside.com 2019 - 2024. All rights reserved.