9
votes

I am developing a Windows phone 8.1 app which connects with a Microsoft Band to send some notification. I need to perform some background tasks, so I have added a Windows Runtime Component project.

I am sending the notification from the background task i.e. from the Runtime component project. But I am getting an error. The error is as follows:

Error: System.TypeInitializationException: The type initializer for 'Microsoft.Band.Store.StoreResources' threw an exception. ---> System.Exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) at Windows.UI.Xaml.Application.get_Current() at Microsoft.Band.Store.StoreResources..cctor() --- End of inner exception stack trace --- at Microsoft.Band.Store.StoreResources.get_RfComm_FromId_ReturnedNull() at Microsoft.Band.Store.BluetoothTransport.GetTransport(RfcommDeviceService service, ILoggerProvider loggerProvider, UInt16 maxConnectAttempts) at Microsoft.Band.Store.BluetoothTransport.<>c__DisplayClass1.b__0() at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()

As said in an answer in this Question that the foreground app should not try to connect to the band while the background app is trying to connect.

  • My foreground app is not trying to connect nor does have any connection with the band.

I think the error is for problems in connecting to Bluetooth, because I have debugged and found out the location of the error:

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        var deferral = taskInstance.GetDeferral();

        try
        {
            Debug.WriteLine("Task Triggered " + DateTime.Now);
            taskInstance.Canceled += (s, e) => { };
            taskInstance.Progress = 0;

            // 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;
            }

            // This is the line I am getting the error
            using (var bandClient = await BandClientManager.Instance.ConnectAsync(pairedBands[0]))
            {
                Debug.WriteLine("Tile creation started");

My band's Bluetooth is connecting fine with Microsoft Health app, so I suppose there is nothing wrong with Bluetooth of my phone and band.

My Package.appmanifest for Foreground app is as follows:

Foreground app Package.appmanifest

Package.appmanifest for Background Task ( Windows Runtime Component project):

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest">
<Capabilities>
<DeviceCapability Name="bluetooth.rfcomm" xmlns="http://schemas.microsoft.com/appx/2013/manifest">
<Device Id="any">
    <!-- Used by the Microsoft Band SDK -->
    <Function Type="serviceId:A502CA9A-2BA5-413C-A4E0-13804E47B38F" />
    <!-- Used by the Microsoft Band SDK -->
    <Function Type="serviceId:C742E1A2-6320-5ABC-9643-D206C677E580" />
  </Device>
</DeviceCapability>

So what can be the possible issue? Can you provide a solution or a work-around to this problem?

2
What version of the NuGet package are you using? There were known issues related to loading resource strings in prior versions that (hopefully) are resolved with the latest release (1.3.10702).Phil Hoff -- MSFT
@PhilHoff--MSFT I am using the release (1.3.10702.1).Utsav Dawn

2 Answers

1
votes

Have you set the right capabilities and declarations in the Package.appxmanifest file?

As a minimum you need to check off "Proximity" in capabilities (to use Bluetooth) and specify a type and entry point for the background task in declarations.

0
votes

Well after lot of trying I finally found out a solution. Though I couldn't find out the actual cause of the error, I got a working solution.

The code now looks like this:

public async void Run(IBackgroundTaskInstance taskInstance)
    {
        Debug.WriteLine("Task triggered");

        var deferral = taskInstance.GetDeferral();
        var bandInfo = (await BandClientManager.Instance.GetBandsAsync()).FirstOrDefault();
        IBandClient bandClient = null;
        if (bandInfo != null)
        {
            Debug.WriteLine("Band found");
            try
            {
                bandClient = await BandClientManager.Instance.ConnectAsync(bandInfo);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception: " + ex);
            }

            if (bandClient != null)
            {
                try
                {
                    Debug.WriteLine("Band connected: " + bandClient.GetFirmwareVersionAsync().Result);
                    var bandContactState = await bandClient.SensorManager.Contact.GetCurrentStateAsync();
                    Debug.WriteLine(bandContactState.State == BandContactState.NotWorn
                        ? "Band not worn"
                        : "Band worn");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception 1: "+ ex);
                }
            }
        }
        if (bandClient != null)
        {
            deferral.Complete();
            bandClient.Dispose();
            bandClient = null;
        }
    }

The package manifests for both the foreground and background tasks are same as in the question.

The only difference is Microsoft.Band package version: 1.3.10417.1, which I have taken from the working samples provided my Microsoft. Previously I had used version 1.3.10702.1.