9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx

上传人:b****0 文档编号:8989382 上传时间:2023-05-16 格式:DOCX 页数:8 大小:221.10KB
下载 相关 举报
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第1页
第1页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第2页
第2页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第3页
第3页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第4页
第4页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第5页
第5页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第6页
第6页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第7页
第7页 / 共8页
9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx

《9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx》由会员分享,可在线阅读,更多相关《9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx(8页珍藏版)》请在冰点文库上搜索。

9Windows Mobile下通过蓝牙Bluetooth发送大文件的实现.docx

9WindowsMobile下通过蓝牙Bluetooth发送大文件的实现

背景

在前一篇文章WindowsMobile与PC之间的Bluetooth文件传输讲述了如何使用Obex开发Bluetooth文件传输的应用。

其中BenBen789同学指出不能传输大文件,因此需要实现大文件的传输。

 

简介

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

 

实现

这个发送大文件的实现是Brecham.Obex的例子程序,基于Brecham.Obex库来开发的,Brecham.Obex是基于的基础上实现的,可以参考Brecham.Obex。

这个库可以免费使用,但是需要注明依赖。

另一方面我没有找到这个库的源代码。

 

发送程序的主窗口。

 

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

DialogResultresult=openFileDialog1.ShowDialog();

if(result==DialogResult.OK)

{

Statestate=newState();

//------------------------------------------------------

//Getthefile

//------------------------------------------------------

StringputName;//="dummy.txt";

try{

state.m_fileStream=newFileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);

}catch(IOExceptionioex){

MessageBox.Show("Failedtoopenthefile:

"+ioex.ToString());

return;

}

state.m_progressStream=newReadProgressStream(state.m_fileStream);

state.m_progressStream.SetTotalReadLength(state.m_fileStream.Length);

putName=Path.GetFileName(openFileDialog1.FileName);

}//if

把选择的文件赋值给ReadProgressStream,这样就可以实现传输进度条功能了。

但是在现实使用中,这个功能还是不work。

 

如果选择了发送文件,弹出设备搜索窗口,对接收设备进行选择。

设备选择和链接对话框其实在里面实现的。

//------------------------------------------------------

//Getthepeer

//------------------------------------------------------

ProtocolFamilypf=this.protocolComboBox1.SelectedProtocol;

state.m_conn=newBrecham.Obex.Net.GuiObexSessionConnection(pf,false,this.labelStatus);

//Setourreceivesizeandrestrictoursendsize

state.m_conn.ObexBufferSize=2028;

state.m_conn.MaxSendSize=2048;

try{

if(!

state.m_conn.Connect()){

//usercancelledtheconnect

return;

}

}catch(Exceptionex){

TypetypeOfEx=ex.GetType();

if(typeof(ObexResponseException)!

=typeOfEx

&&typeof(System.Net.ProtocolViolationException)!

=typeOfEx

&&typeof(System.IO.IOException)!

=typeOfEx

&&typeof(System.Net.Sockets.SocketException)!

=typeOfEx){

//Notoneoftheexpectedexceptiontypes,rethrow!

throw;

}

Stringdescr=ex.Message+"\r\n"+ex.GetType().ToString();

this.labelStatus.Text="Connectfailed:

"+descr;

MessageBox.Show(descr,"Connectfailed");

return;

}

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

StreampeerStream=state.m_conn.PeerStream;

//------------------------------------------------------

//Send

//------------------------------------------------------

try

{

ObexClientSessionsess=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=newPutFromNtiCaller(sess.PutFrom);

AsyncCallbackcb=newAsyncCallback(PutCompleted);

state.SetStartTime();

IAsyncResultar=state.m_putCaller.BeginInvoke(

state.m_progressStream,putName,null,state.m_fileStream.Length,

cb,state);

//EnabletheCancelbutton

m_cancelled=false;

buttonCancel.Enabled=true;

buttonCancel.Tag=sess;//Givethebuttonaccesstothesession.

}

catch

{

//AllOBEXerrorsoccuronthedelegate.BeginInvoke'sthread,and

//thusareseenoncallingEndInvokeinthePutCompletedmethod.

//

//Justensurethestreamsareclosedetc,andrethrow.

state.Dispose();

throw;

}

通过ObexClientSession保存发送到会话,用于取消发送。

PutFromNtiCaller的BeginInvoke()通过线程发送文件。

发送完毕,10M的文件花了3分45秒。

我试过30M的文件也成功,但是文件不知道放哪里了。

我对发送文件的设计是这样认为的,我不提倡用蓝牙发送很大的文件,如果需要蓝牙发送很大很大的文件,那样需要考虑设计方案是否合理,为什么用蓝牙发送那么大的文件,真正的需求是什么,可替换方案是什么。

如果确实有使用蓝牙发送大文件的需要,可以使用Brecham.Obex来实现。

 

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

 

文件保存后放到MyDocuments里面了。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 院校资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2