Dear all,
I would like to use a Bluetooth serial device on the HoloLens. In theory, it should work like this:
Add this manually to you package.appxmanifest (because Unity does not offer a option to include it!)
<DeviceCapability Name="bluetooth.rfcomm">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
Then, you find your Bluetooth device ID and try to open it with:
myRfcommDeviceService = await RFcommDeviceService.FromIdAsync(serial_Id);
At this point, Windows should display a device permission consent dialog (or permission request window) where the user can grant or deny the access to the device.
However, the pitfall is, that when FromIdAsync() is called from any background thread or task, it will silently fail, and you cannot access your device due to some error.
Well, after digging around some time, I found the solution: call FromIdAsync() from the UI-Thread ! But by default, your scripts don’t run on the UI-thread, so you have to use a trick:
UnityEngine.WSA.Application.InvokeOnUIThread(../*call FromIdAsync from here*/..)
Ok, now for the real problem: this freezes my app on the very first run. After I close the window (I need to sleep by using the power button), and try again, the permission dialog is shown.
The behaviour also depends on the my build settings: debug, release or master. master never seems to work.
I think the main problem is the context switch, because the Windows permission dialog is displayed in the “main” HoloLens area after your application is put in some sort of background mode.
So, does anyone also have this problem? Can Unity improve the experience?
Tested with Unity 2017.1.2