serserm / react-native-turbo-serialport

React Native - Usb Serial Port Communication For Android Platform
MIT License
6 stars 0 forks source link

Device not supported with more USB slots #4

Closed vasyl91 closed 1 week ago

vasyl91 commented 1 month ago

I have a head unit with more USB slots what makes app receiving the error "Device not supported". It works only when I leave just one device attached.

I initially planned to fix the source in app code but it quickly turned out that it varies randomly (one time app shows connection with device under /dev/bus/usb/001/003 other time dev/bus/usb/001/005) even though it's attached to the same USB slot.

Any ideas or solution? Maybe there is a function, something like "search again if 'isSupported' is false"?

SerSerch commented 1 month ago

@vasyl91 Hi! I haven't had time to test for multi serialport yet. But I only connected one Arduino and it worked. I did it as it was written here felHR85/UsbSerial you can also try calling listDevices, it returns getPossibleSerialPorts

const serialport = useSerialport({
  // events callback
  onError,
  onReadData,
  onConnected,
  onDisconnected,
  onDeviceAttached,
  onDeviceDetached,
});

const {
  listDevices,
  connect,
} = serialport;

useEffect(() => {
  listDevices().then(res => {
    res.forEach(device => {
      device.isConnected().then(state => {
        // TODO
        if (!state) {
          device.connect();
          // the same
          // connect(device.deviceId);
        }
      });
    });
  });
}, []);

Let me know if there is anything I can do to help you.

vasyl91 commented 1 month ago

Hi, ok after some futile trials I finally spotted that I was using old version of the repository and now the code seems to be working but I'd have some questions. So far I tested it on a phone with single USB port.

Leaving useEffect emtpy changes nothing in terms of connection. It all happens in const serialport = useSerialport. So my idea was to prevent autoconnect. I tried this code:

initSerialport(config);

const config = {
  autoConnect: false,
}

I'm not sure if it's correct but couldn't figure out anything better (here I would like to kindly ask you to update installation.md and provide more usage). With autoConnect || false, in packages/react-native-turbo-serialport/src/initSerialport.ts it prevents connection. However this:

useEffect(() => {
  listDevices().then(res => {
    res.forEach(device => {
      device.isConnected().then(state => {
        // TODO
        if (!state) {
          device.connect();
          // the same
          // connect(device.deviceId);
        }
      });
    });
  });
}, []);

doesn't work anymore (works with autoConnect || true, ) and device remains not connected. Any ideas?

SerSerch commented 1 month ago

Oh, I see the bug. I try to fix it.

vasyl91 commented 1 month ago

Also if I use <usb-device vendor-id="9025" /> in usb_device_filter.xml android asks every time to allow the connection with arduino. This one:

   <usb-device vendor-id="6790" product-id="21795" /> <!-- 0x5523: CH341A -->
   <usb-device vendor-id="6790" product-id="29987" /> <!-- 0x7523: CH340 -->

displays a prompt where I can save the choice and if I do app keeps crashing every time I disconnect and try to reconnect (only reinstalling helps).

Issue not present on 1.0.1

SerSerch commented 1 month ago

The first version only worked with 1 usb. I completely rewrote the second version for Multiple Serial ports.

vasyl91 commented 1 month ago

Sure but I'm sharing my observations and so far only testing it on phone with one USB (1.0.1 and 2.1.0). Both versions work, but 2.1.0 crashes app if I let it to always use the serial USB while on 1.01 I can save the choice and disconnect/connect without crashes.

SerSerch commented 1 month ago

I will try to solve this problem, but I am not a java expert. I rewrote this module because other modules are outdated and their authors do not want to fix anything.

SerSerch commented 4 weeks ago

Hi! Please check the version 2.2.0 update example auto connect fixed but still no xml fix

vasyl91 commented 4 weeks ago

I have tested it so far on the phone. With minor change in renderButton it works flawlessly (had to cast deviceId as number, with that change onPress={onConnect(Number(deviceId))}> works as intended! Later I'll let you know how it works on head unit with multiple USB ports. Thank you very much!! BTW haven't noticed xml issue.

EDIT: leaving useEffect empty changes nothing, however all the rest works!

vasyl91 commented 3 weeks ago

Hi, I didn't have an oportunity to test it in the car so far but I have noticed that app automatically disconnects when I minimize it. Is it somehow related to autoconnect turned to false?

SerSerch commented 3 weeks ago

Hi, no. this is most likely a problem with react-native-screens install this module and add super.onCreate(null);

(kotlin and java examples) image

vasyl91 commented 3 weeks ago

A prosaic matter.. I had to restart the phone. I'll let you know how it works on radio with multiple USB ports ;)

vasyl91 commented 3 weeks ago

Nope, it still persists. Whenever I press the back button (I use it also in app with shell) it disconnects.

SerSerch commented 3 weeks ago

Hi. Please try to do the life cycle test

useEffect(() => {
  Alert.alert('test', 'mount');
  return () => {
    // unmount
  };
}, []);

if mount appears a second time, even without a serial port, then the problem is not in the module

maybe the problem is in the implementation, I don't know for sure. is there any example code?

SerSerch commented 1 week ago

Hi, is there any news with a solution to the problem?

vasyl91 commented 1 week ago

Hi, sorry for the late reply, the module works fine and the connection is very well! The problem was in React Native itself, because it's hard to maintain background processes in this environment. I had to migrate to java.