Hello everyone.

We have decided to phase out the Mixed Reality Forums over the next few months in favor of other ways to connect with us.

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.

The plan between now and the beginning of May is to clean up old, unanswered questions that are no longer relevant. The forums will remain open and usable.

On May 1st we will be locking the forums to new posts and replies. They will remain available for another three months for the purposes of searching them, and then they will be closed altogether on August 1st.

So, where does that leave our awesome community to ask questions? Well, there are a few places we want to engage with you. 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. 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.