亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2016年09月28日 15:16:04   作者:名字好難  
這篇文章主要為大家詳細(xì)介紹了PC藍(lán)牙通信C#代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

添加引用InTheHand.Net.Personal.dll

首先創(chuàng)建一個(gè)藍(lán)牙類

class LanYa {
public string blueName { get; set; }                  //l藍(lán)牙名字
public BluetoothAddress blueAddress { get; set; }        //藍(lán)牙的唯一標(biāo)識(shí)符
public ClassOfDevice blueClassOfDevice { get; set; }      //藍(lán)牙是何種類型
public bool IsBlueAuth { get; set; }                  //指定設(shè)備通過(guò)驗(yàn)證
public bool IsBlueRemembered { get; set; }            //記住設(shè)備
public DateTime blueLastSeen { get; set; }
public DateTime blueLastUsed { get; set; }
}
 

然后就是搜索設(shè)備

List<LanYa> lanYaList = new List<LanYa>();        //搜索到的藍(lán)牙的集合
BluetoothClient client = new BluetoothClient();      
BluetoothRadio radio = BluetoothRadio.PrimaryRadio; //獲取藍(lán)牙適配器
radio.Mode = RadioMode.Connectable;            
BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜索藍(lán)牙 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 });//把搜索到的藍(lán)牙添加到集合中
}
 

藍(lán)牙的配對(duì)

BluetoothClient blueclient = new BluetoothClient();
Guid mGUID1 = BluetoothService.Handsfree;    //藍(lán)牙服務(wù)的uuid
 
blueclient.Connect(s.blueAddress, mGUID)   //開(kāi)始配對(duì) 藍(lán)牙4.0不需要setpin
 

客戶端

BluetoothClient bl = new BluetoothClient();//
Guid mGUID2 = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");//藍(lán)牙串口服務(wù)的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ā)送

服務(wù)器端

bluetoothListener = new BluetoothListener(mGUID2);
bluetoothListener.Start();//開(kāi)始監(jiān)聽(tīng)

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é)
}

基本上就是這些吧!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論