Access to serial port on Android device

Hello there !

My problem is the following : I’ve got an android application who is installed on a Xperia Tablet linked to an Arduino with USB. I need to open a serial port to send data to Arduino board but nothing happen. So my questions are :

  • Does Serial Ports are enabled on Android ?
  • Does anyone knows how to enable it in case of they’re not enabled ?

I used System.IO.ports, it’s worked perfectly on PC but not on Android device.
So I’ve got an error in console when i run my application :

Dll not found Exception : MonoPosixHelper

Does anyone knows what is it talking about ?

Thanks for reading !

Hmm, it shows it’s supported in the Normal Net 2.0, if your android is set to Subset 2.0 then it will fail.

Check out the namespace compatibility here (search for “Namespace System.IO.Ports” without the quotes on the page.)

Also check your Player Settings for android ← you will need to scroll down for android.

Here is a Exemple of my code. Nothing strange i think, but not working on tablet.

using UnityEngine;
using System.Collections;
using System;
using System.Threading;
using System.IO;
using System.IO.Ports;

public class TeensyNetWork : MonoBehaviour
{

    static private SerialPort serialPort = new SerialPort("COM6", 9600);

    void Start()
    {
        Debug.Log("Connecting ...");
        showPort();
        OpenConnection();
    }

    void showPort()
    {
        // Get a list of serial port names. 
        string[] ports = SerialPort.GetPortNames();

        Debug.Log("The following serial ports were found:");

        // Display each port name to the console. 
        foreach (string port in ports)
        {
            Debug.Log(port);
        }
    }

    void OpenConnection()
    {
        if (serialPort != null)
        {
            if (!serialPort.IsOpen)
            {
                serialPort.Open(); // ouverture de la connection
                serialPort.ReadTimeout = 1;
                Debug.Log("Port ouvert");
            }
        }

        else
        {
            Debug.Log("Port == null");
        }
    }

    public static void SendIntMessage(int pin, int value)
    {
        string pinMessage = Convert.ToString(pin);
        string valueMessage = Convert.ToString(value);

        char[] message = new char[6]; ;

        if (value >= 100) // si la valeur est superieure a 100
        {
            if (pin < 10)
            {
                message[0] = '0';
                message[1] = pinMessage[0];
                message[2] = valueMessage[0];
                message[3] = valueMessage[1];
                message[4] = valueMessage[2];
                message[5] = '

';
}
else
{
message[0] = pinMessage[0];
message[1] = pinMessage[1];
message[2] = valueMessage[0];
message[3] = valueMessage[1];
message[4] = valueMessage[2];
message[5] = ’
';
}
}
else if (value < 100 && value >= 10) // si la valeur est entre 99 & 10
{
if (pin < 10)
{
message[0] = ‘0’;
message[1] = pinMessage[0];
message[2] = ‘0’;
message[3] = valueMessage[0];
message[4] = valueMessage[1];
message[5] = ’
';

            }
            else
            {
                message[0] = pinMessage[0];
                message[1] = pinMessage[1];
                message[2] = '0';
                message[3] = valueMessage[0];
                message[4] = valueMessage[1];
                message[5] = '

';
}
}

        else if (value < 10) // si la valeur est inferieure a 10
        {
            if (pin < 10)
            {
                message[0] = '0';
                message[1] = pinMessage[0];
                message[2] = '0';
                message[3] = '0';
                message[4] = valueMessage[0];
                message[5] = '

';
}
else
{
message[0] = pinMessage[0];
message[1] = pinMessage[1];
message[2] = ‘0’;
message[3] = ‘0’;
message[4] = valueMessage[0];
message[5] = ’
';
}
}

        serialPort.Write(message, 0, 6);
        //Debug.Log("send");
    }    
}

@Zeterro have you sloved this problem? Can you tell me how?

@Zeterro I solved the problem by going to Android Studio and not using Unity…but I’d still like to use Unity if possible…but I didn’t have the time to figure it out, and Android Studio for me was easy.

Are you looking for something like this?
https://forum.unity3d.com/threads/serial-ports-in-android.363621/#post-3074558