public override IObservable <CharacteristicResult> Read()
        {
            this.AssertRead();

            return(Observable.Create <CharacteristicResult>(ob =>
            {
                var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) =>
                {
                    if (args.Characteristic.Equals(this.native))
                    {
                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to read characteristic - {args.Status}"));
                        }
                        else
                        {
                            this.Value = args.Characteristic.GetValue();

                            var result = new CharacteristicResult(this, CharacteristicEvent.Read, this.Value);
                            ob.Respond(result);
                            this.ReadSubject.OnNext(result);
                        }
                    }
                });
                this.context.Callbacks.CharacteristicRead += handler;
                this.context.Gatt.ReadCharacteristic(this.native);

                return () => this.context.Callbacks.CharacteristicRead -= handler;
            }));
        }
Beispiel #2
0
        public override IObservable <CharacteristicResult> Read()
        {
            this.AssertRead();

            return(Observable.Create <CharacteristicResult>(ob =>
            {
                var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) =>
                {
                    if (args.Characteristic.UUID.Equals(this.native.UUID))
                    {
                        if (args.Error != null)
                        {
                            ob.OnError(new ArgumentException(args.Error.ToString()));
                        }
                        else
                        {
                            this.Value = this.native.Value.ToArray();
                            var result = new CharacteristicResult(this, CharacteristicEvent.Read, this.Value);
                            ob.Respond(result);
                            this.ReadSubject.OnNext(result);
                        }
                    }
                });
                this.native.Service.Peripheral.UpdatedCharacterteristicValue += handler;
                this.native.Service.Peripheral.ReadValue(this.native);

                return () => this.native.Service.Peripheral.UpdatedCharacterteristicValue -= handler;
            }));
        }
Beispiel #3
0
        void InternalWriteNoResponse(IObserver <CharacteristicResult> ob, byte[] value)
        {
            var data = NSData.FromArray(value);
            var p    = this.native.Service.Peripheral;

            p.WriteValue(data, this.native, CBCharacteristicWriteType.WithoutResponse);
            this.Value = value;
            var result = new CharacteristicResult(this, CharacteristicEvent.Write, value);

            this.WriteSubject.OnNext(result);
            ob?.Respond(result);
        }
        void RawWriteNoResponse(IObserver <CharacteristicResult> ob, byte[] bytes)
        {
            var result = new CharacteristicResult(this, CharacteristicEvent.Write, bytes);

            AndroidConfig.SyncPost(() =>
            {
                this.native.SetValue(bytes);
                this.native.WriteType = GattWriteType.NoResponse;
                this.context.Gatt.WriteCharacteristic(this.native);
                this.Value = bytes;
            });
            this.WriteSubject.OnNext(result);
            ob?.Respond(result);
        }
        public override IObservable <CharacteristicResult> Write(byte[] value)
        {
            this.AssertWrite(false);

            return(Observable.Create <CharacteristicResult>(ob =>
            {
                var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) =>
                {
                    WriteLine($"Incoming Characteristic Write Event - " + args.Characteristic.Uuid);

                    if (args.Characteristic.Equals(this.native))
                    {
                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException($"Failed to write characteristic - {args.Status}"));
                        }
                        else
                        {
                            this.Value = value;
                            var result = new CharacteristicResult(this, CharacteristicEvent.Write, this.Value);
                            ob.Respond(result);
                            this.WriteSubject.OnNext(result);
                        }
                    }
                });

                if (this.Properties.HasFlag(CharacteristicProperties.Write))
                {
                    WriteLine("Hooking for write response - " + this.Uuid);
                    this.context.Callbacks.CharacteristicWrite += handler;
                    this.RawWriteWithResponse(value);
                }
                else
                {
                    WriteLine("Write with No Response - " + this.Uuid);
                    this.RawWriteNoResponse(ob, value);
                }
                return () => this.context.Callbacks.CharacteristicWrite -= handler;
            }));
        }
Beispiel #6
0
        public override IObservable <CharacteristicResult> Write(byte[] value)
        {
            this.AssertWrite(false);

            return(Observable.Create <CharacteristicResult>(ob =>
            {
                var data = NSData.FromArray(value);
                var p = this.native.Service.Peripheral;
                var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) =>
                {
                    if (args.Characteristic.UUID.Equals(this.native.UUID))
                    {
                        if (args.Error != null)
                        {
                            ob.OnError(new ArgumentException(args.Error.ToString()));
                        }
                        else
                        {
                            this.Value = value;

                            var result = new CharacteristicResult(this, CharacteristicEvent.Write, value);
                            ob.Respond(result);
                            this.WriteSubject.OnNext(result);
                        }
                    }
                });

                if (this.Properties.HasFlag(CharacteristicProperties.Write))
                {
                    p.WroteCharacteristicValue += handler;
                    p.WriteValue(data, this.native, CBCharacteristicWriteType.WithResponse);
                }
                else
                {
                    this.InternalWriteNoResponse(ob, value);
                }
                return () => p.WroteCharacteristicValue -= handler;
            }));
        }
        public override IObservable <CharacteristicResult> SubscribeToNotifications()
        {
            this.AssertNotify();

            this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob =>
            {
                var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) =>
                {
                    if (args.Characteristic.Equals(this.native))
                    {
                        if (!args.IsSuccessful)
                        {
                            ob.OnError(new ArgumentException("Error subscribing to " + args.Characteristic.Uuid));
                        }
                        else
                        {
                            this.Value = args.Characteristic.GetValue();

                            var result = new CharacteristicResult(this, CharacteristicEvent.Notification, this.Value);
                            ob.OnNext(result);
                            this.NotifySubject.OnNext(result);
                        }
                    }
                });
                this.EnableNotifications();
                this.context.Callbacks.CharacteristicChanged += handler;

                return(() =>
                {
                    this.DisableNotifications();
                    this.context.Callbacks.CharacteristicChanged -= handler;
                });
            })
                            .Publish()
                            .RefCount();

            return(this.notifyOb);
        }
Beispiel #8
0
        public override IObservable <CharacteristicResult> SubscribeToNotifications()
        {
            this.AssertNotify();

            this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob =>
            {
                var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) =>
                {
                    if (args.Characteristic.UUID.Equals(this.native.UUID))
                    {
                        if (args.Error != null)
                        {
                            ob.OnError(new ArgumentException(args.Error.ToString()));
                        }
                        else
                        {
                            this.Value = this.native.Value.ToArray();

                            var result = new CharacteristicResult(this, CharacteristicEvent.Notification, this.Value);
                            ob.OnNext(result);
                            this.NotifySubject.OnNext(result);
                        }
                    }
                });
                this.native.Service.Peripheral.UpdatedCharacterteristicValue += handler;
                this.native.Service.Peripheral.SetNotifyValue(true, this.native);

                return(() =>
                {
                    this.native.Service.Peripheral.SetNotifyValue(false, this.native);
                    this.native.Service.Peripheral.UpdatedCharacterteristicValue -= handler;
                });
            })
                            .Publish()
                            .RefCount();

            return(this.notifyOb);
        }