用戶端實現藍芽接收(C#)知識總結

來源:互聯網
上載者:User

在實現藍芽接收時,網上的資料很多,使用起來也很簡單,但是我覺得還是有必要把這些知識總結下來。藍芽開發需要用到一個第三方的庫InTheHand.Net.Personal.dll,其中關鍵的兩個類是 BluetoothClient 和 BluetoothListener,首先開啟一個子線程來不斷的接收資料,使用很簡單,直接上代碼: 複製代碼 代碼如下: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;

備忘:發現用兩個手機跟電腦配對成功後,兩個手機同時連上PC端軟體,一起發資料的話,PC端誰的也不接,暫時不下結論。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.