Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.

Updating UI Text from MQTT Message Received

bkfichterbkfichter
edited August 2016 in Discussion

I am writing an app in Unity that will involve receiving MQTT messages using the UWP version of the M2MQTT dll. I have gotten everything working and can receive messages. I view this by setting a breakpoint and viewing the results. I am trying to get the result into a UI Text object.

The MQTT result comes in on a different thread. In normal UWP programming you do something like this in the callback method:

private async void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            //DO UI THREAD CODE HERE
        } );
    }

(btw...if there is a better way to do this, I am all ears)

However you must wrap this code in "#if !UNITY_EDITOR" so it won't blow up in the Unity editor. So my full method in Unity looks like this:

public Text resultTextField;  //assigned in the Unity editor

...
...

#if !UNITY_EDITOR
    private async void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            resultTextField.text = System.Text.Encoding.UTF8.GetString(e.Message);
        });
    }
#endif

However I am getting this error for "resultTextField.text = System.Text.Encoding.UTF8.GetString(e.Message);":

get_isActiveAndEnabled can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

I think this is telling me that it is not accessing the Text object on the main thread, which is what is supposed to happen. So I am not sure how to fix this. How do I, using the UWP framework, access and update the object on the main thread?

Thanks

Best Answer

Answers

  • bkfichterbkfichter
    edited August 2016

    That did it! Thanks!

    As a note, still have to wrap the method in the preprocessor directive because of the UWP "uPLibrary".

    For anyone curious, here it is in the Unity docs: https://docs.unity3d.com/Manual/windowsstore-appcallbacks.html

    #if !UNITY_EDITOR
    
        private void Client_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
        {
            UnityEngine.WSA.Application.InvokeOnAppThread(() =>
            {
                resultTextField.text = System.Text.Encoding.UTF8.GetString(e.Message);
            }, false);
        }
    #endif
    
  • Hello,

    could you please point me to the right directions on how to use MQTT with hololens/UWP? I've tried so many things so far, everything failed.

    Thank you in advance.
    Best,
    Nelson

  • bradenbraden
    edited February 2017

    Got it working fine in Hololens with the UWP DLL.

    Is there anyway to have it work in editor using the .Net DLL too ? Using the InvokeOnAppThread in editor comes back complaining "can only be called from the main thread"

Sign In or Register to comment.