Thanks Alanjmcf.
What I want to do is to keep listening until I receive a String. Once received, the program read it, sends a file to the String´s sender and starts listening again. The problem is that it works very bad, it only receive the string sometimes. Most of the times it stays listening but doesn´t get anything. I do not know if it is because I don´t close the session, the stream, the client or what...
My code is as follow:
private void backgroundWorker8_DoWork(object sender, DoWorkEventArgs e)
{
int bytesRead=0;
System.IO.Stream stream1=null;
byte [ Buffer=new byte [255];
BluetoothListener btListener = new BluetoothListener(BluetoothService.SerialPort);
btListener.Start();
String str;
String nombreDevice;
ObexClientSession session;
BluetoothClient client2 = new BluetoothClient();
while (true)
{
try
{
btListener.Pending();
BluetoothClient client = btListener.AcceptBluetoothClient();
stream1 = client.GetStream();
bytesRead = stream1.Read(Buffer, 0, 255);
str = System.Text.Encoding.ASCII.GetString(Buffer, 0, bytesRead);
nombreDevice = client.RemoteMachineName.ToString();
client.Close();
stream1.Close();
for (int ii = 0; ii < devices.Length; ii++)
{
if (String.Compare(nombreDevice, devices[ii].DeviceName) == 0 || String.Compare(nombreDevice, devices[ii].DeviceAddress.ToString()) == 0)
{
BluetoothEndPoint remoteEndPoint = new BluetoothEndPoint(devices[ii].DeviceAddress, BluetoothService.ObexObjectPush);
client2.Connect(remoteEndPoint);
session = new ObexClientSession(client2.GetStream(), UInt16.MaxValue);
session.Connect();
switch (str)
{
case "@001#":
session.PutFile(SPAMMovil);
break;
case "@002#":
session.PutFile(SPAMPC);
break;
case "@004#":
session.PutFile(SPAMSmartphone);
break;
case "@000#":
session.PutFile(SPAMPDA);
break;
case "@003#":
session.PutFile(SPAMPDA);
break;
}
session.Disconnect();
}
}
}
catch (Exception err)
{
client2.Close();
}
}
}
Any help is welcomed! Thankss you!!
Fernando.