ArticleDirectory
- Initialization
- Start the service
- Process requests
- Stop Service
Background
I have also written some articles on Bluetooth development under Windows Mobile and wince as follows.
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
During this period, two students asked me how to implement Bluetooth file transmission.CodeAnd record it.
Introduction
This article describes how to transfer Bluetooth files between Windows Mobile and PCs. By using the 32feet.net library to encapsulate OBEX, the push file is implemented.Program. The PC program of obex push can transmit files to all devices that support OBEX, including non-Windows Mobile devices.
Obex
Bluetooth file transmission can be implemented using obex. Obex (the Object Exchange Protocol) is widely used for file transfer between devices in a personal wireless network. Basically, all mobile devices support this protocol. Obex is implemented, not only file transmission between window mobile and PC is realized, but also file transmission between all devices that support OBEX protocol. For OBEX, refer to Object Exchange Protocol.
Push files to PC on Windows Mobile
This section describes how to push files from Windows Mobile to a PC. In fact, there is little difference in how to push files from PCs to Windows Mobile.
Implementation of Windows Mobile Client
SeeSource codeObexpushdevice project.
Private void Menuitem1_click ( Object Sender, Eventargs E)
{
// Use the new select bluetooth device Dialog
Selectblustmthdevicedialog Sbdd = New Selectblustmthdevicedialog ();
Sbdd. showauthenticated = True ;
Sbdd. showremembered = True ;
Sbdd. showunknown = True ;
If (Sbdd. showdialog () = Dialogresult . OK)
{
Openfiledialog Ofdfiletobeam = New Openfiledialog ();
If (Ofdfiletobeam. showdialog () = Dialogresult . OK)
{
Cursor . Current = Cursors . Waitcursor;
System. Uri Uri = New Uri ( "Obex ://" + Sbdd. selecteddevice. deviceaddress. tostring () + "/" + System. Io. Path . Getfilename (ofdfiletobeam. filename ));
Obexwebresponse Response = Null ;
Try
{
Obexwebrequest Request = New Obexwebrequest (URI );
Request. readfile (ofdfiletobeam. filename );
Response = request. getresponse ()As Obexwebresponse ;
MessageBox . Show (response. statuscode. tostring ());
}
Catch
{
MessageBox . Show ( "Fail to beam the file" + URI );
}
Finally
{
If (Response! = Null )
{
Response. Close ();
}
}
Cursor . Current = Cursors . Default;
}
}
}
SelectblustmthdevicedialogIs a Bluetooth Discovery class in 32feet.net that automatically discovers peripheral Bluetooth devices and then presents them in the form of a dialog box. For example:
After selecting the target PC for file pushOpenfiledialogClass to push files, such:
PassObexwebrequestTo push files to the target machine.ObexwebrequestThe implementation mode of is similar to httpwebrequest. Both send requests and other responses. The response is encapsulated inObexwebresponseClass. If the obex service of the target machine is not enabled, the following error occurs. For more information about httpwebrequest files, see httpwebrequest development under. NET Compact framework.
PC server implementation
See the source code obexlistenerpc project.
Initialization
Inthehand. net. Bluetooth.Bluetoothradio. Primaryradio. mode = inthehand. net. Bluetooth.Radiomode. Discoverable;
Listener =NewObexlistener(Obextransport. Bluetooth );
Because Bluetooth communication supports communication between a device, find the main device and bind itObexlistener.
Start the service
Listener. Start ();
System. threading.ThreadT =NewSystem. threading.Thread(NewSystem. threading.Threadstart(Dealwithrequest ));
T. Start ();
Start the thread to process the request.
Process requests
Public void Dealwithrequest ()
{
While (Listener. islistening)
{
Try
{
Obexlistenercontext OLC = listener. getcontext ();
Obexlistenerrequest OLR = OLC. request;
String Filename = Uri . Unescapedatastring (OLR. rawurl. trimstart ( New char [] { '/' }));
OLR. writefile ( Environment . Specialfolder . Mydocuments + Datetime . Now. tostring ( "Yymmddhhmmss" ) + "" + Filename );
Begininvoke ( New Methodinvoker ( Delegate ()
{
This . Listboxdetail. Items. Add ( "Received" + Filename );
}));
}
Catch ( Exception E)
{
Begininvoke ( New Methodinvoker (Delegate ()
{
This . Listboxdetail. Items. Add (E. Message );
}));
Continue ;
}
}
}
The dealwithrequest () function processesObexlistenerrequestRequest. Store received filesEnvironment.SpecialfolderIn the. mydocuments folder. If you receive the "ABC shops.bmp" file.
Stop Service
Listener. Stop ();
The service must be stopped when the program is closed.
Push files from PC to Windows Mobile
In fact, the implementation of pushing files from PCs to Windows Mobile and Windows Mobile to PCs is the same. Using 32feet.net can push files between different winodws mobile or between different PCs, different projects in the source code can be used as needed.
PC client implementation
See the source code obexpushpc project.
Private void Buttonbeam_click ( Object Sender, Eventargs E)
{
// Use the new select bluetooth device Dialog
Selectblustmthdevicedialog Sbdd = New Selectblustmthdevicedialog ();
Sbdd. showauthenticated = True ;
Sbdd. showremembered = True ;
Sbdd. showunknown = True ;
If (Sbdd. showdialog () = Dialogresult . OK)
{
Openfiledialog Ofdfiletobeam = New Openfiledialog ();
If (Ofdfiletobeam. showdialog () = Dialogresult . OK)
{
Cursor . Current = Cursors . Waitcursor;
System. Uri Uri = New Uri ( "Obex ://" + Sbdd. selecteddevice. deviceaddress. tostring () + "/" + System. Io. Path . Getfilename (ofdfiletobeam. filename ));
Obexwebresponse Response = Null ;
Try
{
Obexwebrequest Request = New Obexwebrequest (URI );
Request. readfile (ofdfiletobeam. filename );
Response = request. getresponse () As Obexwebresponse ;
MessageBox . Show (response. statuscode. tostring ());
}
Catch
{
MessageBox . Show ("Fail to beam the file" + URI );
}
Finally
{
If (Response! = Null )
{
Response. Close ();
}
}
Cursor . Current = Cursors . Default;
}
}
}
It can be said that there is no difference between the "Implementation of Windows Mobile client" and the "32 feet.net shielding.
Select the target device.
Select transfer file.
By default, Windows Mobile enables the obex service. Therefore, you can receive files on Windows Mobile without deploying any program. It is very convenient. If some devices do not support obex services and you need to deploy programs, you can use the obexlistenerdevice project in the source code.
I also use this obex push program to successfully send files to non-Windows Mobile systems. This is a common obex file transfer program.
Environment: VS 2008 + XP + Windows Mobile 6.5 + 32feet.net
Source code: http://files.cnblogs.com/procoder/BluetoothObex.rar