HoloLens Bluetooth Ports

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

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 serial_ports = new List();
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
}

Hi there,

Try changing your Scripting Runtime Version to “Experimental (4.6 equivalent)” and then reload the solution in Visual Studio.

System.IO.Ports is unlikely to work on UWP due to app sandboxing restrictions. You should try using this instead:

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?

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.

Did you manage to communicate?