Exemplo n.º 1
0
        //----

        internal string CreatePort(BluetoothAddress addr)
        {
            if (IsSet(_waitConnect))
            {
                throw new InvalidOperationException("Already used.");
            }
            _addr = addr;
            byte[] bd_addr           = WidcommUtils.FromBluetoothAddress(addr);
            byte[] tcharzServiceName = { 0, 0 };
            var    inUse             = Interlocked.CompareExchange(ref _sfInConnect, 1, 0);

            if (inUse != 0)
            {
                throw new InvalidOperationException("Widcomm only allows one SPP Connect attempt at a time.");
            }
            SPP_CLIENT_RETURN_CODE ret = (SPP_CLIENT_RETURN_CODE)(-1);

            _singleThreader.AddCommand(new WidcommPortSingleThreader.MiscNoReturnCommand(
                                           () => ret = NativeMethods.SppClient_CreateConnection(
                                               _pSppCli, bd_addr, tcharzServiceName)
                                           )).WaitCompletion();
            Debug.WriteLine("SppClient_CreateConnection ret: " + ret);
            int  timeout   = 30000;
            bool signalled = _waitConnect.WaitOne(timeout, false);

            // Eeek want to set this even when we got NO callback........
            // Do for now because Win32 is not working at all......
            Interlocked.Exchange(ref _sfInConnect, 0);
            if (!signalled)
            {
                throw CommonSocketExceptions.Create_NoResultCode(
                          WidcommSppSocketExceptions.SocketError_Misc,
                          "CreatePort failed (time-out).");
            }
            MemoryBarrier();
            if (_statusState != SPP_STATE_CODE.CONNECTED)
            {
                throw WidcommSppSocketExceptions.Create(_statusState, "CreatePort");
            }
            if (_statusComPort == null)
            {
                throw CommonSocketExceptions.Create_NoResultCode(
                          WidcommSppSocketExceptions.SocketError_Misc,
                          "CreatePort did not complete (cpn).");
            }
            //
            // TODO Move these into the native-event handler.
            //string comPort = MakePortName(_statusComPort.Value);
            //_comNum = _statusComPort.Value;
            //_comPortName = comPort;
            Debug.Assert(_comPortName != null, "_comPortName IS null");
            Debug.Assert(WidcommUtils.ToBluetoothAddress(_statusBda) == addr,
                         "addr NOT equal, is: " + WidcommUtils.ToBluetoothAddress(_statusBda));
            return(_comPortName);
        }
Exemplo n.º 2
0
        //----
        tBT_CONN_STATS GetConnectionStats()
        {
            tBT_CONN_STATS         stats = new tBT_CONN_STATS();
            SPP_CLIENT_RETURN_CODE ret   = _singleThreader.AddCommand(
                new WidcommPortSingleThreader.MiscReturnCommand <SPP_CLIENT_RETURN_CODE>(
                    () => NativeMethods.SppClient_GetConnectionStats(_pSppCli,
                                                                     out stats, Marshal.SizeOf(stats))
                    )).WaitCompletion();

            if (ret != SPP_CLIENT_RETURN_CODE.SUCCESS)
            {
                throw WidcommSppSocketExceptions.Create(ret, "GetConnectionStats");
            }
            return(stats);
        }