首页 技术 正文
技术 2022年11月12日
0 收藏 340 点赞 4,130 浏览 1678 个字

添加引用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, , sendData.Length); //发送

服务器端

 bluetoothListener = new BluetoothListener(mGUID2);
bluetoothListener.Start();//开始监听 bl = bluetoothListener.AcceptBluetoothClient();//接收 while (true)
{
byte[] buffer = new byte[];
Stream peerStream = bl.GetStream(); peerStream.Read(buffer, , buffer.Length); string data= Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");//去掉后面的\0字节
}

基本上就是这些吧!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774