【问题标题】:C# bluetooth serial terminal with InTheHand带有 InTheHand 的 C# 蓝牙串口终端
【发布时间】:2020-06-20 19:37:13
【问题描述】:

怎么样?!

我想要的只是在我的 C# 应用程序和其他东西之间建立一个简单的串行连接——Android 上的蓝牙终端应用程序,最后是 Arduino。

但是,我尝试过的任何方法都不起作用。

Android 终端应用可以连接到这个,C# 接收数据,但不能发送任何东西。

    public partial class Bluetooth : Form
    {
        BluetoothListener _Listener;

        public Bluetooth()
        {
            InitializeComponent();

            _Listener = new BluetoothListener(BluetoothService.SerialPort);
            _Listener.Start();
            Thread t = new Thread(new ThreadStart(Listen));
            t.Start();
        }

        private void Listen()
        {
            while(true)
            {
                if(_Listener.Pending())
                {
                    BluetoothClient c = _Listener.AcceptBluetoothClient();
                    ListenProcessor p = new ListenProcessor(c);
                }
            }
        }

        class ListenProcessor
        {
            private BluetoothClient _Client;
            public ListenProcessor(BluetoothClient c) { 
                _Client = c;
                Thread t = new Thread(new ThreadStart(Do));
                t.Start();
            }

            private void Do()
            {
                Stream s = _Client.GetStream();
                while (true)
                {
                    if (s.CanRead)
                    {
                        int a = int.Parse(s.Length.ToString());
                        byte[] buffer = new byte[a];
                        s.Read(buffer, 0, a);
                        string msg = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                        //if (InvokeRequired)
                        //{
                        //    BeginInvoke((MethodInvoker)delegate { textBoxReceived.AppendText(msg); });
                        //}
                        //else
                        //{
                        //    textBoxReceived.AppendText(msg);
                        //}
                        _Client.Client.Send(buffer);
                    }
                }
            }
        }


        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                string msg = textBoxInput.Text.Trim();
                _Client.Client.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(msg));
                textBoxInput.Text = string.Empty;
                textBoxReceived.AppendText("> " + msg);
                _Client.Client.Send(buffer); // System.Net.Sockets.SocketException: 'A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied'
            }
        }
    }

所以我尝试反过来让 Windows 连接到 Android 蓝牙终端服务器应用程序,但 Windows 再次无法发送数据。

    public partial class Bluetooth : Form
    {
        private BluetoothClient _Client;

        public Bluetooth()
        {
            InitializeComponent();
            comboBoxDevices.Enabled = false;
            buttonConnect.Enabled = false;
            textBoxReceived.Enabled = false;
            textBoxInput.Enabled = false;
            _Client = new BluetoothClient();

            BluetoothDeviceInfo di = _Client.PairedDevices.FirstOrDefault(z => z.DeviceName == "RWB");
            
            di.SetServiceState(BluetoothService.SerialPort, true);
            _Client.Connect(di.DeviceAddress, BluetoothService.SerialPort);
            if (!di.Connected)
            {
                MessageBox.Show("Connecting failed.", "Connecting failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            Thread t = new Thread(new ThreadStart(HandleConnection));
            t.Start();
        }


        private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                string msg = textBoxInput.Text.Trim();
                _Client.Client.Send(System.Text.ASCIIEncoding.ASCII.GetBytes(msg)); // System.Net.Sockets.SocketException: 'A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied'
                textBoxInput.Text = string.Empty;
                textBoxReceived.AppendText("> " + msg);
            }
        }

        private void HandleConnection()
        {
            Stream s = _Client.GetStream();
            while (true)
            {
                if (!_Client.Connected) { break; }

                if (s.CanRead)
                {
                    int a = int.Parse(s.Length.ToString());
                    byte[] buffer = new byte[a];
                    s.Read(buffer, 0, a);
                    string msg = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                    if(InvokeRequired)
                    {
                        BeginInvoke((MethodInvoker)delegate { textBoxReceived.AppendText(msg); });
                    }
                    else
                    {
                        textBoxReceived.AppendText(msg);
                    }
                }
            }
        }
    }

BluetoothClient.Client 内部似乎正在发生一些非常奇怪的事情。当我从 Android 发送数据并且另一个 Available>0 时,调用代码总是看到 Available == 0

[![在此处输入图片描述][1]][1]



  [1]: https://i.stack.imgur.com/oiB5j.png

So: how to do in C# simple serial communication over Bluetooth?
Is it impossible?

