【发布时间】: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?
【问题讨论】: