Ejemplo n.º 1
0
        protected override void DoOpenClient(int scn, BluetoothAddress addressToConnect)
        {
            _fcty.CancelAllQueryNames();
            var addrX = BluetopiaUtils.BluetoothAddressAsInteger(addressToConnect);
            int hConn = _fcty.Api.SPP_Open_Remote_Port(_fcty.StackId, addrX, (uint)scn,
                                                       _callbackAAAA, 0);
            var ret = (BluetopiaError)hConn;
            int i;

            for (i = 0; i < 5 && ret == BluetopiaError.RFCOMM_UNABLE_TO_CONNECT_TO_REMOTE_DEVICE; ++i)
            {
                // Sometimes see this error here, my guess is that the baseband
                // connection used by the SDP Query is closing right when as we
                // wanted to connect, so we fail to initiate the connect.  In
                // the debugger when we retry it succeeds, so retry.
                if (i > 0)
                {
                    Thread.Sleep(100);        // Try right away, then after 100ms sleeps
                }
                hConn = _fcty.Api.SPP_Open_Remote_Port(_fcty.StackId, addrX, (uint)scn,
                                                       _callbackAAAA, 0);
                ret = (BluetopiaError)hConn;
            }
            if (i > 0)
            {
                Debug.WriteLine("Auto-retry " + i + " after RFCOMM_UNABLE_TO_CONNECT_TO_REMOTE_DEVICE for SPP_Open_Remote_Port");
            }
            BluetopiaUtils.CheckAndThrow(ret, "SPP_Open_Remote_Port");
            _hPortClient = checked ((uint)ret);
        }
Ejemplo n.º 2
0
        internal IAsyncResult BeginQuery(BluetoothAddress device, Guid svcClass,
                                         bool rfcommOnly,
                                         AsyncCallback asyncCallback, object state)
        {
            int id0 = Interlocked.Increment(ref _callbackCount);
            var id  = unchecked ((uint)id0);
            var ar  = new AsyncResultGsr(asyncCallback, state, id);

            // Attribute range/etc
            Structs.SDP_Attribute_ID_List_Entry[] attrIdListArr;
            var attrIdAll     = Structs.SDP_Attribute_ID_List_Entry.CreateRange(0, 0xFFFF);
            var attrIdSvcList = Structs.SDP_Attribute_ID_List_Entry.CreateItem(
                UniversalAttributeId.ServiceClassIdList);
            var attrIdProto = Structs.SDP_Attribute_ID_List_Entry.CreateItem(
                UniversalAttributeId.ProtocolDescriptorList);

            if (rfcommOnly)
            {
                attrIdListArr = new[] { attrIdSvcList, attrIdProto };
            }
            else
            {
                attrIdListArr = new[] { attrIdAll };
            }
            // UUID
            var uuidArg = new Structs.SDP_UUID_Entry(svcClass);

            Structs.SDP_UUID_Entry[] uuidArr = new[] { uuidArg };
            AsyncResultGsr           old;

            old = MyInterlockedCompareExchange <AsyncResultGsr>(ref _ar, ar, null);
            if (old != null)
            {
                throw new NotImplementedException("One at a time please.");
            }
            bool trySuccess = false;

            try {
                // Call
                _fcty.CancelAllQueryNames();
                int hRequest = _fcty.Api.SDP_Service_Search_Attribute_Request(_fcty.StackId,
                                                                              BluetopiaUtils.BluetoothAddressAsInteger(device),
                                                                              (uint)uuidArr.Length, uuidArr,
                                                                              (uint)attrIdListArr.Length, attrIdListArr,
                                                                              _callback, id);
                var ret = (BluetopiaError)hRequest;
                int i;
                for (i = 0; i < 5 && ret == BluetopiaError.ATTEMPTING_CONNECTION_TO_DEVICE; ++i)
                {
                    // See BluetopiaRfcommStream.DoOpenClient... Assuming here a
                    // previous baseband connection was closing right when as we
                    // wanted to connect, so retry.
                    if (i > 0)
                    {
                        Thread.Sleep(100);        // Try right away, then after 100ms sleeps
                    }
                    hRequest = _fcty.Api.SDP_Service_Search_Attribute_Request(_fcty.StackId,
                                                                              BluetopiaUtils.BluetoothAddressAsInteger(device),
                                                                              (uint)uuidArr.Length, uuidArr,
                                                                              (uint)attrIdListArr.Length, attrIdListArr,
                                                                              _callback, id);
                    ret = (BluetopiaError)hRequest;
                }
                if (i > 0)
                {
                    Debug.WriteLine("Auto-retry " + i + " after ATTEMPTING_CONNECTION_TO_DEVICE for SDP_Service_Search_Attribute_Request");
                }
                BluetopiaUtils.CheckAndThrowZeroIsIllegal(ret, "SDP_Service_Search_Attribute_Request");
                trySuccess = true;
                return(ar);
            } finally {
                if (!trySuccess)
                {
                    // If we got here it must be our value in the variable!
                    var replaced = MyInterlockedExchange(ref _ar, null);
                    Debug.Assert(replaced != null);
                    Debug.Assert(replaced == ar);
                }
            }
        }