michielpost / Q42.HueApi

C# helper library to talk to the Philips Hue bridge
MIT License
411 stars 114 forks source link

Not working on HoloLens2? #261

Closed jelmer3000 closed 2 years ago

jelmer3000 commented 2 years ago

I'm trying Q42.HueApi as a NuGet package in Unity. Works fine on Windows and Mac in the editor but when deployed to HoloLens 2 it does not work. There are 2 clues in the Debug.Log that I'm trying to understand:

_'HL2.exe' (Win32): Loaded 'C:\Windows\SysArm32\winnsi.dll'. onecore\net\netprofiles\service\src\nsp\dll\namespaceserviceprovider.cpp(547)\nlanspc.dll!5917A027: (caller: 773B6DCB) LogHr(2) tid(ba0) 8007277C No such service is known. The service cannot be found in the specified name space.

And this one: error: System.Reflection.Emit.DynamicMethod::.ctor ..get's thrown after checking for Hue Bridges var bridges = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

What could this be?

michielpost commented 2 years ago

I don't know , the Q42.HueApi library doesn't use much exotic stuff. It should work on most .net platforms. Maybe you can run the source code and see where it fails.

System.Reflection is used here: https://github.com/Q42/Q42.HueApi/blob/master/src/Q42.HueApi/Converters/StringNullableEnumConverter.cs

Not sure if that code is still needed.

jelmer3000 commented 2 years ago

Thanks, I tried importing the source straight into Unity but Unity doesn't support C#8 yet it appears. I also tried changing StringNullableEnumConverter.cs (just removed the references to System.Reflection) but somehow the error still showed up. I now read that it could be related to newtonsoft JSON. It is suggested to use the "netstandard" version of newtonsoft JSON. I'm not so familiar with C#, especially not outside of Unity, but I'll look into that one of the coming days :)

jelmer3000 commented 2 years ago

EDIT: the problem below was probably caused by code stripping. Trying to fix that now. EDIT2: I get this as well when compiled to UWP x64 and deployed on WIndows10. Still everything works in the editor.. Super strange.

Followed some advice I found online and swapped out the newtonsoft JSON package wiht a .net standard 2 version. Now this error shows on HoloLens2: There was a problem: Unable to find a constructor to use for type Q42.HueApi.NuPnPResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path '[0].id', line 1, position 7.

The app is still working fine on Windows in the Unity editor. Maybe this Newtonsoft is not working on HoloLens2?

jelmer3000 commented 2 years ago

I tried to create a constructor for newtonsoft JSON, like this, hoping that it would solve the error but alas. I still get the same error. What could be wrong with this code?

using Newtonsoft.Json;

namespace Q42.HueApi
{
    /// <summary>
    /// Model for response from http://www.meethue.com/api/nupnp
    /// </summary>
    /// 
    public class NuPnPResponse
    {
        public string Id { get; set; }
        public string InternalIpAddress { get; set; }
        public string MacAddress { get; set; }

        public NuPnPResponse()
        {

        }

        [JsonConstructor]
        public NuPnPResponse(string _id, string _internalIpAddress, string _macAddress)
        {
            Id = _id;
            InternalIpAddress = _internalIpAddress;
            MacAddress = _macAddress;
        }
    }
}
michielpost commented 2 years ago

The NuPnP request and response is very basic. If that doesn't work on the HoloLens 2, the rest is never going to work.

It looks like a really specifc issue with the HoloLens 2, maybe it's because it's running on ARM? That's why it probably runs fine in the emulator that's running on your PC? Also found this, might help: https://localjoost.github.io/migrating-to-mrtk2newtonsoftjson-aka/

My advice is to get some simple Newtonsoft code to run on the HoloLens 2 first,, if that works then use this library. I don't have access to a HoloLens 2 or other ARM device, so unable to test it.

jelmerS2 commented 2 years ago

Yeah true.. This isn't a Q42.HueAPI problem. You can close the issue if you want. Or leave it for now and I will post a solution if I find one.. A colleague mentioned this: https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/What-even-is-AOT and this https://github.com/jilleJr/Newtonsoft.Json-for-Unity/wiki/Fix-AOT-using-AotHelper so I'll look into that next.

jelmer3000 commented 2 years ago

This was solved by using https://github.com/jilleJr/Newtonsoft.Json-for-Unity/

and by adding the respective class to link.xml https://github.com/jilleJr/Newtonsoft.Json-for-Unity/issues/143