8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx

上传人:b****5 文档编号:6409786 上传时间:2023-01-06 格式:DOCX 页数:9 大小:490.93KB
下载 相关 举报
8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx_第1页
第1页 / 共9页
8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx_第2页
第2页 / 共9页
8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx_第3页
第3页 / 共9页
8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx_第4页
第4页 / 共9页
8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx

《8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx》由会员分享,可在线阅读,更多相关《8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx(9页珍藏版)》请在冰豆网上搜索。

8Windows Mobile 与 PC之间的通过蓝牙Bluetooth 传输文件的开发.docx

8WindowsMobile与PC之间的通过蓝牙Bluetooth传输文件的开发

背景

之前也写过一些WindowsMobile和Wince下Bluetooth开发的文章如下。

.NETCompactFramework下的Bluetooth开发之WindowsEmbeddedSourceToolsforBluetooth

.NETCompactFramework下的Bluetooth开发之32feet.NET

.NETCompactFramework下的Bluetooth开发之BluetoothVirtualSerialPort(可以用于把Bluetooth的GPSreceiver变成串口)

.NETCompactFramework下的Bluetooth设备的配对

30Daysof.NET[WindowsMobileApplications]-Day02:

BluetoothManager(蓝牙管理器)(简单的Bluetooth应用)

.NETCompactFramework下的Bluetooth广播程序的开发

期间有两个同学问我如何实现蓝牙的文件传输,今天整理出蓝牙文件传输的代码实现,并把他记录下来。

简介

本文讲述WindowsMobile和PC之间蓝牙文件传输的实现。

通过使用库对Obex的封装实现了Push文件的程序。

ObexPush的PC程序可以给所有支持Obex的设备传输文件,包括非WindowsMobile的设备。

OBEX

蓝牙文件传输可以借助OBEX实现。

OBEX(TheObjectExchangeProtocol,对象交换协议)被广泛用于个人无线网络中设备的文件传输,基本上所有的移动设备都支持该协议。

实现了OBEX,不仅仅可以实现WindowMobile和PC的文件传输,可以实现所有支持OBEX协议的设备的文件传输。

关于OBEX可以参考ObjectExchangeProtocol.

 

WindowsMobile推文件到PC

本节讲述WindowsMobile推文件到PC的实现,其实PC推文件到WindowsMobile的实现差异性不大。

WindowsMobile客户端的实现

见源代码ObexPushDevice项目。

privatevoidmenuItem1_Click(objectsender,EventArgse)

