Hi,
I use the myObexListener.GetContext() fonction which it's work very fine for my need. Except for one need : when the connection is lost during a transfert nothing happen and the getcontext() never return. Is there a way to put a timeout or a connection lost behaviour or anything else to force getcontext to return if there is a loose of connection ?
(see server and client code below)
Thanks
Marc Alzieu
sender code ( on device) :
if (server != null)
{
try
{
file = null;
// define an EndPoint for the server and connect to it
// define the stream
NetworkStream service;
Log("Create new End Point");
serverEP = new BluetoothEndPoint(server.DeviceAddress, BluetoothService.ObexObjectPush);
Log("Create new Client");
blue_client = new BluetoothClient();
Log("Connect Client");
blue_client.Connect(serverEP);
Log("Blue Tooth Connected : " + blue_client.Connected);
//serverEP.
// if I am connected ...
if (blue_client.Connected)
{
// receive the stream
service = blue_client.GetStream();
// connect to OBEX server
sessionFileSending = new ObexClientSession(service, 65535);
sessionFileSending.Connect();
Log("session Connected : ");
// load the file to send
try
{
file = new FileStream(localPath + fileName, FileMode.Open, FileAccess.Read);
}
catch (FileNotFoundException e)
{
Log("Fichier Absent" + e.ToString());
return;
}
// send file to server
StartMonitoring();
Log("Sending File...");
sessionFileSending.PutFrom(file, file.Name, null);
Log("File Sent");
StopMonitoring();
fileSent = true;
// close resource
file.Close();
//sessionFileSending.Disconnect();
//sessionFileSending.Disconnect
//sessionFileSending.Dispose();
//blue_client.Dispose();
}
else
{ // if I am not connected ...
fileSent = false;
}
}
catch (ObexResponseException exc)
{
// exception caused from the lacked availability of Bluetooth server
Log("Connection lost :" + exc.ToString());
}
catch (ObjectDisposedException exc)
{
// exception caused from the closing of the client
Log("Client closed: " + exc.ToString());
}
catch (System.IO.EndOfStreamException exc)
{
Log("Communication Interrupted" + exc.ToString());
}
catch (System.Net.Sockets.SocketException exc)
{
Log("Unable to connect to server" + exc.ToString());
}
catch (Exception exc)
{
// generic exception caused from the other operation
Log("Something go wrong :" + exc.ToString());
}
finally
{
//blue_client.();
StopMonitoring();
Log("Disconnected");
if (file!=null)
file.Close();
Log("File Closed");
if (sessionFileSending != null)
{
try
{
sessionFileSending.Disconnect();
} catch(Exception){}
sessionFileSending.Dispose();
}
if (blue_client !=null)
blue_client.Dispose();
}
}
Obex listener code (on PC) :
ol = new ObexListener(ObexTransport.Bluetooth);
ol.Start();
while (ol.IsListening)
{
try
{
//Session = new ObexSessionConnection();
Log("Attente reception");
//Session.Connect();
ObexListenerContext olc = ol.GetContext();
Log("Recus");
ObexListenerRequest olr = olc.Request;
string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[ { '/' }));
WebHeaderCollection HeaderValues;
Log("Taille Recus : " + olr.InputStream.Length.ToString());
olr.WriteFile(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\" + DateTime.Now.ToString("yyMMddHHmmss") + " " + System.IO.Path.GetFileName(filename));
HeaderValues = olr.Headers;
Log(filename);
Log("Header :");
for (int i = 0; i <= HeaderValues.Count - 1; i++)
{
String header = HeaderValues.GetKey(i);
String[ values = HeaderValues.GetValues(header);
if (values.Length > 0)
{
Log("The values of " + header + " header are : ");
int j;
for (j = 0; j <= values.Length - 1; j++)
{
//byte[ test=Encoding.Unicode.GetBytes(values[j]);
Log(values[j].ToString());
}
}
else
Log("There is no value associated with the header");
}
}
catch (Exception ex)
{
break;
}
}