【问题讨论】:

    标签: c# 32feet


    【解决方案1】:

    经过 大量 的反复试验,这似乎是可行的。它演示了 C# 既是客户端又是服务器。

    using InTheHand.Net.Bluetooth;
    using InTheHand.Net.Sockets;
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace Can
    {
        public partial class Bluetooth : Form
        {
            BluetoothListener _Listener;
            private BluetoothClient _ConnectedClient;
            private bool _Go;
            private Thread _ListenForConnection;
            private Thread _ListenToConnected;
    
            public Bluetooth()
            {
                InitializeComponent();
                _Go = true;
                comboBoxDevices.Enabled = false;
                buttonConnect.Enabled = false;
                textBoxReceived.Enabled = false;
                textBoxInput.Enabled = false;
                BluetoothClient c = new BluetoothClient();
    
                foreach (BluetoothDeviceInfo di in c.PairedDevices)
                {
                    comboBoxDevices.Items.Add(di.DeviceName ?? di.DeviceAddress.ToString());
                }
                comboBoxDevices.Enabled = true;
                buttonConnect.Enabled = comboBoxDevices.Items.Count > 0;
    
                _Listener = new BluetoothListener(BluetoothService.SerialPort);
                _Listener.Start();
                _ListenForConnection = new Thread(new ThreadStart(ListenForConnection));
                _ListenForConnection.Start();
            }
    
            private void AppendText(string text)
            {
                BeginInvoke((MethodInvoker)delegate
                {
                    textBoxReceived.AppendText(text + Environment.NewLine);
                });
            }
    
            /// <summary>
            /// Server mode: listen for devices that want to connect.
            /// </summary>
            private void ListenForConnection()
            {
                while (_Go)
                {
                    if (_Listener.Pending())
                    {
                        if (_ConnectedClient != null)
                        {
                            BluetoothClient c = _Listener.AcceptBluetoothClient();
                            c.GetStream().Write(ASCIIEncoding.ASCII.GetBytes("Go away."), 0, 8);
                            c.Close();
                            c.Dispose();
                            AppendText($"Refused connection from {c.RemoteMachineName} because already connected to {_ConnectedClient.RemoteMachineName}.");
                            continue;
                        }
    
                        _ConnectedClient = _Listener.AcceptBluetoothClient();
                        BluetoothDeviceInfo di = _ConnectedClient.PairedDevices.FirstOrDefault(z => z.DeviceName == _ConnectedClient.RemoteMachineName);
    
                        if(di == null)
                        {
                            AppendText($"Refused connection from unpaired device {_ConnectedClient.RemoteMachineName}.");
                            _ConnectedClient.Dispose();
                            _ConnectedClient = null;
                            continue;
                        }
    
                        AppendText("Connection incoming from " + di.DeviceName + "...");
    
                        _ListenToConnected = new Thread(new ThreadStart(ListenToConnected));
                        _ListenToConnected.Start();
    
                        AppendText("Connected.");
                        BeginInvoke((MethodInvoker)delegate
                        {
                            textBoxReceived.Enabled = true;
                            textBoxInput.Enabled = true;
                        });
                    }
                }
            }
    
            /// <summary>
            /// Client mode: conect to a paired device.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void buttonConnect_Click(object sender, EventArgs e)
            {
                if(_ConnectedClient != null)
                {
                    _ConnectedClient.GetStream().Write(ASCIIEncoding.ASCII.GetBytes("Go away."), 0, 8);
                    _ConnectedClient.Close();
                    _ConnectedClient.Dispose();
                    _ConnectedClient = null;
                }
    
                _ConnectedClient = new BluetoothClient();
                BluetoothDeviceInfo di = _ConnectedClient.PairedDevices.FirstOrDefault(z => z.DeviceName == comboBoxDevices.SelectedItem.ToString());
                if (di == null)
                {
                    AppendText($"No such device: {comboBoxDevices.SelectedItem.ToString()}.");
                    return;
                }
    
                di.SetServiceState(BluetoothService.SerialPort, true);
                _ConnectedClient.Connect(di.DeviceAddress, BluetoothService.SerialPort);
                if (!di.Connected)
                {
                    AppendText("Connecting failed.");
                }
                AppendText($"Connected to {di.DeviceName} @ {di.DeviceAddress}.");
                textBoxReceived.Enabled = true;
                textBoxInput.Enabled = true;
    
                Thread t = new Thread(new ThreadStart(ListenToConnected));
                t.Start();
            }
    
            private void ListenToConnected()
            {
                Stream s = _ConnectedClient.GetStream();
                while (_Go)
                {
                    if (!_ConnectedClient.Connected) { break; }
    
                    if (s.CanRead && s.Length > 0)
                    {
                        int a = int.Parse(s.Length.ToString());
                        byte[] buffer = new byte[a];
                        s.Read(buffer, 0, a);
                        string msg = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                        AppendText("> " + msg);
                    }
                }
                _ConnectedClient.Close();
                _ConnectedClient.Dispose();
            }
    
            private void textBoxInput_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Return)
                {
                    string msg = textBoxInput.Text.Trim() + Environment.NewLine;
                    byte[] buffer = ASCIIEncoding.ASCII.GetBytes(msg);
                    try
                    {
                        _ConnectedClient.GetStream().Write(buffer, 0, buffer.Length);
                        textBoxInput.Text = string.Empty;
                        textBoxReceived.AppendText("< " + msg);
                    }
                    catch (Exception ex)
                    {
                        AppendText(ex.Message);
                    }
                }
            }
    
            private void Bluetooth_FormClosing(object sender, FormClosingEventArgs e)
            {
                textBoxInput.Enabled = false;
                textBoxReceived.Enabled = false;
                _Go = false;
                _ListenForConnection.Join();
                _ListenToConnected.Join();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-16
      • 2016-01-28
      • 1970-01-01
      • 2018-11-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2017-02-09
      相关资源
      最近更新 更多