c#编程连接蓝牙设备,文件收发

上传人:第*** 文档编号:33582835 上传时间:2018-02-15 格式:DOCX 页数:8 大小:151.23KB
返回 下载 相关 举报
c#编程连接蓝牙设备,文件收发_第1页
第1页 / 共8页
c#编程连接蓝牙设备,文件收发_第2页
第2页 / 共8页
c#编程连接蓝牙设备,文件收发_第3页
第3页 / 共8页
c#编程连接蓝牙设备,文件收发_第4页
第4页 / 共8页
c#编程连接蓝牙设备,文件收发_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《c#编程连接蓝牙设备,文件收发》由会员分享,可在线阅读,更多相关《c#编程连接蓝牙设备,文件收发(8页珍藏版)》请在金锄头文库上搜索。

1、现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用 USB 蓝牙方便的连接手机等蓝牙设备进行通信。操作蓝牙要使用类库 InTheHand.Net.Personal首先在项目中引用该类库;C#代码 1. static void Main(string args) 2. 3. BluetoothRadio bluetoothRadio = BluetoothRadio.PrimaryRadio; 4. if (bluetoothRadio = null) 5. 6. Console.WriteLine(没有找到本机蓝牙设备!); 7. 8. else 9. 10. Consol

2、e.WriteLine(ClassOfDevice: + bluetoothRadio.ClassOfDevice); 11. Console.WriteLine(HardwareStatus: + bluetoothRadio.HardwareStatus); 12. Console.WriteLine(HciRevision: + bluetoothRadio.HciRevision); 13. Console.WriteLine(HciVersion: + bluetoothRadio.HciVersion); 14. Console.WriteLine(LmpSubversion: +

3、 bluetoothRadio.LmpSubversion); 15. Console.WriteLine(LmpVersion: + bluetoothRadio.LmpVersion); 16. Console.WriteLine(LocalAddress: + bluetoothRadio.LocalAddress); 17. Console.WriteLine(Manufacturer: + bluetoothRadio.Manufacturer); 18. Console.WriteLine(Mode: + bluetoothRadio.Mode); 19. Console.Writ

4、eLine(Name: + bluetoothRadio.Name); 20. Console.WriteLine(Remote: + bluetoothRadio.Remote); 21. Console.WriteLine(SoftwareManufacturer: + bluetoothRadio.SoftwareManufacturer); 22. Console.WriteLine(StackFactory: + bluetoothRadio.StackFactory); 23. 24. Console.ReadKey(); 25. 如果 PC 插入了蓝牙适配器,便会显示蓝牙相关信息

5、: 然后我们就要利用蓝牙收发文件了:前提是蓝牙设备(如手机)已经和 PC 配对了C#代码 1. public partial class Form1 : Form 2. 3. BluetoothRadio radio = null;/蓝牙适配器 4. string sendFileName = null;/发送文件名 5. BluetoothAddress sendAddress = null;/发送目的地址 6. ObexListener listener = null;/监听器 7. string recDir = null;/接受文件存放目录 8. Thread listenThread

6、, sendThread;/发送/接收线程 9. 10. public Form1() 11. 12. InitializeComponent(); 13. radio = BluetoothRadio.PrimaryRadio;/获取当前 PC 的蓝牙适配器 14. CheckForIllegalCrossThreadCalls = false;/不检查跨线程调用 15. if (radio = null)/检查该电脑蓝牙是否可用 16. 17. MessageBox.Show(这个电脑蓝牙不可用!, 提示 , MessageBoxButtons.OK, MessageBoxIcon.Inf

7、ormation); 18. 19. recDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 20. labelRecDir.Text = recDir; 21. 22. 23. private void buttonSelectBluetooth_Click(object sender, EventArgs e)/选择远程蓝牙设备 24. 25. SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog(); 26. dialo

8、g.ShowRemembered = true;/显示已经记住的蓝牙设备 27. dialog.ShowAuthenticated = true;/显示认证过的蓝牙设备 28. dialog.ShowUnknown = true;/显示位置蓝牙设备 29. if (dialog.ShowDialog() = DialogResult.OK) 30. 31. sendAddress = dialog.SelectedDevice.DeviceAddress;/获取选择的远程蓝牙地址 32. labelAddress.Text = 地址: + sendAddress.ToString() + 设备

9、名: + dialog.SelectedDevice.DeviceName; 33. 34. 35. 36. private void buttonSelectFile_Click(object sender, EventArgs e)/选择要发送的本地文件 37. 38. OpenFileDialog dialog = new OpenFileDialog(); 39. if (dialog.ShowDialog() = DialogResult.OK) 40. 41. sendFileName = dialog.FileName;/设置文件名 42. labelPath.Text = Pa

10、th.GetFileName(sendFileName); 43. 44. 45. 46. private void buttonSend_Click(object sender, EventArgs e)/发送按钮 47. 48. sendThread = new Thread(sendFile);/开启发送文件线程 49. sendThread.Start(); 50. 51. 52. private void sendFile()/发送文件方法 53. 54. ObexWebRequest request = new ObexWebRequest(sendAddress, Path.Ge

11、tFileName(sendFileName);/创建网络请求 55. WebResponse response = null; 56. try 57. 58. buttonSend.Enabled = false; 59. request.ReadFile(sendFileName);/发送文件 60. labelInfo.Text = 开始发送!; 61. response = request.GetResponse();/获取回应 62. labelInfo.Text = 发送完成!; 63. 64. catch (System.Exception ex) 65. 66. Message

12、Box.Show(发送失败!, 提示 , MessageBoxButtons.OK, MessageBoxIcon.Warning); 67. labelInfo.Text = 发送失败!; 68. 69. finally 70. 71. if (response != null) 72. 73. response.Close(); 74. buttonSend.Enabled = true; 75. 76. 77. 78. 79. private void buttonselectRecDir_Click(object sender, EventArgs e)/选择接受目录 80. 81.

13、FolderBrowserDialog dialog = new FolderBrowserDialog(); 82. dialog.Description = 请选择蓝牙接收文件的存放路径; 83. if (dialog.ShowDialog() = DialogResult.OK) 84. 85. recDir = dialog.SelectedPath; 86. labelRecDir.Text = recDir; 87. 88. 89. 90. private void buttonListen_Click(object sender, EventArgs e)/开始/停止监听 91. 92. if (listener = null | !listener.IsListening) 9

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号