客戶端實(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è)類是 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# Bitmap轉(zhuǎn)bitmapImage高效方法
本文主要介紹了c# Bitmap轉(zhuǎn)bitmapImage高效方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)的滾動(dòng)網(wǎng)頁(yè)截圖功能,結(jié)合具體實(shí)例形式分析了C#圖形操作的相關(guān)技巧,需要的朋友可以參考下2017-07-07Unity實(shí)現(xiàn)多平臺(tái)二維碼掃描
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)多平臺(tái)二維碼掃描,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07unity實(shí)現(xiàn)方向盤(pán)轉(zhuǎn)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)方向盤(pán)轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于C#連接SQL Server數(shù)據(jù)庫(kù)的實(shí)例內(nèi)容,有需要的朋友們參考學(xué)習(xí)下。2020-01-01C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03C#實(shí)現(xiàn)appSettings節(jié)點(diǎn)讀取與修改的方法
這篇文章主要介紹了C#實(shí)現(xiàn)appSettings節(jié)點(diǎn)讀取與修改的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10