1

我在我的研究中找到了这个例子(https://developer.nokia.com/Community/Wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth)来为 Windows phone 8 开发一个蓝牙控制台。这个例子工作得很好,除了 TERMINATE 功能。当我调用 TERMINATE 函数时,ReceiveMessages 函数仍在尝试接收数据,但没有更多可用的套接字,它会生成一个 system.exception。我尝试了很多解决方法,但我没有足够的 C# 经验,这是我的第一个 APP。任何人都知道我该如何解决这种情况或有更好的例子?

我只做了 1 处修改:

private async void AppToDevice()
        {
            if (!connected)
            {
                ConnectAppToDeviceButton.Content = "Connecting...";
                PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
                var pairedDevices = await PeerFinder.FindAllPeersAsync();

                if (pairedDevices.Count == 0)
                {
                    Debug.WriteLine("No paired devices were found.");
                }
                else
                {
                    foreach (var pairedDevice in pairedDevices)
                    {
                        if (pairedDevice.DisplayName == DeviceName.Text)
                        {
                            connectionManager.Connect(pairedDevice.HostName);
                            ConnectAppToDeviceButton.Content = "Disconnect";
                            DeviceName.IsReadOnly = true;
                            //ConnectAppToDeviceButton.IsEnabled = false;
                            continue;
                        }
                    }
                }
            }
            else
            {
                connectionManager.Terminate();
                ConnectAppToDeviceButton.Content = "Connect";
            }
        }

4

1 回答 1

0

我在这里找到了一个解决方案: WinRT: DataReader.LoadAsync Exception with StreamSocket TCP

我只做了一些修改:

public void Terminate()
    {
        try
        {
            if (socket != null)
            {
                taskLoadLength.Cancel();
                taskLoadLength.Close();
                taskLoadMessage.Cancel();
                taskLoadMessage.Close();
                socket.Dispose();
                dataReadWorker.CancelAsync();
                dataReader.Dispose();
                dataWriter.Dispose(); 
                isInicialized = false;   
            }
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

private void ReceiveMessages(object sender, DoWorkEventArgs ev)
    {
        while (true)
        {
            try
            {
                // Read first byte (length of the subsequent message, 255 or less). 
                //uint sizeFieldCount = await dataReader.LoadAsync(1);
                taskLoadLength = dataReader.LoadAsync(1);
                taskLoadLength.AsTask().Wait();
                uint sizeFieldCount = taskLoadLength.GetResults();
                if (sizeFieldCount != 1)
                {
                    // The underlying socket was closed before we were able to read the whole data. 
                    return;
                }
                // Read the message. 
                uint messageLength = dataReader.ReadByte();
                taskLoadMessage = dataReader.LoadAsync(messageLength);
                taskLoadMessage.AsTask().Wait();
                uint actualMessageLength = taskLoadMessage.GetResults();
                //uint actualMessageLength = await dataReader.LoadAsync(messageLength);
                if (messageLength != actualMessageLength)
                {
                    // The underlying socket was closed before we were able to read the whole data. 
                    return;
                }
                // Read the message and process it.
                string message = dataReader.ReadString(actualMessageLength);
                MessageReceived(message);
            }
            catch (AggregateException ae)
            {
                MessageBox.Show(ae.Message);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
于 2013-08-23T20:59:58.867 回答