Beispiel #1
0
        public override IObservable <ReadRequest> WhenReadReceived()
        {
            this.readOb = this.readOb ?? Observable.Create <ReadRequest>(ob =>
            {
                var handler = new EventHandler <CBATTRequestEventArgs>((sender, args) =>
                {
                    if (args.Request.Characteristic.Equals(this.Native))
                    {
                        var device  = new Device(args.Request.Central);
                        var request = new ReadRequest(device, (int)args.Request.Offset);
                        ob.OnNext(request);

                        var nativeStatus   = (CBATTError)Enum.Parse(typeof(CBATTError), request.Status.ToString());
                        args.Request.Value = NSData.FromArray(request.Value);
                        this.manager.RespondToRequest(args.Request, nativeStatus);
                    }
                });
                this.manager.ReadRequestReceived += handler;
                return(() => this.manager.ReadRequestReceived -= handler);
            })
                          .Publish()
                          .RefCount();

            return(this.readOb);
        }
Beispiel #2
0
        public override IObservable <ReadRequest> WhenReadReceived()
        {
            this.readOb = this.readOb ?? Observable.Create <ReadRequest>(ob =>
            {
                var handler = new TypedEventHandler <GattLocalCharacteristic, GattReadRequestedEventArgs>(async(sender, args) =>
                {
                    var request = await args.GetRequestAsync();
                    var dev     = this.FindDevice(args.Session);
                    var read    = new ReadRequest(dev, (int)request.Length);
                    ob.OnNext(read);

                    if (read.Status == GattStatus.Success)
                    {
                        request.RespondWithValue(read.Value.AsBuffer());
                    }
                    else
                    {
                        request.RespondWithProtocolError((byte)read.Status);
                    }
                });
                var sub = this.nativeReady.Subscribe(dev => this.native.ReadRequested += handler);
                return(() =>
                {
                    sub.Dispose();
                    if (this.native != null)
                    {
                        this.native.ReadRequested -= handler;
                    }
                });
            })
                          .Publish()
                          .RefCount();

            return(this.readOb);
        }