Search Unity

Serial port on android phone

Discussion in 'Editor & General Support' started by walkeraki, Apr 24, 2017.

  1. walkeraki

    walkeraki

    Joined:
    Jan 31, 2014
    Posts:
    35
    I am having problem with serial ports on my android phone. In Unity editor everything works fine.
    First I am getting list of all serial ports, then i connect to the one i need and i get data from it.
    Now when i export my app to android phone, i get following error:
    I thought it might be problem with bluetooth permission, i added it in my android manifest but it doesnt get rid of the error.
    Code (CSharp):
    1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jj.ll" xmlns:tools="http://schemas.android.com/tools" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal">
    2.   <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
    3.   <uses-permission android:name="android.permission.BLUETOOTH" />
    4.   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    5.   <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
    6.     <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="sensorLandscape" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale">
    7.       <intent-filter>
    8.         <action android:name="android.intent.action.MAIN" />
    9.         <category android:name="android.intent.category.LAUNCHER" />
    10.         <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
    11.       </intent-filter>
    12.       <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    13.     </activity>
    14.   </application>
    And this is my c# code for getting serial port names.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO.Ports;
    4. using System;
    5. using System.Collections.Generic;
    6. using System.IO;
    7. using System.Threading;
    8. using System.Text.RegularExpressions;
    9. public class DataGathering : MonoBehaviour {  
    10. string[] ports;
    11.         void Start () {
    12.         lastDigits = new List <string>();
    13.         checkArray = new List <string>();  
    14.         ports = new string[10];
    15.         filePath = Application.persistentDataPath;  
    16.        
    17.         ports = SerialPort.GetPortNames();
    18.         for(var k = 0; k < ports.Length; k++)
    19.         {
    20.             Debug.Log(ports[k]);
    21.         }
    22.         /*if(GameObject.Find("Info")!=null)
    23.         {
    24.             subjectID = int.Parse(GameObject.Find("Info").transform.GetChild(1).name);
    25.             testID = int.Parse(GameObject.Find("Info").transform.GetChild(0).name);
    26.         }
    27.         if(File.Exists(filePath+""+subjectID+"_"+testID+".txt"))
    28.         {
    29.             File.Delete(filePath+""+subjectID+"_"+testID+".txt");
    30.         }
    31.         writer = new StreamWriter(filePath+""+subjectID+"_"+testID+".txt", true);*/
    32.     }
     
  2. qiwi92

    qiwi92

    Joined:
    Jun 2, 2016
    Posts:
    1
    Hey there, did you manage to solve your problem? I am trying a similar thing.
     
  3. Tudor

    Tudor

    Joined:
    Sep 27, 2012
    Posts:
    150
    I'm also getting a permission denied error when trying to connect to the serial interface in android. I GetPortNames(), find that the USB connected to my phone (an arduino) is under /dev/ttyACM0, but when I try to open that port it says "permission denied".

    If I run a terminal emulator app with this command: ls -l /dev/ttyACM*
    I see that I have permission denied on all.

    How the heck do you fix this? Do I need to root the phone to be able to change anything and communicate via USB?
     
  4. zenasprime

    zenasprime

    Joined:
    Mar 31, 2010
    Posts:
    166
    I'm having a similar issue. Was there ever a resolution to this?