Backgroundtask被SocketActivityTrigger激活,但是trigger中的socket returns null

Backgroundtask is activated by SocketActivityTrigger, but the socket in the trigger returns null

我希望在我的应用程序暂停时能够使用蓝牙通过套接字发送和接收数据。在前台应用程序中建立连接后,我会注册我的 BackgroundReader。

代码运行 建立连接后:

    private void OnConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {           
        BackgroundTaskRegistration receiveTask = RegisterBackgroundTask("X.ReceiverTask", "ReceiverTask", new SocketActivityTrigger(), null);
        BackgroundTaskRegistration sendTask = RegisterBackgroundTask("X.Z", "SenderTask", new ApplicationTrigger(), null);

        var socket = args.Socket;
        socket.EnableTransferOwnership(receiveTask.TaskId, SocketActivityConnectedStandbyAction.DoNotWake);
        socket.TransferOwnership("RfcommSocket");
    }

后台任务注册方法:

     private static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint, string name, IBackgroundTrigger trigger, IBackgroundCondition condition)
            {
                foreach(var cur in BackgroundTaskRegistration.AllTasks)
                {
                    if(cur.Value.Name == name)
                    {
                        //task is already registered
                        return (BackgroundTaskRegistration)(cur.Value);
                    }
                }

                var builder = new BackgroundTaskBuilder();
                builder.Name = name;
                if(taskEntryPoint != null && taskEntryPoint != String.Empty)
                {
                    builder.TaskEntryPoint = taskEntryPoint;
                }
                builder.SetTrigger(trigger);

                if(condition != null)
                {
                    builder.AddCondition(condition);
                }

                BackgroundTaskRegistration task = builder.Register();
                return task;
            }

当我尝试发送数据时,接收器后台任务按预期激活。只有当我尝试从其属性中读取流套接字时,它 returns null 并在我尝试访问它时抛出异常:

    public async void Run(IBackgroundTaskInstance taskInstance)
    {
        taskDeferral = taskInstance.GetDeferral();
        taskInstance.Canceled += OnTaskCanceled;
        var details = taskInstance.TriggerDetails as SocketActivityTriggerDetails;
        var socketInformation = details.SocketInformation;
        switch (details.Reason)
        {
            ...
            case SocketActivityTriggerReason.SocketActivity:
                Debug.WriteLine(socketInformation.SocketKind.ToString());
                var socket = socketInformation.StreamSocket; //This returns null
                DataReader reader = new DataReader(socket.InputStream);
                reader.InputStreamOptions = InputStreamOptions.Partial;
                await reader.LoadAsync(250);
                var dataString = reader.ReadString(reader.UnconsumedBufferLength);
                Debug.WriteLine(dataString);
                socket.TransferOwnership(socketInformation.Id); /* Important! */
                break;
            ...
        }
        taskDeferral.Complete();
    }

backgroundTaskHost.exe 退出,并且据推测会带走套接字。

提前致谢!

Backgroundtask is activated by SocketActivityTrigger, but the socket in the trigger returns null

我检查了你的代码,问题是你使用了 SocketActivityTrigger in Bluetooth RF communication. In UWP we often use RfcommConnectionTrigger to detect Bluetooth message notification. The RfcommConnectionTrigger has more features that support Bluetooth RF communication. And I have test with official code sample 并且它可以正确获取套接字实例并且可以使用 reader.

加载消息