示例#1
0
        internal BluetoothHidDevice()
        {
            BluetoothHidDeviceImpl.Instance.ConnectionStateChanged += (s, e) =>
            {
                if (e.Address == RemoteAddress)
                {
                    if (_taskForConnection != null && !_taskForConnection.Task.IsCompleted)
                    {
                        if (e.Result == (int)BluetoothError.None)
                        {
                            _taskForConnection.SetResult(true);
                        }
                        else
                        {
                            _taskForConnection.SetException(BluetoothErrorFactory.CreateBluetoothException(e.Result));
                        }
                    }

                    if (_taskForDisconnection != null && !_taskForDisconnection.Task.IsCompleted)
                    {
                        if (e.Result == (int)BluetoothError.None)
                        {
                            _taskForDisconnection.SetResult(true);
                        }
                        else
                        {
                            _taskForDisconnection.SetException(BluetoothErrorFactory.CreateBluetoothException(e.Result));
                        }
                    }

                    if (e.Result == (int)BluetoothError.None)
                    {
                        /* User does not need 'Result' in HidDeviceConnectionStateChangedEventArgs */
                        ConnectionStateChanged?.Invoke(this, new HidDeviceConnectionStateChangedEventArgs(e.IsConnected, e.Address));
                    }
                }
            };

            BluetoothHidDeviceImpl.Instance.DataReceived += (s, e) =>
            {
                if (e.ReceivedData.Address == RemoteAddress)
                {
                    DataReceived?.Invoke(this, e);
                }
            };
        }
示例#2
0
        private void OnAcceptStateChanged(Object s, AcceptStateChangedEventArgs e)
        {
            if (e.Connection.ServerFd == socketFd)
            {
                if (_taskForAccept != null && !_taskForAccept.Task.IsCompleted)
                {
                    if (e.State == BluetoothSocketState.Connected)
                    {
                        _taskForAccept.SetResult(e.Connection);
                    }
                    else
                    {
                        _taskForAccept.SetException(BluetoothErrorFactory.CreateBluetoothException((int)e.Result));
                    }
                    _taskForAccept = null;
                }

                AcceptStateChanged?.Invoke(this, e);
            }
        }
示例#3
0
        private void OnConnectionChanged(object s, AvrcpControlConnectionChangedEventArgs e)
        {
            if (e.RemoteAddress != RemoteAddress)
            {
                return;
            }

            _isConnected = e.IsConnected;

            if (_taskForConnection != null && !_taskForConnection.Task.IsCompleted)
            {
                if (e.IsConnected == true)
                {
                    _taskForConnection.SetResult(true);
                    ConnectionStateChanged?.Invoke(this, e);
                }
                else
                {
                    _taskForConnection.SetException(BluetoothErrorFactory.CreateBluetoothException((int)BluetoothError.OperationFailed));
                }
                _taskForConnection = null;
                return;
            }
            if (_taskForDisconnection != null && !_taskForDisconnection.Task.IsCompleted)
            {
                if (e.IsConnected == false)
                {
                    _taskForDisconnection.SetResult(true);
                    ConnectionStateChanged?.Invoke(this, e);
                }
                else
                {
                    _taskForDisconnection.SetException(BluetoothErrorFactory.CreateBluetoothException((int)BluetoothError.OperationFailed));
                }
                _taskForDisconnection = null;
            }
        }