Ejemplo n.º 1
0
        public override IObservable <DescriptorResult> Write(byte[] data)
        {
            return(Observable.Create <DescriptorResult>(ob =>
            {
                var handler = new EventHandler <CBDescriptorEventArgs>((sender, args) =>
                {
                    if (!this.Equals(args.Descriptor))
                    {
                        if (args.Error != null)
                        {
                            ob.OnError(new ArgumentException(args.Error.ToString()));
                        }
                        else
                        {
                            this.Value = data;

                            var result = new DescriptorResult(this, DescriptorEvent.Write, this.Value);
                            ob.Respond(result);
                            this.WriteSubject.OnNext(result);
                        }
                    }
                });

                var nsdata = NSData.FromArray(data);
                this.Peripheral.WroteDescriptorValue += handler;
                this.Peripheral.WriteValue(nsdata, this.native);

                return () => this.Peripheral.WroteDescriptorValue -= handler;
            }));
        }
Ejemplo n.º 2
0
        public override IObservable <DescriptorResult> Write(byte[] data) => this.context.Lock(Observable.Create <DescriptorResult>(async ob =>
        {
            var sub = this.context
                      .Callbacks
                      .DescriptorWrite
                      .Where(this.NativeEquals)
                      .Subscribe(args =>
            {
                if (!args.IsSuccessful)
                {
                    ob.OnError(new ArgumentException($"Failed to write descriptor value - {this.Uuid} - {args.Status}"));
                }
                else
                {
                    this.Value = data;

                    var result = new DescriptorResult(this, DescriptorEvent.Write, data);
                    ob.Respond(result);
                    this.WriteSubject.OnNext(result);
                }
            });

            await this.context.Marshall(() =>
            {
                this.native.SetValue(data);
                this.context.Gatt.WriteDescriptor(this.native);
            });

            return(sub);
        }));
Ejemplo n.º 3
0
        public override IObservable <DescriptorResult> Read()
        {
            return(Observable.Create <DescriptorResult>(ob =>
            {
                var handler = new EventHandler <CBDescriptorEventArgs>((sender, args) =>
                {
                    if (!this.Equals(args.Descriptor))
                    {
                        if (args.Error != null)
                        {
                            ob.OnError(new ArgumentException(args.Error.ToString()));
                        }
                        else
                        {
                            this.Value = ((NSData)args.Descriptor.Value).ToArray();

                            var result = new DescriptorResult(this, DescriptorEvent.Read, this.Value);
                            ob.Respond(result);
                            this.ReadSubject.OnNext(result);
                        }
                    }
                });
                this.Peripheral.UpdatedValue += handler;
                this.Peripheral.ReadValue(this.native);

                return () => this.Peripheral.UpdatedValue -= handler;
            }));
        }
Ejemplo n.º 4
0
        public override IObservable <DescriptorResult> Read()
        {
            //this.native.Permissions == GattDescriptorPermission.Read
            return(Observable.Create <DescriptorResult>(ob =>
            {
                var handler = new EventHandler <GattDescriptorEventArgs>((sender, args) =>
                {
                    if (args.Descriptor.Equals(this.native))
                    {
                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to read descriptor value {this.Uuid} - {args.Status}"));
                        }
                        else
                        {
                            this.Value = this.native.GetValue();

                            var result = new DescriptorResult(this, DescriptorEvent.Write, this.Value);
                            ob.Respond(result);
                            this.ReadSubject.OnNext(result);
                        }
                    }
                });
                this.context.Callbacks.DescriptorRead += handler;
                this.context.Gatt.ReadDescriptor(this.native);
                return () => this.context.Callbacks.DescriptorRead -= handler;
            }));
        }
