现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用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~
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。