C# 无法通过蓝牙从Windows运行时组件连接到Microsoft band

C# 无法通过蓝牙从Windows运行时组件连接到Microsoft band,c#,.net,bluetooth,windows-phone-8.1,microsoft-band,C#,.net,Bluetooth,Windows Phone 8.1,Microsoft Band,我正在开发一个Windows phone 8.1应用程序,它与Microsoft Band连接以发送一些通知。我需要执行一些后台任务,因此我添加了一个Windows运行时组件项目 我正在从后台任务(即运行时组件项目)发送通知。但是我犯了一个错误。错误如下: 错误:System.TypeInitializationException:“Microsoft.Band.Store.StoreResources”的类型初始值设定项引发异常。-->系统异常:灾难性故障(来自HRESULT的异常:0x800

我正在开发一个Windows phone 8.1应用程序,它与Microsoft Band连接以发送一些通知。我需要执行一些后台任务,因此我添加了一个Windows运行时组件项目

我正在从后台任务(即运行时组件项目)发送通知。但是我犯了一个错误。错误如下:

错误:System.TypeInitializationException:“Microsoft.Band.Store.StoreResources”的类型初始值设定项引发异常。-->系统异常:灾难性故障(来自HRESULT的异常:0x8000FFFF(E_意外)) 在Windows.UI.Xaml.Application.get_Current()中 在Microsoft.Band.Store.StoreResources..cctor()上 ---内部异常堆栈跟踪的结束--- 在Microsoft.Band.Store.StoreResources.get\u RfComm\u FromId\u ReturnedNull()中 在Microsoft.Band.Store.BluetoothTransport.GetTransport(RfcommDeviceService服务、ILoggerProvider loggerProvider、UInt16 maxConnectAttempts) 在Microsoft.Band.Store.BluetoothTransport.c_udisplayClass1.b_u0()上 在System.Threading.Tasks.Task`1.InnerInvoke()中 在System.Threading.Tasks.Task.Execute()中

正如本文中的回答所述,前台应用程序不应在后台应用程序尝试连接时尝试连接到乐队

  • 我的前台应用程序未尝试连接,也没有任何连接 与乐队的联系
我认为错误是因为连接蓝牙时出现问题,因为我已经调试并找到了错误的位置:

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");
public异步无效运行(IBackgroundTaskInstance taskInstance)
{
var deleral=taskInstance.getDeleral();
尝试
{
Debug.WriteLine(“任务触发”+DateTime.Now);
taskInstance.Cancelled+=(s,e)=>{};
taskInstance.Progress=0;
//获取与手机配对的Microsoft频段列表。
var pairedBands=await-BandClientManager.Instance.GetBandsAsync();
if(成对条带长度<1)
{
Debug.WriteLine(
“此示例应用程序需要与您的设备配对的Microsoft Band。还要确保您的Band上安装了最新的固件,如最新的Microsoft Health应用程序所提供的。”);
返回;
}
//这是我得到的错误
使用(var-bandClient=await-BandClientManager.Instance.ConnectAsync(pairedBands[0]))
{
Debug.WriteLine(“磁贴创建已开始”);
我的乐队的蓝牙与微软健康应用程序连接良好,所以我想我的手机和乐队的蓝牙没有问题

前台应用程序的MyPackage.appmanifest如下所示:

用于后台任务(Windows运行时组件项目)的Package.appmanifest



那么,可能的问题是什么呢?您能否提供解决方案或解决此问题的方法?

您是否在Package.appxmanifest文件中设置了正确的功能和声明


至少,您需要在功能(使用蓝牙)中勾选“接近”,并在声明中指定后台任务的类型和入口点。

经过多次尝试,我终于找到了解决方案。虽然我无法找到错误的实际原因,但我得到了一个可行的解决方案

代码现在如下所示:

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;
        }
    }
前台和后台任务的包清单与问题中的相同


唯一的区别是Microsoft.Band软件包版本:
1.3.10417.1
,我从Microsoft提供的工作示例中获取。以前我使用的版本是
1.3.10702.1

我用这些功能更新了我的答案,请检查那里。我已经将任务类型指定为计时器,并添加了声明中有一个入口点。仅供参考,我的任务将被触发,唯一的问题是与蓝牙建立连接。您使用的是哪个版本的NuGet软件包?在以前的版本中存在与加载资源字符串相关的已知问题,这些问题(希望)在最新版本(1.3.10702)中得到解决@PhilHoff--MSFT我正在使用这个版本(1.3.10702.1)。
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;
        }
    }