无法通过蓝牙从Windows运行时组件连接到Microsoft频段

Uts*_*awn 9 .net c# bluetooth windows-phone-8.1 microsoft-band

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

我从后台任务发送通知,即从Runtime组件项目发送通知.但是我收到了一个错误.错误如下:

错误: System.TypeInitializationException:'Microsoft.Band.Store.StoreResources'的类型初始值设定项引发异常.---> System.Exception:在Microsoft.Band.Store.StoreResources..cctor()的Windows.UI.Xaml.Application.get_Current()中发生灾难性故障(来自HRESULT的异常:0x8000FFFF(E_UNEXPECTED))---结束内部异常堆栈跟踪---在Microsoft.Band.Store.Bore.BluetoothTransport的Microsoft.Band.Store.BluetoothTransport.GetTransport(RfcommDeviceService服务,ILoggerProvider loggerProvider,UInt16 maxConnectAttempts)的Microsoft.Band.Store.StoreResources.get_RfComm_FromId_ReturnedNull()处. System_Threading.Tasks.Task.Execute()中的System.Threading.Tasks.Task`1.InnerInvoke()处于c__DisplayClass1.b__0()

正如本问题的答案所述,前台应用程序在后台应用程序尝试连接时不应尝试连接到乐队.

  • 我的前台应用程序不是尝试连接也没有与乐队有任何联系.

我认为错误是连接到蓝牙的问题,因为我已经调试并找出了错误的位置:

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");
Run Code Online (Sandbox Code Playgroud)

我的乐队的蓝牙与微软健康应用程序连接得很好,所以我想我的手机和乐队的蓝牙没有任何问题.

我的前台应用程序的Package.appmanifest如下:

前景app Package.appmanifest

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

<?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>
Run Code Online (Sandbox Code Playgroud)

那可能是什么问题呢?你能为这个问题提供解决方案或解决方案吗?

Jen*_*ens 1

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

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