C#编程连接蓝牙设备文件收发.docx
《C#编程连接蓝牙设备文件收发.docx》由会员分享,可在线阅读,更多相关《C#编程连接蓝牙设备文件收发.docx(10页珍藏版)》请在冰豆网上搜索。
C#编程连接蓝牙设备文件收发
现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用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. Console.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:
" + 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.WriteLine("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插入了蓝牙适配器,便会显示蓝牙相关信息:
然后我们就要利用蓝牙收发文件了:
前提是蓝牙设备(如手机)已经和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, 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.Information);
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. dialog.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() + " 设备名:
" + 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 = Path.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.GetFileName(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. MessageBox.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. 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)
93. {
94. radio.Mode = RadioMode.Discoverable;//设置本地蓝牙可被检测
95. listener = new ObexListener(ObexTransport.Bluetooth);//创建监听
96. listener.Start();
97. if (listener.IsListening)
98. {
99. buttonListen.Text = "停止";
100. labelRecInfo.Text = "开始监听";
101. listenThread = new Thread(receiveFile);//开启监听线程
102. listenThread.Start();
103. }
104. }
105. else
106. {
107. listener.Stop();
108. buttonListen.Text = "监听";
109. labelRecInfo.Text = "停止监听";
110. }
111. }
112.
113. private void receiveFile()//收文件方法
114. {
115. ObexListenerContext context = null;
116. ObexListenerRequest request = null;
117. while (listener.IsListening)
118. {
119. context = listener.GetContext();//获取监听上下文
120. if (context == null)
121. {
122. break;
123. }
124. request = context.Request;//获取请求
125. string uriString = Uri.UnescapeDataString(request.RawUrl);//将uri转换成字符串
126. string recFileName = recDir + uriString;
127. request.WriteFile(recFileName);//接收文件
128. labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' });
129. }
130. }
131.
132. private void Form1_FormClosed(object sender, FormClosedEventArgs e)
133. {
134. if (sendThread !
= null)
135. {
136. sendThread.Abort();
137. }
138. if (listenThread !
= null)
139. {
140. listenThread.Abort();
141. }
142. if (listener !
= null && listener.IsListening)
143. {
144. listener.Stop();
145. }
146. }
147.}
程序界面:
SelectBluetoothDeviceDialog是一个InTheHand.Net.Personal提供的窗体,用于选择蓝牙设备:
从手机往电脑发送文件需要在电脑上开启监听ObexListener,才能收到文件。
核心代码:
C#代码
1.BluetoothRadio radio = null;//蓝牙适配器
2.string sendFileName = null;//发送文件名
3.BluetoothAddress sendAddress = null;//发送目的地址
4.ObexListener listener = null;//监听器
5.string recDir = null;//接受文件存放目录
6.Thread listenThread, sendThread;//发送/接收线程
7.
8.radio = BluetoothRadio.PrimaryRadio;//获取当前PC的蓝牙适配器
9.
10.//关于蓝牙设备选择对话框
11.SelectBluetoothDeviceDialog dialog = new SelectBluetoothDeviceDialog();
12.dialog.ShowRemembered = true;//显示已经记住的蓝牙设备
13.dialog.ShowAuthenticated = true;//显示认证过的蓝牙设备
14.dialog.ShowUnknown = true;//显示位置蓝牙设备
15.sendAddress = dialog.SelectedDevice.DeviceAddress;//获取选择的远程蓝牙地址
16.
17.//发送文件操作
18.ObexWebRequest request = new ObexWebRequest(sendAddress, Path.GetFileName(sendFileName));//创建网络请求
19.WebResponse response = null;
20.request.ReadFile(sendFileName);//发送文件
21.response = request.GetResponse();//获取回应
22.response.Close();
23.
24.//接收文件
25.radio.Mode = RadioMode.Discoverable;//设置本地蓝牙可被检测
26.listener = new ObexListener(ObexTransport.Bluetooth);//创建监听
27.listener.Start();
28.listener.Stop();
29.
30.ObexListenerContext context = null;
31.ObexListenerRequest request = null;
32.context = listener.GetContext();//获取监听上下文
33.request = context.Request;//获取请求
34.string uriString = Uri.UnescapeDataString(request.RawUrl);//将uri转换成字符串
35.string recFileName = recDir + uriString;
36.request.WriteFile(recFileName);//接收文件
37.labelRecInfo.Text = "收到文件" + uriString.TrimStart(new char[] { '/' }
end