程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Windows系統中使用C#編寫藍牙通信程序的簡單實例

Windows系統中使用C#編寫藍牙通信程序的簡單實例

編輯:關於C語言

現在很多電腦提供了藍牙支持,很多筆記本網卡也集成了藍牙功能,也可以采用USB藍牙方便的連接手機等藍牙設備進行通信。
操作藍牙要使用類庫InTheHand.Net.Personal
首先在項目中引用該類庫;

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 static void Main(string[] args) { BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio; if (bluetoothRadio == null) { Console.WriteLine("沒有找到本機藍牙設備!"); } else { Console.WriteLine("ClassOfDevice: " + bluetoothRadio.ClassOfDevice); Console.WriteLine("HardwareStatus: " + bluetoothRadio.HardwareStatus); Console.WriteLine("HciRevision: " + bluetoothRadio.HciRevision); Console.WriteLine("HciVersion: " + bluetoothRadio.HciVersion); Console.WriteLine("LmpSubversion: " + bluetoothRadio.LmpSubversion); Console.WriteLine("LmpVersion: " + bluetoothRadio.LmpVersion); Console.WriteLine("LocalAddress: " + bluetoothRadio.LocalAddress); Console.WriteLine("Manufacturer: " + bluetoothRadio.Manufacturer); Console.WriteLine("Mode: " + bluetoothRadio.Mode); Console.WriteLine("Name: " + bluetoothRadio.Name); Console.WriteLine("Remote:" + bluetoothRadio.Remote); Console.WriteLine("SoftwareManufacturer: " + bluetoothRadio.SoftwareManufacturer); Console.WriteLine("StackFactory: " + bluetoothRadio.StackFactory); } Console.ReadKey(); }

如果PC插入了藍牙適配器,便會顯示藍牙相關信息:

201648155454003.jpg (544×265)

