PC藍(lán)牙通信C#代碼實(shí)現(xiàn)
本文實(shí)例為大家分享了C#實(shí)現(xiàn)PC藍(lán)牙通信代碼,供大家參考,具體內(nèi)容如下
添加引用InTheHand.Net.Personal.dll
首先創(chuàng)建一個(gè)藍(lán)牙類(lèi)
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)牙是何種類(lèi)型
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í)有所幫助,也希望大家多多支持我們。
上一篇:C#與Java的MD5簡(jiǎn)單驗(yàn)證(實(shí)例代碼)
欄 目:C#教程
下一篇:C#自定義日志記錄
本文標(biāo)題:PC藍(lán)牙通信C#代碼實(shí)現(xiàn)
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6243.html
您可能感興趣的文章
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
 - 01-10C#實(shí)現(xiàn)子窗體與父窗體通信方法實(shí)例總結(jié)
 - 01-10C#獲取網(wǎng)頁(yè)源代碼的方法
 - 01-10C#中Socket通信用法實(shí)例詳解
 - 01-1010個(gè)C#程序員經(jīng)常用到的實(shí)用代碼片段
 - 01-10C#導(dǎo)出網(wǎng)站功能實(shí)例代碼講解
 - 01-10C#代碼實(shí)現(xiàn)PDF文檔操作類(lèi)
 - 01-10C#超實(shí)用代碼段合集
 - 01-10分享WCF聊天程序--WCFChat實(shí)現(xiàn)代碼
 - 01-10Silverlight將圖片轉(zhuǎn)換為byte的實(shí)現(xiàn)代碼
 


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
 - 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
 - 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
 - 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
 - 5c語(yǔ)言計(jì)算三角形面積代碼
 - 6什么是 WSH(腳本宿主)的詳細(xì)解釋
 - 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
 - 8正則表達(dá)式匹配各種特殊字符
 - 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
 - 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
 
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
 - 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
 - 01-10C#中split用法實(shí)例總結(jié)
 - 01-10delphi制作wav文件的方法
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
 - 04-02jquery與jsp,用jquery
 


