
本文实例为大家分享了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");//蓝牙串口服务的uuiIDtry{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[100];Stream peerStream = bl.GetStream();peerStream.Read(buffer,buffer.Length);string data= EnCoding.UTF8.GetString(buffer).ToString().Replace("","");//去掉后面的字节}基本上就是这些吧!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的PC蓝牙通信C#代码实现全部内容,希望文章能够帮你解决PC蓝牙通信C#代码实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)