public CharacteristicEditPage(IGattCharacteristic selectedCharacteristic)
        {
            InitializeComponent();

            Title = selectedCharacteristic.Uuid.ToString();


            if (selectedCharacteristic.CanRead())
            {
                ReadCharacteristicAsync(selectedCharacteristic);
            }

            if (selectedCharacteristic.CanWrite())
            {
                //await Characteristic.Write(bytes);
            }

            if (selectedCharacteristic.CanNotify())
            {
                // TODO: couldn't get this to work for the demo.
                // The docs show this, but SetNotificationValue() doesn't exist in IGattCharacteristic
                //var success = selectedCharacteristic.SetNotificationValue(CharacteristicConfigDescriptorValue.Notify);

                // TODO: this didn't seem to do much
                selectedCharacteristic.SubscribeToNotifications();
                selectedCharacteristic.WhenNotificationReceived().Subscribe(result =>
                {
                    //result.Data
                });
            }
        }
Exemplo n.º 2
0
 public static void AssertNotify(this IGattCharacteristic characteristic)
 {
     if (!characteristic.CanNotify())
     {
         throw new ArgumentException($"This characteristic '{characteristic.Uuid}' does not support notifications");
     }
 }
Exemplo n.º 3
0
        public static IObservable <byte[]> WhenReadOrNotify(this IGattCharacteristic character, TimeSpan readInterval)
        {
            if (character.CanNotify())
            {
                return(character.SubscribeToNotifications());
            }

            if (character.CanRead())
            {
                return(character.ReadInterval(readInterval));
            }

            throw new ArgumentException($"Characteristic {character.Uuid} does not have read or notify permissions");
        }
Exemplo n.º 4
0
        public static IObservable <CharacteristicResult> WhenReadOrNotify(this IGattCharacteristic character, TimeSpan readInterval)
        {
            if (character.CanNotify())
            {
                return(character
                       .EnableNotifications()
                       .Where(x => x)
                       .Select(x => character.WhenNotificationReceived())
                       .Switch());
            }

            if (character.CanRead())
            {
                return(character.ReadInterval(readInterval));
            }

            throw new ArgumentException($"Characteristic {character.Uuid} does not have read or notify permissions");
        }
Exemplo n.º 5
0
        public Device(Plugin.BluetoothLE.IDevice dev)
        {
            device = dev;
            Name   = dev.Name;

            chararacteristicListener = device.WhenAnyCharacteristicDiscovered().Subscribe(characteristic =>
            {
                if (characteristic.Uuid.ToString() == BleService.CharacteristictReadUuid)
                {
                    readCharacteristic = characteristic;
                    OnStateChanged(DeviceState.Connected);
                    RaisePropertyChanged(nameof(ConnectState));
                    if (readCharacteristic.CanNotify())
                    {
                        readCharacteristicObserver = characteristic.RegisterAndNotify(true).Subscribe(r =>
                        {
                            Read?.Invoke(r.Data);
                        },
                                                                                                      exception =>
                        {
                            //   Crashes.TrackError(exception);
                        });
                    }
                }

                if (characteristic.Uuid.ToString() == BleService.CharacteristicWriteUuid)
                {
                    if (characteristic.CanWrite())
                    {
                        writeCharacteristic = characteristic;
                        OnStateChanged(DeviceState.Connected);
                        RaisePropertyChanged(nameof(ConnectState));
                    }
                }
            }, exception =>
            {
                //  Crashes.TrackError(exception);
            });
            stateListener = device.WhenStatusChanged().Subscribe(f =>
            {
                OnStateChanged(ConnectState);
            });
        }
Exemplo n.º 6
0
        private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
        {
            var select = (CharacteristicsList)e.Item;

            foreach (var c in AllCharacteristics)
            {
                if (c.Uuid == select.Uuid)
                {
                    SelectCharacteristic = c;
                    info_uuid.Text       = "UUID:" + SelectCharacteristic.Uuid;
                    info_read.Text       = "CallBack UUID:";
                    notify_btn.Text      = "Notify";
                    if (SelectCharacteristic.CanRead())
                    {
                        read_btn.IsVisible = true;
                    }
                    else
                    {
                        read_btn.IsVisible = false;
                    }
                    if (SelectCharacteristic.CanWrite())
                    {
                        write_btn.IsVisible = true;
                    }
                    else
                    {
                        write_btn.IsVisible = false;
                    }
                    if (SelectCharacteristic.CanNotify())
                    {
                        notify_btn.IsVisible = true;
                    }
                    else
                    {
                        notify_btn.IsVisible = false;
                    }
                    background.IsVisible = true;
                    info.IsVisible       = true;
                    break;
                }
            }
        }
Exemplo n.º 7
0
 public static bool CanNotifyOrIndicate(this IGattCharacteristic ch) => ch.CanNotify() || ch.CanIndicate();