示例#1
0
        public async Task ConnectToDeviceAsync(IDevice device, ConnectParameters connectParameters = default(ConnectParameters), CancellationToken cancellationToken = default(CancellationToken))
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            if (device.State == DeviceState.Connected)
            {
                return;
            }

            using (var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                await TaskBuilder.FromEvent <bool, EventHandler <DeviceEventArgs>, EventHandler <DeviceErrorEventArgs> >(
                    execute : () =>
                {
                    ConnectToDeviceNativeAsync(device, connectParameters, cts.Token);
                },

                    getCompleteHandler : (complete, reject) => (sender, args) =>
                {
                    if (args.Device.Id == device.Id)
                    {
                        Trace.Message("ConnectToDeviceAsync Connected: {0} {1}", args.Device.Id, args.Device.Name);
                        complete(true);
                    }
                },
                    subscribeComplete : handler => DeviceConnected   += handler,
                    unsubscribeComplete : handler => DeviceConnected -= handler,

                    getRejectHandler : reject => (sender, args) =>
                {
                    if (args.Device?.Id == device.Id)
                    {
                        Trace.Message("ConnectAsync Error: {0} {1}", args.Device?.Id, args.Device?.Name);
                        reject(new DeviceConnectionException((Guid)args.Device?.Id, args.Device?.Name,
                                                             args.ErrorMessage));
                    }
                },

                    subscribeReject : handler => DeviceConnectionError   += handler,
                    unsubscribeReject : handler => DeviceConnectionError -= handler,
                    token : cts.Token);
            }
        }
示例#2
0
 public abstract Task <IDevice> ConnectToKnownDeviceAsync(Guid deviceGuid, ConnectParameters connectParameters = default(ConnectParameters), CancellationToken cancellationToken = default(CancellationToken));
示例#3
0
 protected abstract Task ConnectToDeviceNativeAsync(IDevice device, ConnectParameters connectParameters, CancellationToken cancellationToken);