a
PC蓝牙通信C#代码实现

阅0转02017-12-07

  PC蓝牙通信C#代码实现这篇文章主要为大家详细介绍了PC蓝牙通信C#代码实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文实例为大家分享了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, 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字节}基本上就是这些吧!
来自:15879434608  > 待分类
举报
猜你喜欢
类似文章
UBOOT 内存映射以及修改问题
用蓝牙把手机文件发送到电脑------发word文档
BL之我
【新提醒】完成BL
BL
bl
更多类似文章 >>
生活服务
热门推广