博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Mobile下通过蓝牙(Bluetooth)发送大文件的实现
阅读量:6933 次
发布时间:2019-06-27

本文共 3774 字,大约阅读时间需要 12 分钟。

背景

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

 

简介

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

 

实现

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

 

发送程序的主窗口。

 

使用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。

 

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

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

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()通过线程发送文件。

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

 

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

 

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

 

其他相关文章

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

 (可以用于把Bluetooth的GPS receiver变成串口)

 (简单的Bluetooth应用)

 

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

源代码:

    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2009/10/01/Windows_Mobile_Bluetooth_Transfer_Big_File.html,如需转载请自行联系原作者

你可能感兴趣的文章
ASPxGridView之PreviewRow
查看>>
vsftd搭建
查看>>
洛谷P2057 【SHOI2007】善意的投票
查看>>
一篇关于java变量定义的文章
查看>>
jQuery获取json数据
查看>>
超出父控件怎么才能点击
查看>>
2011最新个人所得税计算器---起点3500元
查看>>
PHP随手记1--内置函数date
查看>>
【转】【RDS教程】专业DBA速成 - CPU优化篇
查看>>
UVa 11624,两次BFS
查看>>
关于输入流与输出流
查看>>
MVC分页控件
查看>>
“==”和equals方法的区别
查看>>
原生socket穿透HTTP代理服务器
查看>>
2019/5/12 查漏补缺
查看>>
超酷图片压缩工具,就是不支持批量
查看>>
jvm设置
查看>>
C语言引用不同路径下的头文件的方法
查看>>
Git查看、删除、重命名远程分支和tag【转】
查看>>
PHP生成PDF并转换成图片爬过的坑
查看>>