title: H7 Heart Rate Sensor in C# for Windows Desktop
body: <p>Specifically, I need to develop a Desktop app that pulls data from H7 Heart Rate Sensor in C# code.
  I have searched everywhere and i cant find nothing to help me.
  The closest i cam is with this video (<a href="https://www.youtube.com/watch?v=Jn05CU3mxzo&amp;list=UUizfLH6Q2igGUyTWO1bH3YA" rel="nofollow">https://www.youtube.com/watch?v=Jn05CU3mxzo&amp;list=UUizfLH6Q2igGUyTWO1bH3YA</a>) tutorial but still it didn't find my H7 Heart Rate Sensor.</p>
  
  <pre><code>namespace _123
  {
      public partial class Form1 : Form
      {
          List&lt;string&gt; items;
          public Form1()
          {
              InitializeComponent();
  
          items = new List&lt;string&gt;();
  
      }
  
      private void bGo_Click(object sender, EventArgs e)//button
      {
          if (serverStarted)
          {
              updateUI("server aleredy started");
              return;
          }
          if (rbClient.Checked) {
              startScan();
          }
          else{
          connectAsServer();
          }
      }
  
      private void startScan() {
          listBox1.DataSource = null;
          listBox1.Items.Clear();
          items.Clear();
          Thread bluetoothScanThread =  new Thread(new ThreadStart(scan));
          bluetoothScanThread.Start();
      }
  
      BluetoothDeviceInfo[] devices;
      private void scan ()
      {
  
          updateUI("Starting scan...");
          BluetoothClient client = new BluetoothClient();
         devices = client.DiscoverDevicesInRange();
          updateUI("Scan complet");
          updateUI(devices.Length.ToString() + "devices discovered");
          foreach (BluetoothDeviceInfo d in devices)
          {
              items.Add(d.DeviceName);
          }
          updateDeviceList();
      }
  
      private void connectAsServer()
      {
  
  
          Thread bluetoothServerTherad = new Thread(new ThreadStart(ServerConnectThread));
          bluetoothServerTherad.Start();
      }
  
      private void connectAsClient()
      {
  
  
  
      }
  
      Guid mUUID = new Guid("ECC037FD-72AE-AFC5-9213-CA785B3B5C63");
      bool serverStarted = false;
      private void ServerConnectThread()
      {
          //throw new NotImplementedException();
          serverStarted = true;
          updateUI("Server started, waiting for clients");
          BluetoothListener blueListener = new BluetoothListener(mUUID);
          blueListener.Start();
          BluetoothClient conn = blueListener.AcceptBluetoothClient();
          updateUI("Client has connected");
  
          Stream mStream = conn.GetStream();
          while (true)
          {
              try
              {
                  byte[] received = new byte[1024];
                  mStream.Read(received, 0, received.Length);
                  updateUI("Received: " + Encoding.ASCII.GetString(received));
                  byte[] sent = Encoding.ASCII.GetBytes("hello world");
                  mStream.Write(sent, 0, sent.Length);
              }
              catch (IOException exeption)
              {
                  updateUI("Client has disconected!");
              }
  
              }
  
      }
  
      private void updateUI(string message) 
      {
          Func&lt;int&gt; del = delegate()
         {
             tbOutput.AppendText(message + System.Environment.NewLine);
             return 0;
  
         };
          Invoke(del);
  
      }
  
      private void updateDeviceList()
      {
          Func&lt;int&gt; del = delegate()
          {
              listBox1.DataSource = items;
              return 0;
  
          };
          Invoke(del);
      }
  
      BluetoothDeviceInfo deviceInfo;
      private void listBox1_DoubleClick(object sender, EventArgs e)
      {
          deviceInfo = devices.ElementAt(listBox1.SelectedIndex);
          updateUI(deviceInfo.DeviceName + " was selected, attemting connect");
  
          if (pairDevice())
          {
              updateUI("device paired..");
              updateUI("starting conected thread");
              Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
              bluetoothClientThread.Start();
          }
          else
          {
              updateUI("pair failed");
          }
  
      }
  
      private void ClientConnectThread()
      {
          BluetoothClient client = new BluetoothClient();
          updateUI("attepting connnection");
          client.BeginConnect(deviceInfo.DeviceAddress, mUUID, this.BluetoothClientConnectCallback, client);
  
      }
  
      void BluetoothClientConnectCallback(IAsyncResult result)
      {
          BluetoothClient client = (BluetoothClient)result.AsyncState;
          client.EndConnect(result);
  
          Stream stream = client.GetStream();
          stream.ReadTimeout = 1000;
  
          while (true)
          {
              while (!ready) ;
              stream.Write(message, 0, message.Length);
          }
  
      }
  
      string myPin = "1234";
      private bool pairDevice()
      {
          if (!deviceInfo.Authenticated)
          {
              if (!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress, myPin))
              {
                  return false;
              }
          }
          return true;
      }
  
      bool ready = false;
      byte[] message;
      private void tbText_KeyPress(object sender, KeyPressEventArgs e)
      {
          if (e.KeyChar == 13)
          {
              message = Encoding.ASCII.GetBytes(tbText.Text);
              ready = true;
              tbText.Clear();
          }
  
      }
    }
  }
  </code></pre>