在Windows Phone 8中通过蓝牙从arduino接收数据

时间:2013-08-13 06:45:20

标签: windows-phone-8

我在我的研究中找到了这个例子(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";
            }
        }

1 个答案:

答案 0 :(得分: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);
            }
        }
    }