专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »博文摘选 » windowsbluetooth:Windows Mobile下通过Bluetooth发送大文件的实现 »正文

windowsbluetooth:Windows Mobile下通过Bluetooth发送大文件的实现

来源: 发布时间:星期五, 2009年10月2日 浏览:40次 评论:0

背景

在前一篇文章 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://files.cnblogs.com/procoder/PutGuiCs.rar


作者:Jake.NETJake's Blog on 博客园
出处:http://procoder.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

更多文章请看 .NET Compact Framework, WinCE, Windows Mobile开发系列

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: