How?!
All I want is a simple serial connection between my C# app and something else -- Bluetooth terminal app on Android, and eventually Arduino.
However, nothing that I've tried works.
The Android terminal app can connect to this and C# receives data, but can't send anything.
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'
}
}
}
So I tried the other way round to get Windows to connect to an Android Bluetooth terminal server app, and again Windows can't send data.
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);
}
}
}
}
}
And something really strange seems to be going on inside BluetoothClient.Client
. Calling code always sees Available == 0
when I've sent data from Android and the other Available is >0
.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/oiB5j.png
So: how to do in C# simple serial communication over Bluetooth?
Is it impossible?