1

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:

04-24 08:28:11.788: W/UnityMain(4461): type=1400 audit(0.0:18): avc: denied { read } for name="/" dev="tmpfs" ino=6207 scontext=u:r:untrusted_app:s0 tcontext=u:eek:bject_r:device:s0 tclass=dir 04-24 08:28:11.788: W/UnityMain(4461): type=1400 audit(0.0:19): avc: denied { read } for name="/" dev="tmpfs" ino=6207 scontext=u:r:untrusted_app:s0 tcontext=u:eek:bject_r:device:s0 tclass=dir 04-24 08:28:11.810: I/Unity(4419): UnauthorizedAccessException: Access to the path "/dev" is denied. 04-24 08:28:11.810: I/Unity(4419): at System.IO.Directory.GetFileSystemEntries (System.String path, System.String searchPattern, FileAttributes mask, FileAttributes attrs) [0x00000] in :0 04-24 08:28:11.810: I/Unity(4419): at System.IO.Directory.GetFiles (System.String path, System.String searchPattern) [0x00000] in :0 04-24 08:28:11.810: I/Unity(4419): at System.IO.Ports.SerialPort.GetPortNames () [0x00000] in :0 04-24 08:28:11.810: I/Unity(4419): at DataGathering.Start () [0x00000] in :0 04-24 08:28:11.810: I/Unity(4419): 04-24 08:28:11.810: I/Unity(4419): (Filename: Line: -1)

I thought it might be problem with bluetooth permission, i added it in my android manifest but it doesnt get rid of the error.

<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">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <uses-permission android:name="android.permission.BLUETOOTH" />
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  <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">
    <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">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
  </application>

And this is my c# code for getting serial port names.

using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
public class DataGathering : MonoBehaviour {  
string[] ports;
        void Start () {
        lastDigits = new List <string>();
        checkArray = new List <string>();  
        ports = new string[10];
        filePath = Application.persistentDataPath;  

        ports = SerialPort.GetPortNames(); // this causes an error
        for(var k = 0; k < ports.Length; k++)
        {
            Debug.Log(ports[k]);
        }

    }
  • You can't do this on Android with C# AP . See [here](https://stackoverflow.com/questions/39348406/serial-port-on-android-for-use-with-unity#comment66035663_39348406) for a workaround. – Programmer Apr 24 '17 at 09:29
  • @Programmer You are saying I have to create costume java plugin to make this work ? Can you tell me how i would start to make one? I have no clue about java or about making costume plugins. Thanks – user3311120 Apr 24 '17 at 09:44
  • Yes, you need a custom plugin. Google "Android Unity plugin tutorial". You will find many tutorials for how to make a simple plugin. Once you know how to receive and send data to Java, Google "Android Java Serial Port tutorial". I haven't done this before, otherwise I would have made an answer with a code. I just don't have Android USB OTG to do the require test. – Programmer Apr 24 '17 at 09:49
  • @Programmer If c# doesnt work, can i use unityscript or is java only option? – user3311120 Apr 24 '17 at 10:26
  • You use C# to call Java functions. You can also call C# functions from Java. That's all I can tell you. If you want to use unityscript and Java, you can but finding tutorials for that would be hard. It should work with C# and Java – Programmer Apr 24 '17 at 10:33

0 Answers0