Search Unity

HoloLens Bluetooth Ports

Discussion in 'VR' started by Hololens17, Sep 25, 2017.

  1. Hololens17

    Hololens17

    Joined:
    Sep 6, 2017
    Posts:
    6
    Hey guys!

    I'm facing the problem, that I can't use the SerialPort Class in my WSA UWP HoloLens app, to read bluetooth input, because the Unity compiler complains the following, when I'm trying to build:

    error CS0234: The type or namespace name 'Ports' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

    Any help about fixing this is, or alternatives is greatly appreciated.

    Thank you in advance,

    Hololens17
     
  2. Hololens17

    Hololens17

    Joined:
    Sep 6, 2017
    Posts:
    6
    I tried to fix it, using the #if NETFX_CORE tag, but it keeps preventing me from building.

    That's the code I'm currently using:

    using UnityEngine;
    using System.IO;
    #if NETFX_CORE
    using System.IO.Ports;
    #endif
    using System.Collections.Generic;
    using System;
    //using Windows.Devices.Bluetooth.Rfcomm;
    public class sizeManipulator: MonoBehaviour
    {
    #if NETFX_CORE
    public SerialPort serialPort = new SerialPort("COM3", 9600, Parity.None, 8);
    #endif
    public static string strIn;
    //private char[] chars=new char[4];
    private string inputString;
    private float size;
    Vector3 startScale, scaleManipulation;
    void Start()
    {
    //RfcommServiceId.AsString(RfcommServiceId.SerialPort);
    //GetPortNames();
    #if NETFX_CORE
    string[] ports;
    ports = SerialPort.GetPortNames();

    foreach (String port in ports)
    {
    Debug.Log(port);
    }
    OpenConnection();
    #endif
    startScale = new Vector3(transform.localScale.x, transform.localScale.y, transform.localScale.z);
    scaleManipulation = new Vector3(0f, 0f, 0f);
    }
    void Update()
    {
    inputString = string.Empty;
    #if NETFX_CORE
    inputString=serialPort.ReadLine(/*chars,0,4*/);
    #endif
    /*for (int i=0; i<4; i++)
    {
    Debug.Log(chars);
    inputString += chars;
    if (chars == '\0')
    {
    chars ='0';
    }
    }*/
    Debug.Log(inputString);
    //inputString += ".0";
    scaleManipulation.x =(((float.Parse(inputString))/1023)-0.5f);
    transform.localScale=startScale+scaleManipulation;
    }
    /*void GetPortNames()
    {
    int p = (int)System.Environment.OSVersion.Platform;
    List<string> serial_ports = new List<string>();
    if(p == 4 || p == 128 || p == 6)
    {
    string[] ttys = Directory.GetFiles("/dev/", "tty.*");
    foreach(string dev in ttys)
    {
    if (dev.StartsWith("/dev/tty.*"))
    serial_ports.Add(dev);
    Debug.Log(System.String.Format(dev));
    }
    }
    }*/
    #if NETFX_CORE
    public void OpenConnection()
    {
    if(serialPort != null)
    {
    if(serialPort.IsOpen)
    {
    serialPort.Close();
    Debug.Log("Closing port, because it was already open!");
    }
    else
    {
    serialPort.Open();
    serialPort.ReadTimeout = 50;
    Debug.Log("Port Opened!");
    }
    }
    else
    {
    if(serialPort.IsOpen)
    {
    print("Port is already open");
    }
    else
    {
    print("Port == null");
    }
    }
    }
    void OnApplicationQuit()
    {
    serialPort.Close();
    Debug.Log("Port closed!");
    }
    #endif
    }
     
  3. JasonCostanza

    JasonCostanza

    Unity Technologies

    Joined:
    May 23, 2017
    Posts:
    404
    Hi there,

    Try changing your Scripting Runtime Version to "Experimental (4.6 equivalent)" and then reload the solution in Visual Studio.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
  5. Hololens17

    Hololens17

    Joined:
    Sep 6, 2017
    Posts:
    6
    Thanks for your replies!
    @JasonCostanza Unfortunately, it did not change anything. But my Problem is Building the app in Unity not in VS.
    @Tautvydas-Zilys Unity does not find any windows library. I don't know why, but otherwise, I would have used the Windows.Devices.Bluetooth.rfcomm namespace.

    What alternatives do I have?
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Did you wrap the Windows API usage in "#if ENABLE_WINMD_SUPPORT/#endif"? Those APIs are only available in the built player and they cannot be used in the editor - that's why you have to use those defines.
     
  7. diogoiub

    diogoiub

    Joined:
    May 8, 2015
    Posts:
    4
    Did you manage to communicate?