Пример #1
0
        public async Task <byte> ConnectionDisconnectAsync(byte connectionHandle)
        {
            const int TIMEOUT_MS = 6000;

            var cmd = _bglib.BLECommandConnectionDisconnect(connectionHandle);

            await _bglib.SendCommandAsync(cmd).ConfigureAwait(false);

            using (var cts = new CancellationTokenSource(TIMEOUT_MS)) {
                bool didTimeout = false;
                try { await _cmdRespWaitHandle.WaitAsync(cts.Token).ConfigureAwait(false); }
                catch (OperationCanceledException) { didTimeout = true; }
                if (didTimeout)
                {
                    throw new TimeoutException();
                }
            }

            if (_lastResult != 0)
            {
                var    error  = Enum.ToObject(typeof(BGErrorCode), _lastResult);
                string errMsg = $"ConnectionDisconnect returned: error={error} code={_lastResult:X4}";
                throw new Exception(errMsg);
            }

            byte conHandle = ((DisconnectEventArgs)_lastResponseArgs).connection;

            return(conHandle);
        }