




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用USB蓝牙方便的连接手机等蓝牙设备进行通信。操作蓝牙要使用类库InTheHand.Net.Personal首先在项目中引用该类库;C#代码1. staticvoidMain(stringargs)2. 3. BluetoothRadiobluetoothRadio=BluetoothRadio.PrimaryRadio;4. if(bluetoothRadio=null)5. 6. Console.WriteLine(没有找到本机蓝牙设备!);7. 8. else9. 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. publicpartialclassForm1:Form2. 3. BluetoothRadioradio=null;/蓝牙适配器4. stringsendFileName=null;/发送文件名5. BluetoothAddresssendAddress=null;/发送目的地址6. ObexListenerlistener=null;/监听器7. stringrecDir=null;/接受文件存放目录8. ThreadlistenThread,sendThread;/发送/接收线程9. 10. publicForm1()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. privatevoidbuttonSelectBluetooth_Click(objectsender,EventArgse)/选择远程蓝牙设备24. 25. SelectBluetoothDeviceDialogdialog=newSelectBluetoothDeviceDialog();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. privatevoidbuttonSelectFile_Click(objectsender,EventArgse)/选择要发送的本地文件37. 38. OpenFileDialogdialog=newOpenFileDialog();39. if(dialog.ShowDialog()=DialogResult.OK)40. 41. sendFileName=dialog.FileName;/设置文件名42. labelPath.Text=Path.GetFileName(sendFileName);43. 44. 45. 46. privatevoidbuttonSend_Click(objectsender,EventArgse)/发送按钮47. 48. sendThread=newThread(sendFile);/开启发送文件线程49. sendThread.Start();50. 51. 52. privatevoidsendFile()/发送文件方法53. 54. ObexWebRequestrequest=newObexWebRequest(sendAddress,Path.GetFileName(sendFileName);/创建网络请求55. WebResponseresponse=null;56. try57. 58. buttonSend.Enabled=false;59. request.ReadFile(sendFileName);/发送文件60. labelInfo.Text=开始发送!;61. response=request.GetResponse();/获取回应62. labelInfo.Text=发送完成!;63. 64. catch(System.Exceptionex)65. 66. MessageBox.Show(发送失败!,提示,MessageBoxButtons.OK,MessageBoxIcon.Warning);67. labelInfo.Text=发送失败!;68. 69. finally70. 71. if(response!=null)72. 73. response.Close();74. buttonSend.Enabled=true;75. 76. 77. 78. 79. privatevoidbuttonselectRecDir_Click(objectsender,EventArgse)/选择接受目录80. 81. FolderBrowserDialogdialog=newFolderBrowserDialog();82. dialog.Description=请选择蓝牙接收文件的存放路径;83. if(dialog.ShowDialog()=DialogResult.OK)84. 85. recDir=dialog.SelectedPath;86. labelRecDir.Text=recDir;87. 88. 89. 90. privatevoidbuttonListen_Click(objectsender,EventArgse)/开始/停止监听91. 92. if(listener=null|!listener.IsListening)93. 94. radio.Mode=RadioMode.Discoverable;/设置本地蓝牙可被检测95. listener=newObexListener(ObexTransport.Bluetooth);/创建监听96. listener.Start();97. if(listener.IsListening)98. 99. buttonListen.Text=停止;100. labelRecInfo.Text=开始监听;101. listenThread=newThread(receiveFile);/开启监听线程102. listenThread.Start();103. 104. 105. else106. 107. listener.Stop();108. buttonListen.Text=监听;109. labelRecInfo.Text=停止监听;110. 111. 112. 113. privatevoidreceiveFile()/收文件方法114. 115. ObexListenerContextcontext=null;116. ObexListenerRequestrequest=null;117. while(listener.IsListening)118. 119. context=listener.GetContext();/获取监听上下文120. if(context=null)121. 122. break;123. 124. request=context.Request;/获取请求125. stringuriString=Uri.UnescapeDataString(request.RawUrl);/将uri转换成字符串126. stringrecFileName=recDir+uriString;127. request.WriteFile(recFileName);/接收文件128. labelRecInfo.Text=收到文件+uriString.TrimStart(newchar/);129. 130. 131. 132. privatevoidForm1_FormClosed(objectsender,FormClosedEventArgse)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. BluetoothRadioradio=null;/蓝牙适配器2. stringsendFileName=null;/发送文件名3. BluetoothAddresssendAddress=null;/发送目的地址4. ObexListenerlistener=null;/监听器5. stringrecDir=null;/接受文件存放目录6. ThreadlistenThread,sendThread;/发送/接收线程7. 8. radio=BluetoothRadio.PrimaryRadio;/获取当前PC的蓝牙适配器9. 10. /关于蓝牙设备选择对话框11. SelectBluetoothDeviceDialogdialog=newSelectBluetoothDeviceDialog();12. dialog.ShowRemembered=true;/显示已经记住的蓝牙设备13. dialog.ShowAuthenticated=true;/显示认证过的蓝牙设备14. dialog.ShowUnknown=true;/显示位置蓝牙设备15. sendAddress=dialog.SelectedDevice.DeviceAddress;/获取选择的远程蓝牙地址16. 17. /发送文件操作18. ObexWebRequestrequest=newObexWebRequest(sendAddress,Path.GetFileName(sendFileName);/创建网络请求19. WebResponserespons
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025授权代理委托合同
- 2025销售补充合同范本
- 2025春季呼和浩特石化分公司高校毕业生招聘20人模拟试卷完整答案详解
- 2025广东珠海市香洲区劳动人事争议仲裁委员会选聘特邀调解员10人考前自测高频考点模拟试题及答案详解(各地真题)
- 2025福建亿力集团有限公司所属单位招聘98人考前自测高频考点模拟试题(含答案详解)
- 2025贵州黔南州瓮安县江界河镇招聘城镇公益性岗位人员1人考前自测高频考点模拟试题及1套参考答案详解
- 2025年上半年四川凉山州宁南县考核招聘教师44人考前自测高频考点模拟试题(含答案详解)
- 2025年甘肃医学院招聘事业编制专业技术人员13人(第一批)模拟试卷及答案详解(易错题)
- 2025福建福州市长乐区行政服务中心管理委员会招聘编外人员2人考前自测高频考点模拟试题及参考答案详解1套
- 2025知识产权许可合同样书
- 2025年新版中层副职面试题及答案
- 蜂窝组织炎护理小讲课
- 智慧树知道网课《工业机器人技术基础》课后章节测试满分答案
- (一检)泉州市2026届高三高中毕业班质量监测(一)数学试卷(含标准答案)
- 2025年福建省榕圣建设发展有限公司项目招聘12人笔试参考题库附带答案详解
- 矿山设备检修安全培训课件
- 2025-2030数据安全合规审计服务市场爆发及等保测评机构并购价值评估
- 纤维转盘滤布滤池运行维护技术说明
- 2025至2030中国无烟产品行业发展趋势分析与未来投资战略咨询研究报告
- 小学六年级体育教案(全册48课时)
- 不可撤销跟单信用证申请书中英文
评论
0/150
提交评论