Background
In the previous articleArticleThe Bluetooth file transfer between Windows Mobile and PC describes how to use obex to develop a Bluetooth file transfer application. Among them, benben789 pointed out that large files cannot be transmitted, so it is necessary to transmit large files.
Introduction
This article describes how to send large files via Bluetooth on Windows Mobile.
Implementation
The implementation of this large file sending is an example of brecham. obex.ProgramDeveloped based on the brecham. obex library. brecham. obex is implemented based on 32feet.net. You can refer to brecham. obex. This database can be used for free, but dependencies must be specified. On the other hand, I did not find this librarySource code.
The main window of the sender.
Use System. Windows. Forms. openfiledialog to pop up the window for selecting the file to be sent.
Dialogresult Result = openfiledialog1.showdialog ();
If (Result = Dialogresult . OK)
{
State State = New State ();
//------------------------------------------------------
// Get the file
//------------------------------------------------------
String Putname; // = "Dummy.txt ";
Try {
State. m_filestream = New Filestream (Openfiledialog1.filename,Filemode . Open, Fileaccess . Read );
} Catch ( Ioexception Ioex ){
MessageBox . Show ( "Failed to open the file :" + Ioex. tostring ());
Return ;
}
State. m_progressstream = New Readprogressstream (State. m_filestream );
State. m_progressstream.settotalreadlength (State. m_filestream.length );
Putname = Path . Getfilename (openfiledialog1.filename );
} // If
Assign the selected file to readprogressstream to implement the transfer progress bar function. However, in actual use, this function still does not work.
If the send file is selected, the device search window is displayed to select the receiving device. The device selection and connection dialog box is actually implemented in 32feet.net.
//------------------------------------------------------
// Get the peer
//------------------------------------------------------
Protocolfamily PF = This . Protocolcombobox1.selectedprotocol;
State. m_conn = New Brecham. obex. net. Guiobexsessionconnection (PF, False , This . Labelstatus );
// Set our receive size and restrict our send size
State. m_conn.obexbuffersize = 2028;
State. m_conn.maxsendsize = 2048;
Try {
If (! State. m_conn.connect ()){
// User canceled the connect
Return ;
}
} Catch ( Exception Ex ){
Type Typeofex = ex. GetType ();
If ( Typeof ( Obexresponseexception )! = Typeofex
&& Typeof (System. net. Protocolviolationexception )! = Typeofex
&& Typeof (System. Io. Ioexception )! = Typeofex
&& Typeof (System. net. sockets. Socketexception )! = Typeofex ){
// Not one of the expected exception types, rethrow!
Throw ;
}
String Descr = ex. Message + "\ R \ n" + Ex. GetType (). tostring ();
This . Labelstatus. Text = "Connect failed :" + Descr;
MessageBox . Show (descr, "Connect failed" );
Return ;
}
After you select a device, the sending process starts.
Stream Peerstream = state. m_conn.peerstream;
//------------------------------------------------------
// Send
//------------------------------------------------------
Try
{
Obexclientsession Sess = state. m_conn.obexclientsession;
//
This . Labelstatus. Text ="Sending ..." ;
This . Progressbar1.visible = True ;
Startprogressbarupdater (State );
// Sess. putfrom (State. m_progressstream, putname, null, state. m_filestream.length );
State. m_putcaller = New Putfromnticaller (Sess. putfrom );
Asynccallback CB = New Asynccallback (Putcompleted );
State. setstarttime ();
Iasyncresult AR = state. m_putcaller.begininvoke (
State. m_progressstream, putname, Null , State. m_filestream.length,
CB, State );
// Enable the cancel button
M_cancelled = False ;
Buttoncancel. Enabled = True ;
Buttoncancel. Tag = sess; // Give the button access to the session.
}
Catch
{
// All obex errors occur on the delegate. begininvoke's thread, and
// Thus are seen on calling endinvoke in the putcompleted method.
//
// Just ensure the streams are closed etc, and rethrow.
State. Dispose ();
Throw ;
}
PassObexclientsessionSave and send to session to cancel sending. Putfromnticaller begininvoke () sends files through a thread.
After sending, it took 10 MB of files to be sent at 3 minutes 45 seconds. I tried a 30 m file, but I don't know where to store it. I think this is the design of sending files. I do not advocate using Bluetooth to send large files. If you need Bluetooth to send large files, in this case, we need to consider whether the design scheme is reasonable. Why do we use Bluetooth to send such large files? What is the real requirement and what can be replaced. If you need to use Bluetooth to send large files, you can use brecham. obex.
The device that receives files. This device does not need to install any program. Generally, Windows Mobile has obex service running.
Save the file and put it in my documents.
Other related articles
For more information, see my previous documents on Bluetooth.
Windows Embedded source tools for Bluetooth development under. NET Compact framework
32feet. Net for Bluetooth development under. NET Compact framework
Bluetooth virtual serial port under. NET Compact framework)
Pairing Bluetooth devices under. NET Compact framework
30 days of. Net [Windows Mobile applications]-day 02: Bluetooth Manager (simple Bluetooth Application)
Development of Bluetooth broadcast programs under. NET Compact framework
Bluetooth file transfer between Windows Mobile and PC
Environment: VS 2008 + XP + Windows Mobile 6.5 + brecham. obex + 32feet.net
SourceCode: Http://files.cnblogs.com/procoder/PutGuiCs.rar