Windows Mobile 与PC 之间的Bluetooth 文件传输- Visual Studio.NET ...
背景

之前也写过一些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项目。
  1. private void menuItem1_Click(object sender, EventArgs e)
  2. {
  3.     // use the new select bluetooth device dialog
  4.     SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
  5.     sbdd.ShowAuthenticated = true;
  6.     sbdd.ShowRemembered = true;
  7.     sbdd.ShowUnknown = true;
  8.     if (sbdd.ShowDialog() == DialogResult.OK)
  9.     {
  10.         OpenFileDialog ofdFileToBeam = new OpenFileDialog();
  11.         if (ofdFileToBeam.ShowDialog() == DialogResult.OK)
  12.         {
  13.            
  14.                 Cursor.Current = Cursors.WaitCursor;
  15.                 System.Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName));
  16.                 ObexWebResponse response = null;
  17.                 try
  18.                 {
  19.                     ObexWebRequest request = new ObexWebRequest(uri);
  20.                     request.ReadFile(ofdFileToBeam.FileName);

  21.                     response = request.GetResponse() as ObexWebResponse;
  22.                     MessageBox.Show(response.StatusCode.ToString());
  23.                 }
  24.                 catch
  25.                 {
  26.                     MessageBox.Show("Fail to beam the file " + uri);
  27.                 }
  28.                 finally
  29.                 {
  30.                     if (response != null)
  31.                     {
  32.                         response.Close();
  33.                     }
  34.                 }
  35.                 Cursor.Current = Cursors.Default;
  36.         }
  37.     }
  38. }
复制代码
SelectBluetoothDeviceDialog 是 32feet.net里面的一个蓝牙发现类,自动发现周边的蓝牙设备,然后通过对话框的形式呈现。如下图:


 附件: 您所在的用户组无法下载或查看附件

• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
• 
郑重声明:资讯 【Windows Mobile 与PC 之间的Bluetooth 文件传输- Visual Studio.NET ...】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——