Access to serial port on Android device

Currently I am working on an application build in Unity that controls Numato Lab USBGPIO8 module (it will simply turn on and off channels on gpio when conditions in a separate script in app would be met). Whole project is based on ARFoundation.

Opening and controlling serial port using Unity editor and PC works fine (my PC uses COM4 port). However, when building on Android I am getting an “IOException error: permission denied” when script tries to open serial port (gpio is now connected to smartphone through usb cable):

USB Device Info app shows the device path is /dev/bus/usb/001/002, so I use this as a port adress in the script:

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

public class SerialConnector : MonoBehaviour
{
    SerialPort serialPort = new SerialPort("/dev/bus/usb/001/002");
    //COM4 in unity editor pc
    void Start()
    {
        OpenPort();
        GPIOSetChannelOn(1);
    }

    private void OpenPort()
    {
        Debug.Log("Opening port");
        if (!serialPort.IsOpen)
            serialPort.Open();
        serialPort.BaudRate = 19200;
    }
    private void GPIOSetChannelOn(int channel)
    {
       ...
    }
    private void GPIOSetChannelOff(int channel)
    {
        ...
    }
}

I have added a line to manifest file, that allows external read write using custom AndroidManifest.xml file in Assets/Plugins/Android folder:

 <?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools" package="com.unity3d.player" xmlns:android="http://schemas.android.com/apk/res/android" tools:node="merge">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application>
        <activity android:theme="@style/UnityThemeSelector" android:name="com.unity3d.player.UnityPlayerActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true"/>
        </activity>
    </application>
</manifest>

Despite these steps, the problem persists. Is there a way to resolve this issue without rooting my smartphone?

All right, so I have been searching for several days and what I found out that it is impossible to successfully access serial port without any plugin.

I have found some serial port plugins like: GitHub - mik3y/usb-serial-for-android: Android USB host serial driver library for CDC, FTDI, Arduino and other devices.

So as far as I understand introducing java plugins into unity there is a list of steps that should help here:

  1. Build an app with Android Studio using linked repository
  2. Extract classes.jar from the app
  3. Include it in project and call it from the c# script in unity?

There’s an easier way, just throw java files in your project, and that’s it :slight_smile:

They will then end up in gradle project when you build, no need to create jar file

1 Like

Thanks for the reply.

Honestly… knowing this I still lack some understanding how to use this library.

Found this thread, which appears to be a solution for the problem:
https://discussions.unity.com/t/726073

And the sentence “You can build a plugin that can communicate with Unity from this library.” means nothing to me.

Do I understand this correctly? As the Quick Start in link below says:
https://github.com/mik3y/usb-serial-for-android

  1. I should use custom gradle files in my unity project to introduce mentioned repositories and dependencies
  2. optional
  3. I should write java code that contains some specific methods, which will be then called from C# scripts using AndroidJavaClass ?? How to call this usb-serial-for-android library then?

I would really appreciate some help or pointing some directions for learning that will enable me to understand this.

  1. Yes, you’ll probably need to use “gradle template” feature from PlayerSettings.
  2. This is one is probably needed as well - you’ll need to both introduce manifest template and put resource files.
  3. Yes

If it only would be java files, then yes it’s enough to throw them in Unity project and that’s it.

But since in your case, it seems to involve java files + manifest + resources, it’s probably easier to create Android Library project via Android Studio, produce .aar file and put that in Unity project.

1 Like