Exemplo n.º 1
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;
            }));
        }
Exemplo n.º 2
0
        protected virtual bool EnableNotifications()
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);

            if (descriptor == null)
            {
                throw new ArgumentException("Characteristic Client Configuration Descriptor not found");
            }

            var success = this.context.Gatt.SetCharacteristicNotification(this.native, true);

            Thread.Sleep(100);

            if (success)
            {
                AndroidConfig.SyncPost(() =>
                {
                    descriptor.SetValue(BluetoothGattDescriptor.EnableNotificationValue.ToArray());
                    success = this.context.Gatt.WriteDescriptor(descriptor);
                    if (success)
                    {
                        this.IsNotifying = true;
                    }
                });
            }
            return(success);
        }
Exemplo n.º 3
0
 void RawWriteWithResponse(byte[] bytes)
 {
     AndroidConfig.SyncPost(() =>
     {
         this.native.SetValue(bytes);
         this.native.WriteType = GattWriteType.Default;
         this.context.Gatt.WriteCharacteristic(this.native);
     });
 }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        protected virtual void DisableNotifications()
        {
            var descriptor = this.native.GetDescriptor(NotifyDescriptorId);

            if (descriptor == null)
            {
                throw new ArgumentException("Characteristic Client Configuration Descriptor not found");
            }

            AndroidConfig.SyncPost(() =>
            {
                descriptor.SetValue(BluetoothGattDescriptor.DisableNotificationValue.ToArray());
                this.context.Gatt.WriteDescriptor(descriptor);
                this.context.Gatt.SetCharacteristicNotification(this.native, false);
                this.IsNotifying = false;
            });
        }