I need to receive a string from an inertial sensor paired via bluetooth to a Hololens 2. I´m able to connect the sensor in the configuration menu of HoloLens, the problem comes when trying to use serial port to receive data in Unity developed app.
I have managed to stablish a connection and receive the data using the System.IO.Ports.SerialPort class ( https://docs.microsoft.com/es-es/dotnet/api/system.io.ports.serialport?view=dotnet-plat-ext-5.0 ) in the Unity Editor. The problem comes when trying to stablish that same connection in the HoloLens, after some research I found out that System.IO.Ports.SerialPort class can´t be used in UWP apps, so I´m trying to use the Windows.Devices.SerialCommunication.SerialDevice class ( https://docs.microsoft.com/en-us/uwp/api/windows.devices.serialcommunication.serialdevice?view=winrt-19041 ). This is the piece of code that should stablish the connection, I´m using it in the Awake function of my script:
async void openConnection()
{
#if !UNITY_EDITOR
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
{
try
{
DeviceInformationCollection serialDeviceInfos = await DeviceInformation.FindAllAsync(SerialDevice.GetDeviceSelector());
if (serialDeviceInfos != null){
foreach (DeviceInformation serialDeviceInfo in serialDeviceInfos){
SerialDevice serialDevice = await SerialDevice.FromIdAsync(serialDeviceInfo.Id);
if (serialDevice != null)
{
// Found a valid serial device.
// Reading a byte from the serial device.
DataReader dr = new DataReader(serialDevice.InputStream);
string drData = dr.ReadString(60);
estadoSensor.text = drData;
}
} }
}
catch (Exception ex)
{
estadoSensor.text = ex.ToString();
System.Diagnostics.Debug.WriteLine(ex);
}
});
#endif
}
When calling the method SerialDevice.FromIdAsync(), even if serialDeviceInfo.Id is not null, the method throws a System.Exception type exception, which if I´m not wrong, it couldn´t be more generic. As you can see, I´m trying to call the method on the UI thread, because I´ve read that otherwise it won´t work, but even doing that, it is not working for me ( https://forum.unity.com/threads/hololens-device-permission-dialog-for-rfcomm-bluetooth-serial-port.513666/ ).
I have also added this piece of code to the package.appxmanifest file generated after building for UWP in the Unity Editor, in theory it should enable the serialcommunication capability
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
I hope I´ve managed to explain my problem properly.
Thanks in advance!
Bluetooth SerialPort can't work on HoloLens 2, because the HoloLens 2 device does not have the specific drivers installed for the sensor to do this via serial port connected. Besides, it is recommended you work with RFCOMM, more information please see: https://docs.microsoft.com/en-us/windows/uwp/devices-sensors/send-or-receive-files-with-rfcomm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.