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:
I should use custom gradle files in my unity project to introduce mentioned repositories and dependencies
optional
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.
Yes, you’ll probably need to use “gradle template” feature from PlayerSettings.
This is one is probably needed as well - you’ll need to both introduce manifest template and put resource files.
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.