{

//usethenewselectbluetoothdevicedialog

SelectBluetoothDeviceDialogsbdd=newSelectBluetoothDeviceDialog();

sbdd.ShowAuthenticated=true;

sbdd.ShowRemembered=true;

sbdd.ShowUnknown=true;

if(sbdd.ShowDialog()==DialogResult.OK)

{

OpenFileDialogofdFileToBeam=newOpenFileDialog();

if(ofdFileToBeam.ShowDialog()==DialogResult.OK)

{

Cursor.Current=Cursors.WaitCursor;

System.Uriuri=newUri("obex:

//"+sbdd.SelectedDevice.DeviceAddress.ToString()+"/"+System.IO.Path.GetFileName(ofdFileToBeam.FileName));

ObexWebResponseresponse=null;

try

{

ObexWebRequestrequest=newObexWebRequest(uri);

request.ReadFile(ofdFileToBeam.FileName);

response=request.GetResponse()asObexWebResponse;

MessageBox.Show(response.StatusCode.ToString());

}

catch

{

MessageBox.Show("Failtobeamthefile"+uri);

}

finally

{

if(response!

=null)

{

response.Close();

}

}

Cursor.Current=Cursors.Default;

}

}

}

SelectBluetoothDeviceDialog是里面的一个蓝牙发现类,自动发现周边的蓝牙设备,然后通过对话框的形式呈现。

如下图:

选择要推文件的目标PC后,通过OpenFileDialog类选择要推动文件,如下图:

 

通过ObexWebRequest来推文件到目标机器。

ObexWebRequest的实现模式和HttpWebRequest类似,都是发送请求,等等回应,回应封装在ObexWebResponse类里面。

如果目标机器的Obex服务没有打开,会发生下面的错误。

关于HttpWebRequest的文件可以参考.NETCompactFramework下HttpWebRequest开发。

 

PC服务端的实现

见源代码ObexListenerPC项目。

初始化

InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode=InTheHand.Net.Bluetooth.RadioMode.Discoverable;

listener=newObexListener(ObexTransport.Bluetooth);

由于蓝牙通信支持一个设备的通信,所以找出主要(Primary)设备,把他绑定到ObexListener里。

启动服务

listener.Start();

System.Threading.Threadt=newSystem.Threading.Thread(newSystem.Threading.ThreadStart(DealWithRequest));

t.Start();

启动线程来处理请求。

处理请求

publicvoidDealWithRequest()

{

while(listener.IsListening)

{

try

{

ObexListenerContextolc=listener.GetContext();

ObexListenerRequestolr=olc.Request;

stringfilename=Uri.UnescapeDataString(olr.RawUrl.TrimStart(newchar[]{'/'}));

olr.WriteFile(Environment.SpecialFolder.MyDocuments+DateTime.Now.ToString("yyMMddHHmmss")+""+filename);

BeginInvoke(newMethodInvoker(delegate()

{

this.listBoxDetail.Items.Add("Received"+filename);

}));

}

catch(Exceptione)

{

BeginInvoke(newMethodInvoker(delegate()

{

this.listBoxDetail.Items.Add(e.Message);

}));

continue;

}

}

}

DealWithRequest()函数处理来自客户端的ObexListenerRequest请求。

把接收的文件存放到Environment.SpecialFolder.MyDocuments文件夹里面。

如下图收到"abcshops.bmp”文件。

 

停止服务

listener.Stop();

程序关闭时需要停止服务。

 

PC推文件到WindowsMobile

其实PC推文件到WindowsMobile和WindowsMobile推文件到PC的实现是一样的,使用可以在不同WinodwsMobile之间,或者不同PC之间互相推文件,根据需求不同,可以利用源码中的不同项目。

PC客户端的实现

见源代码ObexPushPC项目。

privatevoidbuttonBeam_Click(objectsender,EventArgse)

{

//usethenewselectbluetoothdevicedialog

SelectBluetoothDeviceDialogsbdd=newSelectBluetoothDeviceDialog();

sbdd.ShowAuthenticated=true;

sbdd.ShowRemembered=true;

sbdd.ShowUnknown=true;

if(sbdd.ShowDialog()==DialogResult.OK)

{

OpenFileDialogofdFileToBeam=newOpenFileDialog();

if(ofdFileToBeam.ShowDialog()==DialogResult.OK)

{

Cursor.Current=Cursors.WaitCursor;

System.Uriuri=newUri("obex:

//"+sbdd.SelectedDevice.DeviceAddress.ToString()+"/"+System.IO.Path.GetFileName(ofdFileToBeam.FileName));

ObexWebResponseresponse=null;

try

{

ObexWebRequestrequest=newObexWebRequest(uri);

request.ReadFile(ofdFileToBeam.FileName);

response=request.GetResponse()asObexWebResponse;

MessageBox.Show(response.StatusCode.ToString());

}

catch

{

MessageBox.Show("Failtobeamthefile"+uri);

}

finally

{

if(response!

=null)

{

response.Close();

}

}

Cursor.Current=Cursors.Default;

}

}

}

可以说和上面实现的“WindowsMobile客户端的实现”没有区别,屏蔽的差异性。

选择目标设备。

选择传输文件。

 

WindowsMobile默认是打开了Obex的服务,所以,在WindowsMobile可以不用部署任何程序就可以接收文件了。

非常方便,如果某些设备不支持Obex的服务,需要部署程序,可以使用源代码中的ObexListenerDevice项目。

我同时使用这个ObexPush程序给非WindowsMobile系统成功发送文件。

这是一个通用的Obex文件传输程序。

环境:

VS2008+XP+WindowsMobile6.5+

源代码:

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1