服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Windows系统中使用C#编写蓝牙通信程序的简单实例

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

2021-11-18 11:34hzy3774 C#

这篇文章主要介绍了Windows系统中使用C#编写蓝牙通信程序的简单实例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal类库,需要的朋友可以参考下

现在很多电脑提供了蓝牙支持,很多笔记本网卡也集成了蓝牙功能,也可以采用usb蓝牙方便的连接手机等蓝牙设备进行通信。
操作蓝牙要使用类库inthehand.net.personal
首先在项目中引用该类库;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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插入了蓝牙适配器,便会显示蓝牙相关信息:

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

 然后我们就要利用蓝牙收发文件了:
前提是蓝牙设备(如手机)已经和pc配对了

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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();
    }
  }
}

程序界面:

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

selectbluetoothdevicedialog是一个inthehand.net.personal提供的窗体,用于选择蓝牙设备:

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

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

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

核心代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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~

延伸 · 阅读

精彩推荐
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28