Search code examples
c#microsoft-band

Keep connection open using Microsoft Band


I'm developing an app to take pictures with your phone by pressing a button on you Band. There is a weird issue when you run the app in release mode.

When I run the app in debug mode I am able to keep a connection open to listen for Band events. But when I build in release mode, the TileButtonPressed events are no longer fired. I dont know if this is a bug or if I am connecting in a bad way. If I'm connecting in a bad way, how should I keep the connection open as long as the app is running?

The client variable is used in various functions in the class.

This is the code I'm using to connect and listen for events.

    private async void BandConnection()
    {
        btnRetry.Visibility = Visibility.Collapsed;
        grStatus.Visibility = Visibility.Visible;
        tbStatus.Text = "Looking for your Band...";
        if (!await HasBand())
        {
            btnRetry.Visibility = Visibility.Visible;
            tbStatus.Text = "No Band detected!";
            return;
        }
        tbStatus.Text = "Connecting...";
        client = await BandClientManager.Instance.ConnectAsync(getPairedDevice[0]);
        tbStatus.Text = "Connected!";
        string bandVersion = await client.GetFirmwareVersionAsync();
        string bandHWversion = await client.GetHardwareVersionAsync();

        Debug.WriteLine("Band FW version: " + bandVersion);
        Debug.WriteLine("Band HW version: " + bandHWversion);
        current = Window.Current.Dispatcher;
        await client.TileManager.StartReadingsAsync();

        client.TileManager.TileButtonPressed += async (sender, e) =>
        {
            var buttonId = e.TileEvent.ElementId;
            var pageId = e.TileEvent.PageId;
            var tileId = e.TileEvent.TileId;
            Debug.WriteLine(buttonId + " - " + pageId + " - " + tileId);
            if (buttonId == 1)
            {
                // Take picture
                await current.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {
                    await TakePhotoAsync();

                });
            }
            if (buttonId == 2)
            {
                // Record video
                await current.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {
                    if (!_isRecording)
                    {
                        await StartRecordingAsync();
                    }
                    else
                    {
                        await StopRecordingAsync();
                    }

                    // After starting or stopping video recording, update the UI to reflect the MediaCapture state
                    UpdateCaptureControls();

                });
            }
            if (buttonId == 3)
            {
                // Toggle flash
                await current.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {
                    ToggleFlash();
                });
            }

            if (buttonId == 4)
            {
                await current.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {
                    SwitchCamera();
                });
            }
        };
        grStatus.Visibility = Visibility.Collapsed;

    }

Solution

  • Okay, I contacted the Microsoft Band team about this issue, this was their response:

    This is a known issue with the UWP apps built with the .net native compilation >option (default for release builds).

    We have a fix that we will be pushing out soon. Until then plz continue to build >debug.

    Regards

    Ali

    Edit: the NuGet package has since been updated as promised and working fine now