在.Net(BLUETOOTH)

时间:2017-10-05 05:02:59

标签: c# .net winforms bluetooth 32feet

我正在创建一个Windows窗体应用程序,它通过蓝牙连接到设备。我能够向设备发送命令,并且我正在连续接收数据。我面临的问题是我无法在文本框中显示连续数据。文本框仅显示应用程序正在接收的第一行字符。这是我的代码:

连接按钮操作:

private void btnConnect_Click(object sender, EventArgs e)
    {
        if (listBox.SelectedItem != null)
        {
            lblProgress.Text = "";

            btnStart.Enabled = true;

            cBoxAvailablePorts.Enabled = cBoxAvailableBaudRates.Enabled = true;

            try
            {
                int pos = listBox.SelectedIndex;

                deviceInfo = array.ElementAt(pos);

                if (pairDevice())
                {
                    Thread thread = new Thread(() => connectThread());
                    thread.Start();
                }
                else
                {
                    MessageBox.Show("Pair failed!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        else
        {
            MessageBox.Show("Please connect to a device!");
        }
    }

线索行动

private void connectThread()
    {
        //BluetoothClient client = new BluetoothClient();
        bc.BeginConnect(deviceInfo.DeviceAddress, serviceClass, this.connectCallBack, bc);
    }

CALLBACK ACTION:

private void connectCallBack(IAsyncResult result)
    {
        //BluetoothClient client = (BluetoothClient)result.AsyncState;

        try
        {
            if (bc.Connected)
            {
                MessageBox.Show("Connected!");
            }
            else
            {
                MessageBox.Show("Connection Failed!");
            }
        }
        catch (Exception)
        {
            MessageBox.Show("Not able to identify Bluetooth devices! Please try again.!");
        }
    }

开始按钮操作: 在这里,我发送一个命令“S”。 在按钮操作中,我调用sendMessage(“S”)。 调用的函数如下所示:

public void sendMessage(string msg)
    {
        try
        {
            if (bc.Connected)
            {
                Stream stream = bc.GetStream();

                stream.ReadTimeout = 1000;

                StreamWriter streamWriter = new StreamWriter(stream);

                streamWriter.WriteLine(msg);

                streamWriter.Flush();

                // Read operation

                StreamReader streamReader = new StreamReader(stream);

                string result = streamReader.ReadLine();

                txtResult.Text = result;
            }
            else
            {
                MessageBox.Show("Sending failed!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

我在循环中编写了StreamReader部分,它给了我Socket Exception。 我还尝试从串口获取数据并使用DataReceived事件以防万一,但它仍然没有帮助。 任何帮助,将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

OKAY!我解决了这个问题。没有遇到32feet库的麻烦(尽管32feet编码很有趣),我想通过串口进行通信。我将设备与笔记本电脑连接起来,并在我的笔记本电脑的蓝牙设置中了解了传出的COMPORT。双向通信只能通过传出的COMPORT来完成,而不能通过传入的COMPORT来完成。

假设输出的COMPORT是COM12,我设置的波特率是9600。 所以这是我的代码:

public delegate void updateDelegate(string text);

private updateDelegate objDelegate;

private SerialPort serialPort;

public View() // constructor
{
        InitializeComponent();
        this.WindowState = FormWindowState.Normal;
        this.StartPosition = FormStartPosition.CenterScreen;
        this.objDelegate = new updateDelegate(getText);

        serialPort = new SerialPort("COM12", 9600);
        serialPort.Handshake = Handshake.None;
        serialPort.Parity = Parity.None;
        serialPort.StopBits = StopBits.One;
        serialPort.DtrEnable = true;
        serialPort.RtsEnable = true;
}

开始按钮操作

private void btnStart_Click(object sender, EventArgs e)
    {
        sendData("S");
    }

//发送命令

    public void sendData(string msg)
    {
        try
        {
            if (!serialPort.IsOpen)
            {
                serialPort.Open();

                //serialPort.Close();
            }

            if (serialPort.IsOpen)
            {
                serialPort.Write(msg);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

// READ DATA

    public void readData()
    {
        try
        {
            serialPort.DataReceived += SerialPort_DataReceived;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        string res = serialPort.ReadExisting();

        Thread.Sleep(500);

        txtResult.Invoke(this.objDelegate, new object[] {res});
    }

    public void getText(string text)
    {
        txtResult.Text = text;
    }

我希望这会对某人有所帮助!谢谢!!!