然後我們就要利用藍牙收發文件了:
前提是藍牙設備(如手機)已經和PC配對了

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 public partial class Form1 : Form { BluetoothRadio radio = null;//藍牙適配器 string sendFileName = null;//發送文件名 BluetoothAddress sendAddress = null;//發送目的地址 ObexListener listener = null;//監聽器 string recDir = null;//接受文件存放目錄 Thread listenThread, sendThread;//發送/接收線程 public Form1() { InitializeComponent(); radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器 CheckForIllegalCrossThreadCalls = false;//不檢查跨線程調用 if (radio == null)//檢查該電腦藍牙是否可用 { MessageBox.Show("這個電腦藍牙不可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); labelRecDir.Text = recDir; } private void buttonSelectBluetooth_Click(object sender, EventArgs e)//選擇遠程藍牙設備 { SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true;//顯示已經記住的藍牙設備 dialog.ShowAuthenticated = true;//顯示認證過的藍牙設備 dialog.ShowUnknown = true;//顯示位置藍牙設備 if (dialog.ShowDialog() == DialogResult.OK) { sendAddress = dialog.SelectedDevice.DeviceAddress;//獲取選擇的遠程藍牙地址 labelAddress.Text = "地址:" + sendAddress.ToString() + " 設備名:" + dialog.SelectedDevice.DeviceName; } } private void buttonSelectFile_Click(object sender, EventArgs e)//選擇要發送的本地文件 { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { sendFileName = dialog.FileName;//設置文件名 labelPath.Text = Path.GetFileName(sendFileName); } } private void buttonSend_Click(object sender, EventArgs e)//發送按鈕 { sendThread = new Thread(sendFile);//開啟發送文件線程 sendThread.Start(); } private void sendFile()//發送文件方法 { ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//創建網絡請求 WebResponse response = null; try { buttonSend.Enabled = false; request.ReadFile(sendFileName);//發送文件 labelInfo.Text = "開始發送!"; response = request.GetResponse();//獲取回應 labelInfo.Text = "發送完成!"; } catch (System.Exception ex) { MessageBox.Show("發送失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); labelInfo.Text = "發送失敗!"; } finally { if (response != null) { response.Close(); buttonSend.Enabled = true; } } } private void buttonselectRecDir_Click(object sender, EventArgs e)//選擇接受目錄 { FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "請選擇藍牙接收文件的存放路徑"; if (dialog.ShowDialog() == DialogResult.OK) { recDir = dialog.SelectedPath; labelRecDir.Text = recDir; } } private void buttonListen_Click(object sender, EventArgs e)//開始/停止監聽 { if (listener == null || !listener.IsListening) { radio.Mode = RadioMode.Discoverable;//設置本地藍牙可被檢測 listener = new ObexListener(ObexTransport.Bluetooth);//創建監聽 listener.Start(); if (listener.IsListening) { buttonListen.Text = "停止"; labelRecInfo.Text = "開始監聽"; listenThread = new Thread(receiveFile);//開啟監聽線程 listenThread.Start(); } } else { listener.Stop(); buttonListen.Text = "監聽"; labelRecInfo.Text = "停止監聽"; } } private void receiveFile()//收文件方法 { ObexListenerContext context = null; ObexListenerRequest request = null; while (listener.IsListening) { context = listener.GetContext();//獲取監聽上下文 if (context == null) { break; } request = context.Request;//獲取請求 string uriString = Uri.UnescapeDataString(request.RawUrl);//將uri轉換成字符串 string recFileName = recDir + uriString; request.WriteFile(recFileName);//接收文件 labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' }); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { if (sendThread != null) { sendThread.Abort(); } if (listenThread != null) { listenThread.Abort(); } if (listener != null && listener.IsListening) { listener.Stop(); } } }

程序界面:

201648155559869.jpg (427×270)

SelectBluetoothDeviceDialog是一個InTheHand.Net.Personal提供的窗體,用於選擇藍牙設備:

201648155651467.jpg (650×495)

從手機往電腦發送文件需要在電腦上開啟監聽ObexListener,才能收到文件。

201648155708976.jpg (427×270)

核心代碼:

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 BluetoothRadio radio = null;//藍牙適配器 string sendFileName = null;//發送文件名 BluetoothAddress sendAddress = null;//發送目的地址 ObexListener listener = null;//監聽器 string recDir = null;//接受文件存放目錄 Thread listenThread, sendThread;//發送/接收線程 radio = BluetoothRadio.PrimaryRadio;//獲取當前PC的藍牙適配器 //關於藍牙設備選擇對話框 SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); dialog.ShowRemembered = true;//顯示已經記住的藍牙設備 dialog.ShowAuthenticated = true;//顯示認證過的藍牙設備 dialog.ShowUnknown = true;//顯示位置藍牙設備 sendAddress = dialog.SelectedDevice.DeviceAddress;//獲取選擇的遠程藍牙地址 //發送文件操作 ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//創建網絡請求 WebResponse response = null; request.ReadFile(sendFileName);//發送文件 response = request.GetResponse();//獲取回應 response.Close(); //接收文件 radio.Mode = RadioMode.Discoverable;//設置本地藍牙可被檢測 listener = new ObexListener(ObexTransport.Bluetooth);//創建監聽 listener.Start(); listener.Stop(); ObexListenerContext context = null; ObexListenerRequest request = null; context = listener.GetContext();//獲取監聽上下文 request = context.Request;//獲取請求 string uriString = Uri.UnescapeDataString(request.RawUrl);//將uri轉換成字符串 string recFileName = recDir + uriString; request.WriteFile(recFileName);//接收文件 labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' }

PS:關於InTheHand.Net.Personal
InTheHand.Net.Personal.dll來源於32feet.Net。
32feet.NET是shared-source的項目,支持CF.net 2.0以及桌面版本.NET framework,提供短距離領域(personal area networking technologIE)的通信功能,支持bluetooth,Infrared(IrDA)紅外等. 想了解更多的信息可以參考其 官方主頁,其項目的安裝包和源碼是放在微軟的開源工程網站CodePlex上面的,作為.Net開發人員我們必須要上的網站就是CodePlex~

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved