1

我正在开发一个 Windows 通用应用程序。我想从蓝牙设备获取数据到 Windows Phone。为此,我正在使用 RFCommCommunicationTrigger 的概念。

这是我正在使用的代码片段

   var rfTrigger = new RfcommConnectionTrigger();
            // Specify what the service ID is
            rfTrigger.InboundConnection.LocalServiceId = RfcommServiceId.FromUuid(new Guid("<some_base_guid>"));


            //Register RFComm trigger
            var rfReg = RegisterTaskOnce(
                "HWRFCommTrigger",
                "BackgroundLibrary.RFBackgroundTask",
                rfTrigger, null
                );
            SetCompletedOnce(rfReg, OnTaskCompleted);

这里是RegisterTaskOnce的函数

       static private IBackgroundTaskRegistration RegisterTaskOnce(string taskName, string    entryPoint, IBackgroundTrigger trigger, params IBackgroundCondition[] conditions)
    {

        // Validate
        if (string.IsNullOrEmpty(taskName)) throw new ArgumentException("taskName");
        if (string.IsNullOrEmpty(entryPoint)) throw new ArgumentException("entryPoint");
        if (trigger == null) throw new ArgumentNullException("trigger");

        // Look to see if the name is already registered
        var existingReg = (from reg in BackgroundTaskRegistration.AllTasks
                           where reg.Value.Name == taskName
                           select reg.Value).FirstOrDefault();
        Debug.WriteLine("Background task "+ taskName+" is already running in the Background");
        // If already registered, just return the existing registration
        if (existingReg != null)
        {
            return existingReg;
        }

        // Create the builder
        var builder = new BackgroundTaskBuilder();
        builder.TaskEntryPoint = entryPoint;
        builder.Name = taskName;
        builder.SetTrigger(trigger);

        // Conditions?
        if (conditions != null)
        {
            foreach (var condition in conditions)
            {
                builder.AddCondition(condition);
            }
        }


        // Register
        return builder.Register();
    }

这是 SetCompletedOnce 的代码,它只会添加一次处理程序

     static private void SetCompletedOnce(IBackgroundTaskRegistration reg, BackgroundTaskCompletedEventHandler handler)
    {
        // Validate
        if (reg == null) throw new ArgumentNullException("reg");
        if (handler == null) throw new ArgumentNullException("handler");

        // Unsubscribe in case already subscribed
        reg.Completed -= handler;

        // Subscribe
        reg.Completed += handler;
    }

我还编写了 BackgroundLibrary.RFBackgroundTask.cs

   public sealed class RFBackgroundTask : IBackgroundTask
   {
   public async void Run(IBackgroundTaskInstance taskInstance)
    {
    BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

        try
        {
         Debug.WriteLine(taskInstance.TriggerDetails.GetType());
        taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
            Debug.WriteLine("RFComm Task Running");
            Debug.WriteLine(taskInstance.TriggerDetails.GetType().ToString());

        }
        catch (System.Exception e)
        {
            Debug.WriteLine("RFComm Task Error: {0}", e.Message);
        }
        deferral.Complete();

    }
    }

每次设备尝试打开连接时都会调用 Run 方法。获取到的Trigger的类型(我在RFBackgroundTask.cs的run方法中调试的类型)打印为

Windows.Devices.Bluetooth.Background.RfcommConnectionTriggerDetails

但我无法使用它,因为我在 BackgroundLibrary 项目中没有这个类。文档说这提供了有关导致此触发器触发的蓝牙设备的信息。它具有 Socket、RemoteDevice 等变量。

我想我错过了一些非常简单的东西你能帮帮我吗?

4

1 回答 1

1

启动后台任务后,只需将 TriggerDetails 对象转换为 RfcommConnectionTriggerDetails 对象:

public sealed class RFBackgroundTask : IBackgroundTask
{
    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        BackgroundTaskDeferral deferral = taskInstance.GetDeferral();

         try
         { 
             taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
             RfcommConnectionTriggerDetails details = (RfcommConnectionTriggerDetails)taskInstance.TriggerDetails;
             StreamSocket = details.Socket; // Rfcomm Socket
             // Access other properties...
         }
         catch (System.Exception e)
         {
             Debug.WriteLine("RFComm Task Error: {0}", e.Message);
         }
         deferral.Complete();

     }
}
于 2015-04-03T20:49:34.350 回答