HoloLens Bluetooth Ports

edited September 2017 in Questions And Answers

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,

jschnettker

Answers

  • MondoMondo
    edited September 2017

    Hey,
    if the Unity compiler is complaining, try to use the #if NETFX_CORE flag.

    if NETFX_CORE

    • your code -

    endif

  • MondoMondo
    edited September 2017

    Hey, try to use the #if NETFX_CORE flag.

    if NETFX_CORE -your code- #endif

  • 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[i]);
            inputString += chars[i];
            if (chars[i] == '\0')
            {
                chars[i] ='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

    }

  • Try to put the "using System.IO;" under the flag, too.
    (like "using System.IO.Ports;")

  • @Mondo thanks for your reply!

    I tried it, but unfortunately it still prevents me from building.

  • Ports is not a valid type in System.IO in UWP - https://msdn.microsoft.com/en-us/library/mt185496.aspx

  • @Jesse_McCulloch Ok, thanks. Helps to know, what doesn't work.

    What alternatives do I have

  • @jschnettker : You have to use RFCOMM

Sign In or Register to comment.