PC藍牙通訊C#代碼完成。本站提示廣大學習愛好者:(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");//藍牙串口辦事的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().WordStr("\0", "");//去失落前面的\0字節 }
根本上就是這些吧!
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。