客户端实现蓝牙接收(C#)知识总结
时间:2021-11-21 22:17:11
在实现蓝牙接收时,网上的资料很多,使用起来也很简单,但是我觉得还是有必要把这些知识总结下来。蓝牙开发需要用到一个第三方的库InTheHand.Net.Personal.dll,其中关键的两个类是 BluetoothClient 和 BluetoothListener,首先开启一个子线程来不断的接收数据,使用很简单,直接上代码:
using InTheHand.Net.Sockets;
using System.Threading;
public MainWindow()
{
InitializeComponent();
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
private void ReceiveData()
{
try
{
Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
isConnected = true;
}
catch (Exception)
{
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
}
BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
备注:发现用两个 * 电脑配对成功后,两个手机同时连上PC端软件,一起发数据的话,PC端谁的也不接,暂时不下结论。
标签:蓝牙,客户端
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解java接口(interface)在不同JDK版本中的变化
2022-07-18 03:19:16
Java8时间接口LocalDateTime详细用法
2023-11-25 09:56:49
SpringBoot项目依赖和配置最新示例讲解
2021-08-13 23:04:23
Java实现Timer的定时调度函数schedule的四种用法
2022-08-14 04:49:01
使用注解解决ShardingJdbc不支持复杂SQL方法
2022-08-03 02:15:12
![](https://img.aspxhome.com/file/2023/5/63795_0s.png)
springcloud 如何解决微服务之间token传递问题
2022-08-27 18:43:48
解决spring boot启动扫描不到自定义注解的问题
2023-10-29 14:31:48
Java实现简易扑克牌游戏的完整实例
2022-10-08 14:09:18
![](https://img.aspxhome.com/file/2023/0/63490_0s.png)
java实现快速打字游戏
2022-11-05 11:16:38
Java基础之反射详解
2022-06-16 12:25:11
![](https://img.aspxhome.com/file/2023/5/61955_0s.png)
SpringBoot2.1.4中的错误处理机制
2023-11-06 02:48:47
![](https://img.aspxhome.com/file/2023/3/58923_0s.jpg)
java web实现简单留言板功能
2021-10-29 17:22:31
基于mybatis plus实现数据源动态添加、删除、切换,自定义数据源的示例代码
2021-08-20 20:07:46
MyBatis中映射文件的使用案例代码
2021-09-02 23:55:41
![](https://img.aspxhome.com/file/2023/0/60910_0s.png)
SpringBoot详细讲解yaml配置文件的用法
2022-04-21 16:29:33
![](https://img.aspxhome.com/file/2023/0/61030_0s.png)
Java字节流和字符流总结IO流!
2023-10-21 13:06:02
![](https://img.aspxhome.com/file/2023/1/74971_0s.png)
SpringBoot中自定义注解实现参数非空校验的示例
2022-04-12 10:55:55
![](https://img.aspxhome.com/file/2023/0/70260_0s.webp)
java多线程和并发包入门示例
2022-05-10 12:29:34
详解Java反射创建对象
2022-12-10 03:52:23
![](https://img.aspxhome.com/file/2023/4/78024_0s.png)
探讨:android项目开发 统筹兼顾 需要考虑的因素
2023-08-05 11:21:32