Windows Mobile 與 PC之間的通過藍芽(Bluetooth) 傳輸檔案的開發

來源:互聯網
上載者:User
文章目錄
  • 初始化
  • 啟動服務
  • 處理請求
  • 停止服務
背景

之前也寫過一些Windows Mobile和Wince下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之間藍芽檔案傳輸的實現。通過使用32feet.net庫對Obex的封裝實現了Push檔案的程式。Obex Push 的PC程式可以給所有支援Obex的裝置傳輸檔案,包括非Windows Mobile的裝置。

OBEX

藍芽檔案傳輸可以藉助OBEX實現。OBEX(The Object Exchange Protocol,對象交換協議)被廣泛用於個人無線網路中裝置的檔案傳輸,基本上所有的行動裝置都支援該協議。實現了OBEX,不僅僅可以實現Window Mobile和PC的檔案傳輸,可以實現所有支援OBEX協議的裝置的檔案傳輸。關於OBEX可以參考 Object Exchange Protocol.

 

Windows Mobile 推檔案到PC

本節講述Windows Mobile推檔案到PC的實現,其實PC推檔案到Windows Mobile的實現差異性不大。

Windows Mobile用戶端的實現

見原始碼ObexPushDevice項目。

private void menuItem1_Click(object sender, EventArgs e)
{
// use the new select bluetooth device dialog
SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
sbdd.ShowAuthenticated = true;
sbdd.ShowRemembered = true;
sbdd.ShowUnknown = true;
if (sbdd.ShowDialog() == DialogResult.OK)
{
OpenFileDialog ofdFileToBeam = new OpenFileDialog();
if (ofdFileToBeam.ShowDialog() == DialogResult.OK)
{

Cursor.Current = Cursors.WaitCursor;
System.Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName));
ObexWebResponse response = null;
try
{
ObexWebRequest request = new ObexWebRequest(uri);
request.ReadFile(ofdFileToBeam.FileName);

response = request.GetResponse() as ObexWebResponse;
MessageBox.Show(response.StatusCode.ToString());
}
catch
{
MessageBox.Show("Fail to beam the file " + uri);
}
finally
{
if (response != null)
{
response.Close();
}
}
Cursor.Current = Cursors.Default;
}
}
}

SelectBluetoothDeviceDialog 是 32feet.net裡面的一個藍芽發現類,自動探索周邊的藍牙裝置,然後通過對話方塊的形式呈現。如:

選擇要推檔案的目標PC後,通過OpenFileDialog 類選擇要推動檔案,如:

 

通過ObexWebRequest 來推檔案到目標機器。ObexWebRequest 的實現模式和HttpWebRequest類似,都是發送請求,等等回應,回應封裝在ObexWebResponse 類裡面。如果目標機器的Obex服務沒有開啟,會發生下面的錯誤。關於HttpWebRequest的檔案可以參考 .NET Compact Framework下HttpWebRequest開發。

 

PC服務端的實現

見原始碼ObexListenerPC項目。

初始化
InTheHand.Net.Bluetooth.BluetoothRadio.PrimaryRadio.Mode = InTheHand.Net.Bluetooth.RadioMode.Discoverable;
listener = new ObexListener(ObexTransport.Bluetooth);

由於藍芽通訊支援一個裝置的通訊,所以找出主要(Primary)裝置,把他綁定到ObexListener裡。

啟動服務
listener.Start();
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DealWithRequest));
t.Start();

啟動線程來處理請求。

處理請求
public void DealWithRequest()
{
while (listener.IsListening)
{
try
{
ObexListenerContext olc = listener.GetContext();
ObexListenerRequest olr = olc.Request;
string filename = Uri.UnescapeDataString(olr.RawUrl.TrimStart(new char[] { '/' }));
olr.WriteFile(Environment.SpecialFolder.MyDocuments + DateTime.Now.ToString("yyMMddHHmmss") + " " + filename);
BeginInvoke(new MethodInvoker(delegate()
{
this.listBoxDetail.Items.Add("Received " + filename);
}));
}
catch(Exception e)
{
BeginInvoke(new MethodInvoker(delegate()
{
this.listBoxDetail.Items.Add(e.Message);
}));
continue;
}
}
}

DealWithRequest()函數處理來自用戶端的ObexListenerRequest 請求。把接收的檔案存放到Environment.SpecialFolder.MyDocuments檔案夾裡面。如收到"abc shops.bmp”檔案。

 

停止服務
listener.Stop();

程式關閉時需要停止服務。

 

PC 推檔案到Windows Mobile

其實PC推檔案到Windows Mobile和Windows Mobile推檔案到PC的實現是一樣的,使用32feet.net可以在不同Winodws Mobile之間,或者不同PC之間互相推檔案,根據需求不同,可以利用源碼中的不同項目。

PC用戶端的實現

見原始碼ObexPushPC項目。

private void buttonBeam_Click(object sender, EventArgs e)
{
// use the new select bluetooth device dialog
SelectBluetoothDeviceDialog sbdd = new SelectBluetoothDeviceDialog();
sbdd.ShowAuthenticated = true;
sbdd.ShowRemembered = true;
sbdd.ShowUnknown = true;
if (sbdd.ShowDialog() == DialogResult.OK)
{
OpenFileDialog ofdFileToBeam = new OpenFileDialog();
if (ofdFileToBeam.ShowDialog() == DialogResult.OK)
{

Cursor.Current = Cursors.WaitCursor;
System.Uri uri = new Uri("obex://" + sbdd.SelectedDevice.DeviceAddress.ToString() + "/" + System.IO.Path.GetFileName(ofdFileToBeam.FileName));
ObexWebResponse response = null;
try
{
ObexWebRequest request = new ObexWebRequest(uri);
request.ReadFile(ofdFileToBeam.FileName);

response = request.GetResponse() as ObexWebResponse;
MessageBox.Show(response.StatusCode.ToString());
}
catch
{
MessageBox.Show("Fail to beam the file " + uri);
}
finally
{
if (response != null)
{
response.Close();
}
}
Cursor.Current = Cursors.Default;
}
}
}

可以說和上面實現的“Windows Mobile用戶端的實現”沒有區別, 32feet.net屏蔽的差異性。

選擇目標裝置。

選擇傳輸檔案。

 

Windows Mobile預設是開啟了Obex的服務,所以,在Windows Mobile可以不用部署任何程式就可以接收檔案了。非常方便,如果某些裝置不支援Obex的服務,需要部署程式,可以使用原始碼中的ObexListenerDevice項目。

我同時使用這個Obex Push程式給非Windows Mobile系統成功傳送檔案。這是一個通用的Obex檔案傳輸程式。

環境: VS 2008 + XP + Windows Mobile 6.5 + 32feet.net

原始碼:http://files.cnblogs.com/procoder/BluetoothObex.rar

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.