2
votes

I want to add a custom tile to the Microsoft Band through Microsoft Band SDK in a UWP app for Windows Phone. Here is my sample code.

private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        try
        {
            // Get the list of Microsoft Bands paired to the phone.
            var pairedBands = await BandClientManager.Instance.GetBandsAsync();
            if (pairedBands.Length < 1)
            {
                Debug.WriteLine("This sample app requires a Microsoft Band paired to your device.Also make sure that you have the latest firmware installed on your Band, as provided by the latest Microsoft Health app.");
                return;
            }

            // Connect to Microsoft Band.
            using (var bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
            {
                // Create a Tile with a TextButton on it.
                var myTileId = new Guid("12408A60-13EB-46C2-9D24-F14BF6A033C6");
                var myTile = new BandTile(myTileId)
                {
                    Name = "My Tile",
                    TileIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconLarge.png"),
                    SmallIcon = await LoadIcon("ms-appx:///Assets/SampleTileIconSmall.png")
                };

                // Remove the Tile from the Band, if present. An application won't need to do this everytime it runs. 
                // But in case you modify this sample code and run it again, let's make sure to start fresh.
                await bandClient.TileManager.RemoveTileAsync(myTileId);

                // Create the Tile on the Band.
                await bandClient.TileManager.AddTileAsync(myTile);

                // Subscribe to Tile events.
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
    }

    private async Task<BandIcon> LoadIcon(string uri)
    {
        StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri));

        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
        {
            WriteableBitmap bitmap = new WriteableBitmap(1, 1);
            await bitmap.SetSourceAsync(fileStream);
            return bitmap.ToBandIcon();
        }
    }

If I run this code nothing happend. The app connected to Microsoft Band, but is not able to add a tile. The method AddTileAsync(myTile); Returns false and doesn't add a tile to the Microsoft Band.

If I try this code in a Windows Phone 8.1 app it works, but not in the UWP app.

Any ideas?

Update Here is the sample app as download. Maybe this can help.

2
Are you sure you have not already filled your band with the maximum of tiles? By the way, which band are you testing with?Sven Borden
As @SvenBorden has said, you might want to check that you have the capacity to add the band tile.James Croft
Hello Sven and James, I've also checked that I have capacity on the Band. Currently I have 5 empty Slot. I developed against Microsoft Band 2.Thomas Sebastian Jensen
Is the button associated with this click event handler within a SplitView control (which is a control new to UWP)?Phil Hoff -- MSFT
Hello Phil, in this sample it is not within a SplitView, but I also tried it sometimes with a button which is located in a SplitView Control. Are there any known issues.Thomas Sebastian Jensen

2 Answers

0
votes

maybe this would help, coming from the documentation of MS Band

using Microsoft.Band.Tiles;
...
try
{
    IEnumerable<BandTile> tiles = await bandClient.TileManager.GetTilesAsync();
}
catch (BandException ex)
{
    //handle exception 
}
//determine if there is space for tile
try
{
    int tileCapacity = await bandClient.TileManager.GetRemainingTileCapacityAsync();
}
catch (BandException ex)
{
    //handle ex
}
//create tile
WriteAbleBitmap smallIconBit = new WriteAbleBitmap(24, 24);
BandIcon smallIcon = smallIconBit.ToBandIcon();
WriteAbleBitmap largeIconBit = new WriteAbleBitmap(48, 48);//46, 46 for MS band 1
BandIcon largeIcon = largeIconBit.ToBandIcon();
Guid guid = Guid.NewGuid();
BandTile tile = new BandTile(guid)
{
    //enable Badging
    IsBadgingEnabled = true,
    Name = "MYNAME"
    SmallIcon = smallIcon;
    TileIcon = largeIcon;
};
try
{
    if(await bandClient.TileManager.AddTileAsync(tile))
    {
         ///console print something
    }
}
catch(BandException ex)
{
    //blabla handle
}
0
votes

I think the issue may be you're setting the writeable bitmap size to (1,1)?

I have this method working:

public static class BandIconUtil
{
    public static async Task<BandIcon> FromAssetAsync(string iconFileName, int size = 24)
    {
        string uri = "ms-appx:///" + iconFileName;
        StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(uri, UriKind.RelativeOrAbsolute));

        using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.Read))
        {
            WriteableBitmap bitmap = new WriteableBitmap(size, size);
            await bitmap.SetSourceAsync(fileStream);
            return bitmap.ToBandIcon();
        }

    }
}