USB Serial Connection on Expo & Third party library

58 Views Asked by At

I have a project that requires communication between Android and CDC serial devices. I have tried several approaches but failed to achieve them.

Approaches I tried:

  1. Implemented react-native-usb-serialport-for-android library

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

I faced the error "no driver for device" and I cant solve it. because the author is building this module/library based on mik3y/usb-serial-for-android library I suspect the issues are:

  1. My CDC device not regconized and i cant add my device manually to the code

  2. The library that I am using cannot auto detect device as version outdated v3.4.3 compared to v3.7.0. I saw there note stated v3.5 and above can detect CDC device by USB interface. (Why I knew my CDC device worked is because I tested my device with simpleusbterminal which from the same author from mik3y/usb-serial-for-android The device list detected my device as CDC device list and able to ping from android to CDC device)

  3. I also tried another library, react-native-serialport but it outdated and cannot be npm install

So I trying to find other native way to use JAVA libraries.

  1. Expo Prebuild, which I able to get access to andoird folder, trying to import dependencies and trying to use the library, but I am totally new to Java/Kotlin, so I dont know how to interact with the library with my expo app.

  2. Expo Modules API - Wrap third-party native libraries, it create a module folder which contained also android folder. but when I followed the installation/usage from mik3y/usb-serial-for-android's Quick Start guide, the module folder does not have the build.gradle file for me to put the dependencies and I am stucked.

I am also confused between Expo Prebuild & Expo modules API.

How i can communicate from android to CDC device using expo Please advise me any possible solution that I can learn or research in order to build serial connection between expo/android to a usb serial device!

1

There are 1 best solutions below

1
Абубакр Фазилов On

you can use this fork as npm package https://github.com/SHO-Companies/react-native-usb-serialport-for-android. I had the same issue and used that fork so the problem gone. In your package.json:

    "dependencies": {
...
    "react-native-usb-serialport-for-android": "github:SHO-Companies/react-native-usb-serialport-for-android",
...
  },