国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

PC藍牙通信C#代碼實現(xiàn)

2020-01-24 00:58:45
字體:
供稿:網(wǎng)友

本文實例為大家分享了C#實現(xiàn)PC藍牙通信代碼,供大家參考,具體內(nèi)容如下

添加引用InTheHand.Net.Personal.dll

首先創(chuàng)建一個藍牙類

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);       //發(fā)送

服務器端

bluetoothListener = new BluetoothListener(mGUID2);bluetoothListener.Start();//開始監(jiān)聽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字節(jié)}

基本上就是這些吧!

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 富裕县| 孝感市| 阳西县| 周宁县| 额敏县| 香港 | 黎川县| 浦东新区| 陈巴尔虎旗| 灵武市| 西昌市| 昌都县| 思南县| 江门市| 五大连池市| 卢湾区| 绥滨县| 昭苏县| 昆山市| 连江县| 麟游县| 鱼台县| 屏南县| 麻城市| 监利县| 旺苍县| 博兴县| 白河县| 商水县| 九龙坡区| 拉萨市| 闽清县| 澜沧| 蓬安县| 灯塔市| 马关县| 凯里市| 屯门区| 屯门区| 东莞市| 正镶白旗|