Reputation: 8246
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.
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:
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?
Upvotes: 9
Views: 772
Reputation: 8246
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
.
Upvotes: 0
Reputation: 179
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.
Upvotes: 1