示例#1
0
        public MainViewModel(IPeripheralManager peripheral)
        {
            this.ToggleServer = ReactiveCommand.CreateFromTask(async _ =>
            {
                if (peripheral.IsAdvertising)
                {
                    this.timer?.Dispose();
                    this.IsRunning = false;
                    peripheral.ClearServices();
                    peripheral.StopAdvertising();
                    this.Write("GATT Server Stopped");
                }
                else
                {
                    await peripheral.AddService
                    (
                        ServiceUuid,
                        true,
                        sb =>
                    {
                        this.notifications = sb.AddCharacteristic
                                             (
                            NotifyCharacteristicUuid,
                            cb => cb.SetNotification(cs =>
                        {
                            var @event = cs.IsSubscribing ? "Subscribed" : "Unsubcribed";
                            this.Write($"Device {cs.Peripheral.Uuid} {@event}");
                            //this.Write($"Charcteristic Subcribers: {characteristic.SubscribedDevices.Count}");
                        })
                                             );

                        sb.AddCharacteristic
                        (
                            ReadWriteCharacteristicUuid,
                            cb => cb
                            .SetRead(req =>
                        {
                            var bytes = Encoding.UTF8.GetBytes(this.CharacteristicValue ?? "Test");
                            this.Write($"Characteristic Read Received - {bytes}");
                            return(ReadResult.Success(bytes));
                        })
                            .SetWrite(req =>
                        {
                            var write = Encoding.UTF8.GetString(req.Data, 0, req.Data.Length);
                            this.Write($"Characteristic Write Received - {write}");
                            return(GattState.Success);
                        })
                        );
                    }
                    );
                    await peripheral.StartAdvertising(new AdvertisementData
                    {
                        LocalName = "My GATT"
                    });
                    this.Write("GATT Server Started");

                    var count  = 0;
                    this.timer = Observable
                                 .Timer(TimeSpan.FromSeconds(10))
                                 // TODO: only when characteristic has subscribers
                                 .Subscribe(async x =>
                    {
                        count++;
                        //await this.notifications.Notify(Encoding.UTF8.GetBytes(count.ToString()));
                    });

                    this.IsRunning = true;
                }
            });

            this.Clear = ReactiveCommand.Create(() => this.Output = String.Empty);
        }
示例#2
0
        public MainViewModel(IPeripheralManager peripheral, IUserDialogs dialogs)
        {
            this.dialogs = dialogs;

            this.ToggleServer = ReactiveCommand.CreateFromTask(async _ =>
            {
                if (peripheral.IsAdvertising)
                {
                    this.timer?.Dispose();
                    this.IsRunning = false;
                    peripheral.ClearServices();
                    peripheral.StopAdvertising();
                    this.Write("GATT Server Stopped");
                }
                else
                {
                    await peripheral.AddService
                    (
                        ServiceUuid,
                        true,
                        sb =>
                    {
                        this.notifications = sb.AddCharacteristic
                                             (
                            NotifyCharacteristicUuid,
                            cb => cb.SetNotification(cs =>
                        {
                            var subs = cs.Characteristic.SubscribedCentrals.Count;

                            var @event = cs.IsSubscribing ? "Subscribed" : "Unsubcribed";
                            this.Write($"Device {cs.Peripheral.Uuid} {@event}");
                            this.Write($"Charcteristic Subcribers: {subs}");

                            if (subs == 0)
                            {
                                this.StopTimer();
                            }
                            else
                            {
                                this.StartTimer();
                            }
                        })
                                             );

                        sb.AddCharacteristic
                        (
                            ReadWriteCharacteristicUuid,
                            cb => cb
                            .SetRead(req =>
                        {
                            var value = this.CharacteristicValue ?? "Test";
                            var bytes = Encoding.UTF8.GetBytes(value);
                            this.Write($"Characteristic Read Received - Sent {value}");
                            return(ReadResult.Success(bytes));
                        })
                            .SetWrite(req =>
                        {
                            var write = Encoding.UTF8.GetString(req.Data, 0, req.Data.Length);
                            this.Write($"Characteristic Write Received - {write}");
                            return(GattState.Success);
                        })
                        );
                    }
                    );
                    await peripheral.StartAdvertising(new AdvertisementData
                    {
                        LocalName = "My GATT"
                    });
                    this.Write("GATT Server Started with Name: My GATT");
                    this.IsRunning = true;
                }
            });

            this.Clear = ReactiveCommand.Create(() => this.Output = String.Empty);
        }