zoukankan      html  css  js  c++  java
  • 客户端实现蓝牙接收(C#)

    知识总结发布  (转载)

    网上有关蓝牙接收的资料很多,使用起来也很简单,但是我觉得还是有必要把这些知识总结下来,蓝牙开发需要用到一个第三方的库InTheHand.Net.Personal.dll,感兴趣的朋友可以了解下,或许对你有所帮助
    在实现蓝牙接收时,网上的资料很多,使用起来也很简单,但是我觉得还是有必要把这些知识总结下来。蓝牙开发需要用到一个第三方的库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端谁的也不接,暂时不下结论。
    详细出处参考:http://www.jb51.net/article/33854.htm

  • 相关阅读:
    Docker基础 镜像,容器,仓库核心概念 常用命令和软件安装示例
    JHipster创建微服务及相关微服务架构组件介绍
    PageHelper分页插件及相关案例介绍
    DataTables API及服务端处理模式介绍和后端分页案例
    微服务概念及SpringCloud五大神兽介绍
    GitHub上重要的几个搜索技巧
    Java 内存区域详解
    莫等闲,白了少年头,空悲切!
    解决Mongoose中populate方法导致模板引擎art-template无法渲染的问题,错误-RangeError: Maximum call stack size exceeded
    vscode添加到右键菜单【win10系统】
  • 原文地址:https://www.cnblogs.com/zzu-liulei/p/6077820.html
Copyright © 2011-2022 走看看