Windows體系中應用C#編寫藍牙通訊法式的簡略實例。本站提示廣大學習愛好者:(Windows體系中應用C#編寫藍牙通訊法式的簡略實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Windows體系中應用C#編寫藍牙通訊法式的簡略實例正文
如今許多電腦供給了藍牙支撐,許多筆記本網卡也集成了藍牙功效,也能夠采取USB藍牙便利的銜接手機等藍牙裝備停止通訊。
操作藍牙要應用類庫InTheHand.Net.Personal
起首在項目中援用該類庫;
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拔出了藍牙適配器,便會顯示藍牙相干信息:
然後我們就要應用藍牙收發文件了:
條件是藍牙裝備(如手機)曾經和PC配對了
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(); } } }
法式界面:
SelectBluetoothDeviceDialog是一個InTheHand.Net.Personal供給的窗體,用於選擇藍牙裝備:
從手機往電腦發送文件須要在電腦上開啟監聽ObexListener,能力收到文件。
焦點代碼:
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~