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 :
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] = ’
';
@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.