Search Unity

Serial ports in Android

Discussion in 'Android' started by daurocg, Oct 26, 2015.

  1. daurocg

    daurocg

    Joined:
    Oct 24, 2015
    Posts:
    1
    Hi all

    sorry if this issue has been commented before, i have been 2 days looking on internet and here and not found any solution

    i try to make a hardware to connect to android in games of Unity with arduino, is a controller with buttons, joystick and sensor of movement

    i make great advances with PC but when i export to android, serial ports not work

    in PC I use:

    Code (CSharp):
    1. ....
    2. using System.IO.Ports;
    3. ....
    4. public class serial : MonoBehaviour {
    5. private SerialPort sp;
    6. void Start ()
    7. {
    8. sp = new SerialPort ("COM1",115200, Parity.None, 8, StopBits.One);
    9.         sp.Open ();
    10.         sp.ReadTimeout = 1;
    11. }
    12. void Update() {
    13.     if (sp.IsOpen) {
    14.         try
    15.             {
    16.                 moveObject(sp.ReadByte());  // here, in PC, call the function to use the serial data
    17.              }
    18.             catch ( System.Exception)
    19.             {
    20.                        //here in Android is the problem, the funcion call the exception
    21.             }
    22.         }
    23.     }
    I think the problem is because in Android, i cant work with ports like PC, i cant use the "port name" or something like this, I tested the device in Android with a terminal serial data, and the data is sending to android, but Unity app not is receiving the data

    any help will be great
    sorry by my english
     
  2. coldpizzapunk

    coldpizzapunk

    Joined:
    Aug 20, 2014
    Posts:
    30
    Did you ever get any help with this. Running into the same issue...
     
  3. darydol

    darydol

    Joined:
    May 19, 2017
    Posts:
    9
    Hi
    NGC6543
    I want the AndroidManifest.xml file information
    Please
     
  4. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Check the link above : Used Android source codes
     
  5. darydol

    darydol

    Joined:
    May 19, 2017
    Posts:
    9
    Thank you
     
  6. heartily0421

    heartily0421

    Joined:
    Sep 11, 2014
    Posts:
    2
    NGC6543, I appreciate your works. Your sharing is very helpful.

    For the people who feels mik3y's source is difficult (like me, :)
    -Connecting other USB device needs modifying VendorID, ProductID on UsbId.java and proper *SerialDriver.java
    https://github.com/mik3y/usb-serial...c/main/java/com/hoho/android/usbserial/driver
    -Data acquiring from device, refering this can help: NGC6543's ~/usbserial/util/USBSerialForAndroid.java
     
  7. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Thanks for your kind words of advice! I'm not used to build native android app so it was a really painful work, though it helped me to get some slight knowledges about the system.

    For your information, the plugin sourcecode 'USBSerialForAndroid.java' might not handle the PendingIntention (Requesting user permission to access USB device) properly. If anyone know a proper way to do that please feel free to fix it, thanks.
     
  8. lalo_va

    lalo_va

    Joined:
    Apr 14, 2017
    Posts:
    1
  9. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    @Cbbigham informed that the base library has a bug regarding CdcAcmSerialDriver.java.
    related link : https://www.bountysource.com/issues/23844779-incorrect-endpoint-assignment-in-cdc-driver-with-fix

    A quick fix :

     
    Last edited: Jul 24, 2017
  10. TheBarGuy

    TheBarGuy

    Joined:
    May 23, 2014
    Posts:
    5
    Hey @NGC6543
    I'm looking to connect an Espruino Pico to a Samsung Galaxy S7. As I understand, the Pico is a standard CDC USB device. I have an OTG adapter connected to the phone with the Pico attached to the adapter. I'm having trouble with Unity recognizing the Pico.

    I downloaded the unitypackage that you provided in your latest Dropbox link. I made a build consisting of your example scene and ran it on my Galaxy S7 phone. I connected the Pico to my phone and pressed "Refresh Device List" and nothing happened. I connected the phone to adb and used logcat to see the debug output. When I pressed "Refresh Device List" again, logcat output the string "#### Device List" but nothing along with it. When I pressed "Connect to Device0", the text within Unity told me that the connection failed.

    Is there a step I was missing when setting up your unitypackage's code in Unity? Would there be problem with it being a newer Samsung phone? Or do you think it's the Pico?

    Thanks in advance,
    -Eric
     
  11. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Hey @TheBarGuy, sorry for the late reply.
    The example scene was only meant to be used by myself, sorry. But it should have detected your device, anyway..

    I suggest you should refer to this reply(https://forum.unity3d.com/threads/serial-ports-in-android.363621/#post-3154882) above, and create your own library. The library has an example app that runs on your Android device. It detects your device (if it's supported). If it doesn't detect your device, maybe you should build your own custom plugins.
    Good luck!
    - Steve
     
  12. TheBarGuy

    TheBarGuy

    Joined:
    May 23, 2014
    Posts:
    5
    Hey Steve,

    I figured that my device wasn't a standard device that would already be supported so I went ahead and followed the instructions that @heartily0421 provided in the post above. After adding my device's VID and PID to its corresponding white list (for CDC devices in my case) and built out a new .aar library, my device connected using your example. Thanks for the reply!

    -Eric
     
    NGC6543 likes this.
  13. d-namikawa

    d-namikawa

    Joined:
    Apr 17, 2015
    Posts:
    1
    Hey @NGC6543
    Is there Write() method in your sample?
     
  14. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    Unfortunately no, I didn't implemented it (I don't need it).
    Maybe I'll update the plugin later, but now I'm having a lot other things to do.
    I opened all the codes up there so, be my guest and implement Write() functionality!
     
    d-namikawa likes this.
  15. NGC6543

    NGC6543

    Joined:
    Jun 3, 2015
    Posts:
    228
    The plugin is no longer available, sorry all.
     
  16. heartily0421

    heartily0421

    Joined:
    Sep 11, 2014
    Posts:
    2
    I think applying source is in charge of users themselves.
    Your work is useful. Thank you for your sharing.
     
  17. Ethelfeda

    Ethelfeda

    Joined:
    Dec 30, 2014
    Posts:
    1
    so sad...