客戶端實(shí)現(xiàn)藍(lán)牙接收(C#)知識(shí)總結(jié)
更新時(shí)間:2013年02月06日 15:16:37 作者:
網(wǎng)上有關(guān)藍(lán)牙接收的資料很多,使用起來(lái)也很簡(jiǎn)單,但是我覺(jué)得還是有必要把這些知識(shí)總結(jié)下來(lái),藍(lán)牙開(kāi)發(fā)需要用到一個(gè)第三方的庫(kù)InTheHand.Net.Personal.dll,感興趣的朋友可以了解下,或許對(duì)你有所幫助
在實(shí)現(xiàn)藍(lán)牙接收時(shí),網(wǎng)上的資料很多,使用起來(lái)也很簡(jiǎn)單,但是我覺(jué)得還是有必要把這些知識(shí)總結(jié)下來(lái)。藍(lán)牙開(kāi)發(fā)需要用到一個(gè)第三方的庫(kù)InTheHand.Net.Personal.dll,其中關(guān)鍵的兩個(gè)類(lèi)是 BluetoothClient 和 BluetoothListener,首先開(kāi)啟一個(gè)子線程來(lái)不斷的接收數(shù)據(jù),使用很簡(jiǎn)單,直接上代碼:
using InTheHand.Net.Sockets;
using System.Threading;
public MainWindow()
{
InitializeComponent();
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
private void ReceiveData()
{
try
{
Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
isConnected = true;
}
catch (Exception)
{
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
}
BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
備注:發(fā)現(xiàn)用兩個(gè)手機(jī)跟電腦配對(duì)成功后,兩個(gè)手機(jī)同時(shí)連上PC端軟件,一起發(fā)數(shù)據(jù)的話,PC端誰(shuí)的也不接,暫時(shí)不下結(jié)論。
復(fù)制代碼 代碼如下:
using InTheHand.Net.Sockets;
using System.Threading;
public MainWindow()
{
InitializeComponent();
listenThread = new Thread(ReceiveData);
listenThread.Start();
}
private void ReceiveData()
{
try
{
Guid mGUID = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");
bluetoothListener = new BluetoothListener(mGUID);
bluetoothListener.Start();
bluetoothClient = bluetoothListener.AcceptBluetoothClient();
isConnected = true;
}
catch (Exception)
{
isConnected = false;
}
while (isConnected)
{
string receive = string.Empty;
if (bluetoothClient == null)
{
break;
}
try
{
peerStream = bluetoothClient.GetStream();
byte[] buffer = new byte[6];
peerStream.Read(buffer, 0, 6);
receive = Encoding.UTF8.GetString(buffer).ToString();
}
catch (System.Exception)
{
}
Thread.Sleep(100);
}
}
BluetoothClient bluetoothClient;
BluetoothListener bluetoothListener;
Thread listenThread;
bool isConnected;
備注:發(fā)現(xiàn)用兩個(gè)手機(jī)跟電腦配對(duì)成功后,兩個(gè)手機(jī)同時(shí)連上PC端軟件,一起發(fā)數(shù)據(jù)的話,PC端誰(shuí)的也不接,暫時(shí)不下結(jié)論。
相關(guān)文章
c#使用IAsyncEnumerable實(shí)現(xiàn)流式分段傳輸
這篇文章主要為大家詳細(xì)介紹了c#如何使用IAsyncEnumerable實(shí)現(xiàn)流式分段傳輸,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
基于C#技術(shù)實(shí)現(xiàn)身份證識(shí)別功能
這篇文章主要介紹了基于C#技術(shù)實(shí)現(xiàn)身份證識(shí)別功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
C#仿密??üδ艿暮?jiǎn)單實(shí)現(xiàn)代碼
昨天拿C#寫(xiě)了個(gè)簡(jiǎn)單的密??ǔ绦颍–onsole的,偷懶了一下 哈哈),實(shí)現(xiàn)了隨機(jī)生成5x5矩陣卡、轉(zhuǎn)換為字符串、從字符串讀取矩陣卡以及簡(jiǎn)單驗(yàn)證的功能2013-04-04
WPF+SkiaSharp實(shí)現(xiàn)自繪投籃小游戲
這篇文章主要介紹了如何利用WPF+SkiaSharp實(shí)現(xiàn)自繪投籃小游戲。此案例主要是針對(duì)光線投影法碰撞檢測(cè)功能的示例,順便做成了一個(gè)小游戲,很簡(jiǎn)單,但是,效果卻很不錯(cuò),感興趣的可以動(dòng)手嘗試一下2022-08-08
C#控制IE進(jìn)程關(guān)閉和緩存清理的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#控制IE進(jìn)程關(guān)閉和緩存清理的實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-04-04

