Unable to establish connection using Windows 7 and C#

Topics: Bluetooth - Microsoft
Apr 12, 2015 at 7:19 PM
Edited Apr 16, 2015 at 4:29 PM
Intro
I am getting the follow error:
System.ArgumentException was unhandled
HResult=-2147024809
Message=The IAsyncResult object was not returned from the corresponding asynchronous method on this class.
Parameter name: asyncResult
Source=System
ParamName=asyncResult
StackTrace:
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.EndConnect(IAsyncResult asyncResult)
   at InTheHand.Net.Sockets.BluetoothClient.EndConnect(IAsyncResult asyncResult)
   at myApp_1.frmMT.ConnectMethod(IAsyncResult asyncResult) in c:\xxx\Source\Workspaces\MT\MT_1\MTx_1\MTy_1\Form1.cs:line 194
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.ContextAwareResult.CompleteCallback(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.ContextAwareResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.Sockets.Socket.ConnectCallback()
   at System.Net.Sockets.Socket.RegisteredWaitCallback(Object state, Boolean timedOut)
   at System.Threading._ThreadPoolWaitOrTimerCallback.PerformWaitOrTimerCallback(Object state, Boolean timedOut)
Connection:
       BluetoothClient myClient = new BluetoothClient();

        char[] pinchar = new char[] { 'M', 'y', 'P', 'i', 'n' };
        String pinstring = new string(pinchar);
        Comments.AppendText("Device Address: " + device.DeviceAddress + NewLine);
        isPaired = BluetoothSecurity.PairRequest(device.DeviceAddress, pinstring);
        Comments.AppendText("isPaired?: " + isPaired.ToString() + NewLine);  // This is true!
        Comments.AppendText("isAuthenticated?: " + device.Authenticated + NewLine);  // This is true!
        if (isPaired)
        {
             if (device.Authenticated)
             {
                //asyncconnection method
                btClient.BeginConnect(device.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(ConnectMethod), device);
                Comments.AppendText("Balance Connected? : " + btClient.Connected + NewLine); // This is false!
             }
        }
                    else
                    {
                        // pairing failed
                        Comments.AppendText("Balance Pairing Failed :( (" + pinstring + ")" + NewLine);  // never get here!
                    }
*** just as an FYI - I have also tried connecting via the EndPoint using mac address and guid.

Asynch Method:
private void ConnectMethod(IAsyncResult myAsyncResult)
{
    MessageBox.Show(myAsyncResult.ToString());  // This show off the method

    if (myAsyncResult.IsCompleted)  // This is true!
    {
        //  This where the error is thrown
        myClient.EndConnect(myAsyncResult);  // Error here on line 194
        // Stream myStream = myClient.GetStream();  // This also fails with same error
        MessageBox.Show("Connected? : " + myClient.Connected);  // Never get here or if I do myClient.Connected is always false!
     }
}
Possible related info:

Manufacturer says:
Bluetooth version: 1.2
Protocols: L2CAP, SDP, RFCOMM
Bluetooth Profile: Generic Access, Service Discovery, Serial Port Profile (SPP)
Role of BT Option: Client or Server for all peripherals

Any help greatly appreciated!