PC蓝牙通信C#代码实现
作者:名字好难 时间:2023-07-06 19:59:32
本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下
添加引用InTheHand.Net.Personal.dll
首先创建一个蓝牙类
class LanYa {
public string blueName { get; set; } //l蓝牙名字
public BluetoothAddress blueAddress { get; set; } //蓝牙的唯一标识符
public ClassOfDevice blueClassOfDevice { get; set; } //蓝牙是何种类型
public bool IsBlueAuth { get; set; } //指定设备通过验证
public bool IsBlueRemembered { get; set; } //记住设备
public DateTime blueLastSeen { get; set; }
public DateTime blueLastUsed { get; set; }
}
然后就是搜索设备
List<LanYa> lanYaList = new List<LanYa>(); //搜索到的蓝牙的集合
BluetoothClient client = new BluetoothClient();
BluetoothRadio radio = BluetoothRadio.PrimaryRadio; //获取蓝牙适配器
radio.Mode = RadioMode.Connectable;
BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜索蓝牙 10秒钟
foreach (var item in devices) {
lanYaList.Add(new LanYa { blueName = item.DeviceName, blueAddress = item.DeviceAddress, blueClassOfDevice = item.ClassOfDevice, IsBlueAuth = item.Authenticated, IsBlueRemembered = item.Remembered, blueLastSeen = item.LastSeen, blueLastUsed = item.LastUsed });//把搜索到的蓝牙添加到集合中
}
蓝牙的配对
BluetoothClient blueclient = new BluetoothClient();
Guid mGUID1 = BluetoothService.Handsfree; //蓝牙服务的uuid
blueclient.Connect(s.blueAddress, mGUID) //开始配对 蓝牙4.0不需要setpin
客户端
BluetoothClient bl = new BluetoothClient();//
Guid mGUID2 = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");//蓝牙串口服务的uuiid
try
{
bl.Connect(s.blue_address, mGUID);
//"连接成功";
}
catch(Exception x)
{
//异常
}
var v = bl.GetStream();
byte[] sendData = Encoding.Default.GetBytes(“人生苦短,我用python”);
v.Write(sendData, 0, sendData.Length); //发送
服务器端
bluetoothListener = new BluetoothListener(mGUID2);
bluetoothListener.Start();//开始监听
bl = bluetoothListener.AcceptBluetoothClient();//接收
while (true)
{
byte[] buffer = new byte[100];
Stream peerStream = bl.GetStream();
peerStream.Read(buffer, 0, buffer.Length);
string data= Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");//去掉后面的\0字节
}
基本上就是这些吧!
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android开发之蜂鸣提示音和震动提示的实现原理与参考代码
Java继承的问题引导和测试代码
![](https://img.aspxhome.com/file/2023/8/96078_0s.jpg)
C# Socket连接请求超时机制实现代码分享
java线程池详解及代码介绍
![](https://img.aspxhome.com/file/2023/7/76517_0s.png)
Android添加用户组及自定义App权限的方法
![](https://img.aspxhome.com/file/2023/8/117118_0s.jpg)
浅谈Java继承中的转型及其内存分配
![](https://img.aspxhome.com/file/2023/7/103967_0s.png)
Android AlertDialog的几种用法详解
![](https://img.aspxhome.com/file/2023/2/137692_0s.png)
Java编程关于子类重写父类方法问题的理解
![](https://img.aspxhome.com/file/2023/7/71837_0s.jpg)
Java实现五子棋网络版
![](https://img.aspxhome.com/file/2023/2/63382_0s.png)
Android开发之开发者头条(二)实现左滑菜单
Java中BufferedReader和BufferedWriter使用方式
![](https://img.aspxhome.com/file/2023/9/75779_0s.png)
C# Fiddler插件实现网站离线浏览功能
![](https://img.aspxhome.com/file/2023/8/91348_0s.png)
Java线程同步机制_动力节点Java学院整理
Java毕业设计实战之平行志愿管理系统的实现
![](https://img.aspxhome.com/file/2023/2/74872_0s.jpg)
springboot openfeign从JSON文件读取数据问题
Unity3D实现摄像机镜头移动并限制角度
Android限时抢购倒计时实现代码
![](https://img.aspxhome.com/file/2023/5/137685_0s.jpg)
Springboot快速入门教程
![](https://img.aspxhome.com/file/2023/9/125319_0s.png)