I would like to send a message directly after a device has connected to me.
So I start listening to incoming connections:
public static void StartListen(BluetoothDeviceInfo foundDevice)
{
try
{
_listener = new BluetoothListener(_serviceClass);
_listener.Start();
_listener.BeginAcceptBluetoothClient(AcceptBluetoothClientCallback, _listener);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
Then I accept the the connection in an async way:
private static void AcceptBluetoothClientCallback(IAsyncResult ar)
{
_client = _listener.AcceptBluetoothClient();
var stream = _client.GetStream();
var data = "hello";
stream.Write(Encoding.ASCII.GetBytes(data), 0, data.Length);
Console.WriteLine($"canRead: {stream.CanRead}");
Console.WriteLine($"canWrite: {stream.CanWrite}");
_client.Close();
}
But AcceptBluetoothClient
is a blocking call. So I don't get the client until the other part sends something. Is there a way to get the client/stream before this event?