I am developing an application that has two components. One is an Android App made in MonoAndroid with C#, and the other is a Windows app, made in WPF with C#.
The Android app, should send a pack of 3 bytes with around 250 packs per second. The Windows app should receive all the packs and process them.
The way I communicate between them is as follows:
On Android to connect I do:
BluetoothDevice pcBluetoothDevice= ... device discovered with BluetoothManager... ;
UUID securedUUID=... my secured uuid ...;
var socket = pcBluetoothDevice.CreateInsecureRfcommSocketToServiceRecord(securedUUID);
socket.Connect();
var outputStream=socket.OutputStream;
To send from Android I do:
byte[] dataToSend = new byte[3] { 1, 0, 0 };
outputStream.Write(dataToSend, 0, dataToSend.Length);
outputStream.Flush();
On Windows, to connect I use 32feet.NET Nuget, as follows:
var serviceClass = BluetoothService.SdpProtocol;
BluetoothListenerlistener = new BluetoothListener(serviceClass);
listener.Start();
BluetoothClient client = _listener.AcceptBluetoothClient();
NetworkStream peerStream = client.GetStream();
// On A different thread:
while(connected)
{
byte[] dataBuffer = new byte[3];
var readSize = _connectionStream.Read(dataBuffer, 0, dataBuffer.Length);
// put dataBuffer in a queue on a different thread for processing.
}
This works, most of the time. The only issue is that after a few seconds of data sending, I get a weird latency issue from time to time. I am not sure if this is caused by the Bluetooth receiver (USB received on PC), or the phone Bluetooth, or the way I am communicating via Bluetooth RFCOMM.
The amount of data I send is low. I barely send 1 Kbyte/s. But as I said, the frequency is high (100-250 packs per second).
Should I be using the Bluetooth connection in some other way? Any suggestion is very much appreciated.
Have a nice day.