• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > Windows系统中使用C#编写蓝牙通信程序的简单实例

Windows系统中使用C#编写蓝牙通信程序的简单实例

作者:hzy3774 字体:[增加 减小] 来源:互联网 时间:2017-05-28

hzy3774 通过本文主要向大家介绍了使用windows7系统,windows7系统使用教程,windows系统使用技巧,windows10系统使用,windows系统使用等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用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(); 
} 
</div>

 如果PC插入了蓝牙适配器,便会显示蓝牙相关信息:

201648155454003.jpg (544×265)

 然后我们就要利用蓝牙收发文件了:
前提是蓝牙设备(如手机)已经和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(); 
    } 
  } 
} 
</div>

程序界面:

201648155559869.jpg (427×270)

SelectBluetoothDeviceDialog是一个InTheHand.Net.Personal提供的窗体,用于选择蓝牙设备:

201648155651467.jpg (650×495)

从手机往电脑发送文件需要在电脑上开启监听ObexListener,才能收到文件。

201648155708976.jpg (427×270)

核心代码:

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



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Windows系统中使用C#编写蓝牙通信程序的简单实例
  • Windows系统中使用C#读取文本文件内容的小示例

相关文章

  • 2017-05-28C#转换日期类型实例
  • 2017-05-28.NET程序页面中,操作并输入cmd命令的小例子
  • 2017-05-28重温C# clr 笔记总结
  • 2017-05-28使用异步方式调用同步方法(实例详解)
  • 2017-05-28C#制作简单的多人在线即时交流聊天室
  • 2017-05-28WinForm实现按名称递归查找控件的方法
  • 2017-05-28C#执行DOS命令的方法
  • 2017-05-28C# 设计模式系列教程-桥接模式
  • 2017-05-28c#对象反序列化与对象序列化示例详解
  • 2017-05-28C#怎么给PDF添加背景图片

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 解析C#中#region与#if的作用
    • C#函数式编程中的递归调用之尾递归详解
    • WPF MVVM示例讲解
    • C#多线程学习之(四)使用线程池进行多线程的自动管理
    • C#中遍历Hashtable的4种方法
    • C#流类FileStream学习使用笔记
    • DevExpress实现根据行,列索引来获取RepositoryItem的方法
    • C#实现字体旋转的方法
    • 在C#使用字典存储事件示例及实现自定义事件访问器
    • 浅谈C#中的for循环与foreach循环

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有