Windows Mobile下通过蓝牙(Bluetooth)发送大文件的实现

背景

在前一篇文章 Windows Mobile 与 PC之间的Bluetooth 文件传输 讲述了如何使用Obex开发Bluetooth文件传输的应用。其中BenBen789同学指出不能传输大文件,因此需要实现大文件的传输。

简介

本文讲述在Windows Mobile下通过蓝牙发送大文件的实现。

实现

这个发送大文件的实现是Brecham.Obex的例子程序,基于Brecham.Obex库来开发的,Brecham.Obex是基于32feet.net的基础上实现的,可以参考Brecham.Obex。这个库可以免费使用,但是需要注明依赖。另一方面我没有找到这个库的源代码。

BigFile1

发送程序的主窗口。

BigFile2

使用System.Windows.Forms.OpenFileDialog弹出选择需要发送文件的窗口。

         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

把选择的文件赋值给ReadProgressStream,这样就可以实现传输进度条功能了。但是在现实使用中,这个功能还是不work。

BigFile3

如果选择了发送文件,弹出设备搜索窗口,对接收设备进行选择。设备选择和链接对话框其实在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 cancelled 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;
}

BigFile4

选择设备后,开始发送过程了。

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;
}

通过ObexClientSession 保存发送到会话,用于取消发送。PutFromNtiCaller的BeginInvoke()通过线程发送文件。

BigFile5

发送完毕,10M的文件花了3分45秒。我试过30M的文件也成功,但是文件不知道放哪里了。我对发送文件的设计是这样认为的,我不提倡用蓝牙发送很大的文件,如果需要蓝牙发送很大很大的文件,那样需要考虑设计方案是否合理,为什么用蓝牙发送那么大的文件,真正的需求是什么,可替换方案是什么。如果确实有使用蓝牙发送大文件的需要,可以使用Brecham.Obex来实现。

BigFile6

接收文件的设备,这个设备不需要安装任何程序,一般的Windows Mobile都有Obex的Service在运行。

BigFile7

文件保存后放到My Documents里面了。

 

其他相关文章

可以参考我以前写的关于Bluetooth的文件。

.NET Compact Framework下的Bluetooth开发 之 Windows Embedded Source Tools for Bluetooth

.NET Compact Framework下的Bluetooth开发 之 32feet.NET

.NET Compact Framework下的Bluetooth开发 之 Bluetooth Virtual Serial Port (可以用于把Bluetooth的GPS receiver变成串口)

.NET Compact Framework下的Bluetooth设备的配对

30 Days of .NET [Windows Mobile Applications] - Day 02: Bluetooth Manager(蓝牙管理器) (简单的Bluetooth应用)

.NET Compact Framework下的Bluetooth广播程序的开发

Windows Mobile 与 PC之间的Bluetooth 文件传输

 

环境: VS 2008 + XP + Windows Mobile 6.5 + Brecham.Obex + 32feet.net

 

转载自:http://hi.baidu.com/alalmn/blog/item/32b949812f9cfac8bc3e1eca.html

转载于:https://www.cnblogs.com/Wolves/archive/2010/12/01/1893439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值