title: Sending data from android phone to PC by bluetooth using C#
body: <p>I haven't asked many questions on here so please forgive me as I am still learning all the conventions of posting. </p>
  
  <p>I am trying to build an Android mobile app, that will allow the user to control a PC mouse via the touch screen over bluetooth. The plan is to have a C# program on the PC which will recognize the bluetooth connection, receive the data (x and y coordinates) from the phone and translate the data into mouse movement (If anyone has any other suggestions as how to better translate the data I would also love to hear them). This is where the problems start. I am very new to Android development and have no experience with bluetooth. I have built the app and can track the movement from the screen but I cannot get the phone to connect to the bluetooth dongle of the PC. I'm using the 32feet to try and connect to the phone.</p>
  
  <p>The errors I am receiving are:
  "System.InvalidOperationException: The operation is not allowed to non-connected sockets" - which i think is being thrown because my bluetooth client (PC) is not connecting to the bluetooth device (phone), which i think is due to this line:</p>
  
  <pre><code>BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
  </code></pre>
  
  <p>but I am not sure why.</p>
  
  <p>Below is my C# class, I would be grateful for any feedback or suggestions:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using InTheHand.Net.Sockets;
  using InTheHand.Net;
  using InTheHand.Net.Bluetooth;
  using InTheHand.Windows.Forms;
  using System.Net.Sockets;
  using System.Diagnostics;
  using System.Threading;
  
  namespace bluetooth_connectivity_test
  {
  static class Program
      {
      static string MY_PAIRING_CODE = "1234";
      static string UUID = "00001101-0000-1000-8000-00805F9B34FB";
  
      //dongle
      private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("00:1A:7D:DA:71:13"), BluetoothService.SerialPort);
  
      //BC used to manage connections;
      private static BluetoothClient BC = new BluetoothClient(EP);
  
      //phone
      private static BluetoothDeviceInfo BTDevice = new BluetoothDeviceInfo(BluetoothAddress.Parse("04:1B:BA:31:0A:CB"));
      private static NetworkStream stream = null;
  
      static void Main(string[] args)
      {
          Console.WriteLine(BTDevice.DeviceName + " is a " + BTDevice.ClassOfDevice.MajorDevice.ToString());
  
          bool is_paired = false;
          is_paired = BluetoothSecurity.PairRequest(BTDevice.DeviceAddress, MY_PAIRING_CODE);
  
          if (is_paired)
          {
              Console.WriteLine("PairRequest: OK");
  
              if (BTDevice.Authenticated)
              {
                  Console.WriteLine("Authenticated: OK");
  
                  BC.SetPin(UUID);
  //
                  BC.BeginConnect(BTDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), BTDevice);
  //
                  Console.WriteLine("Decive Name: " + BTDevice.DeviceName);
                  Console.WriteLine("Device Connected: " + BTDevice.Connected);
  
              }
              else
              {
                  Console.WriteLine("Authenticated: No");
              }
          }
          else
          {
              Console.WriteLine("PairRequest: No");
          }
  
          Console.ReadLine();
      }
  
      private static void Connect(IAsyncResult result)
      {
          if (result.IsCompleted)
          {
              try
              {
                  Console.WriteLine("Bluetooth Client Connected: " +BC.Connected);
                  //crash because stream = null
                  stream = BC.GetStream();
              }
              catch(InvalidOperationException e)
              {
                  Console.WriteLine(e);
              }
  
              if (stream.CanRead)
              {
                  byte[] myReadBuffer = new byte[1024];
                  StringBuilder myCompleteMessage = new StringBuilder();
                  int numberOfBytesRead = 0;
  
                  // Incoming message may be larger than the buffer size. 
                  do
                  {
                      numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);
  
                      //right
                      for (int i = 0; i &lt; numberOfBytesRead; i++)
                      {
                          myCompleteMessage.AppendFormat("0x{0:X2} ", myReadBuffer[i]);
                      }
                  }
  
                  while (stream.DataAvailable);
  
                  // Print out the received message to the console.
                  Console.WriteLine("You received the following message : " + myCompleteMessage);
              }
              else
              {
                  Console.WriteLine("Sorry.  You cannot read from this NetworkStream.");
              }
              Console.ReadLine();
              BC.Close();
          }
      }
  }
  }
  </code></pre>
  
  <p>On the console i get:</p>
  
  <pre><code>name of the device and device type (as a test)
  Pair request: 'ok'
  authenticated: 'ok'
  device name: shows the name fine
  device connected: true
  bluetooth client connected: false
  </code></pre>
  
  <p>Edit: I should probably add that I have spent the last 3 days looking at everything from Stackoverflow, to the 32feet wiki and home page as well as MSDN and other peoples personal project pages but have found nothing that has worked. But as I say, I don't have any experience working with bluetooth so I've been trying differen't pieces of code but I'm not sure exactly what I'm meant to be looking for.</p>