欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊: 次

本文實例為大家分享了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");//藍牙串口服務的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);       //發(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)容,希望對大家的學習有所幫助,也希望大家多多支持我們。

上一篇:C#與Java的MD5簡單驗證(實例代碼)

欄    目:C#教程

下一篇:C#自定義日志記錄

本文標題:PC藍牙通信C#代碼實現(xiàn)

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6243.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有