背景之前也写过一些Windows Mobile和Wince下Bluetooth开发的文章如下。
(可以用于把Bluetooth的GPS receiver变成串口)
(简单的Bluetooth应用)
期间有两个同学问我如何实现蓝牙的文件传输,今天整理出蓝牙文件传输的代码实现,并把他记录下来。
简介本文讲述Windows Mobile和PC之间蓝牙文件传输的实现。通过使用32feet.net库对Obex的封装实现了Push文件的程序。Obex Push 的PC程序可以给所有支持Obex的设备传输文件,包括非Windows Mobile的设备。
OBEX蓝牙文件传输可以借助OBEX实现。OBEX(The Object Exchange Protocol,对象交换协议)被广泛用于个人无线网络中设备的文件传输,基本上所有的移动设备都支持该协议。实现了OBEX,不仅仅可以实现Window Mobile和PC的文件传输,可以实现所有支持OBEX协议的设备的文件传输。关于OBEX可以参考 Object Exchange Protocol.
Windows Mobile 推文件到PC本节讲述Windows Mobile推文件到PC的实现,其实PC推文件到Windows Mobile的实现差异性不大。
Windows Mobile客户端的实现见源代码ObexPushDevice项目。
- private void menuItem1_Click(object sender, EventArgs e)
- {
- // use the new select bluetooth device dialog
- SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
- 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;
- }
- }
- }
复制代码SelectBluetoothDeviceDialog 是 32feet.net里面的一个蓝牙发现类,自动发现周边的蓝牙设备,然后通过对话框的形式呈现。如下图:
![](http://pin5i.com/images/attachicons/attachment.gif)
附件:
您所在的用户组无法下载或查看附件