Ejemplo n.º 5
0
        public override IObservable <DescriptorResult> Write(byte[] data)
        {
            return(Observable.Create <DescriptorResult>(ob =>
            {
                var handler = new EventHandler <GattDescriptorEventArgs>((sender, args) =>
                {
                    if (args.Descriptor.Equals(this.native))
                    {
                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to write descriptor value - {this.Uuid} - {args.Status}"));
                        }
                        else
                        {
                            this.Value = data;

                            var result = new DescriptorResult(this, DescriptorEvent.Write, data);
                            ob.Respond(result);
                            this.WriteSubject.OnNext(result);
                        }
                    }
                });
                this.context.Callbacks.DescriptorWrite += handler;
                AndroidConfig.SyncPost(() =>
                {
                    this.native.SetValue(data);
                    this.context.Gatt.WriteDescriptor(this.native);
                });
                return () => this.context.Callbacks.DescriptorWrite -= handler;
            }));
        }
Ejemplo n.º 6
0
        public override IObservable <DescriptorResult> Write(byte[] data)
        {
            return(Observable.Create <DescriptorResult>(async ob =>
            {
                var cancelSrc = new CancellationTokenSource();

                var handler = new EventHandler <GattDescriptorEventArgs>((sender, args) =>
                {
                    if (this.NativeEquals(args))
                    {
                        this.context.Semaphore.Release();

                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to write descriptor value - {this.Uuid} - {args.Status}"));
                        }
                        else
                        {
                            this.Value = data;

                            var result = new DescriptorResult(this, DescriptorEvent.Write, data);
                            ob.Respond(result);
                            this.WriteSubject.OnNext(result);
                        }
                    }
                });

                await this.context.Semaphore.WaitAsync(cancelSrc.Token);
                this.context.Callbacks.DescriptorWrite += handler;
                this.context.Marshall(() =>
                {
                    this.native.SetValue(data);
                    this.context.Gatt.WriteDescriptor(this.native);
                });

                return () =>
                {
                    cancelSrc.Dispose();
                    this.context.Callbacks.DescriptorWrite -= handler;
                };
            }));
        }
Ejemplo n.º 7
0
        public override IObservable <DescriptorResult> Read()
        {
            //this.native.Permissions == GattDescriptorPermission.Read
            return(Observable.Create <DescriptorResult>(async ob =>
            {
                var cancelSrc = new CancellationTokenSource();

                var handler = new EventHandler <GattDescriptorEventArgs>((sender, args) =>
                {
                    if (args.Descriptor.Equals(this.native))
                    {
                        this.context.Semaphore.Release();

                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to read descriptor value {this.Uuid} - {args.Status}"));
                        }
                        else
                        {
                            this.Value = this.native.GetValue();

                            var result = new DescriptorResult(this, DescriptorEvent.Write, this.Value);
                            ob.Respond(result);
                            this.ReadSubject.OnNext(result);
                        }
                    }
                });

                await this.context.Semaphore.WaitAsync(cancelSrc.Token);
                this.context.Callbacks.DescriptorRead += handler;
                this.context.Marshall(() =>
                                      this.context.Gatt.ReadDescriptor(this.native)
                                      );
                return () =>
                {
                    cancelSrc.Dispose();
                    this.context.Callbacks.DescriptorRead -= handler;
                };
            }));
        }
Ejemplo n.º 8
0
        public override IObservable <DescriptorResult> Read() => this.context.Lock(Observable.Create <DescriptorResult>(async ob =>
        {
            var sub = this.context
                      .Callbacks
                      .DescriptorRead
                      .Where(this.NativeEquals)
                      .Subscribe(args =>
            {
                if (!args.IsSuccessful)
                {
                    ob.OnError(new ArgumentException($"Failed to read descriptor value {this.Uuid} - {args.Status}"));
                }
                else
                {
                    this.Value = this.native.GetValue();

                    var result = new DescriptorResult(this, DescriptorEvent.Write, this.Value);
                    ob.Respond(result);
                    this.ReadSubject.OnNext(result);
                }
            });

            await this.context.Marshall(() =>
            {
                try
                {
                    if (!this.context.Gatt.ReadDescriptor(this.native))
                    {
                        ob.OnError(new Exception($"Failed to read descriptor."));
                    }
                }
                catch (Exception ex)
                {
                    ob.OnError(ex);
                }
            });

            return(sub);
        }));