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); }
public override async void OnAppearing() { base.OnAppearing(); var service = await peripheralManager.AddService(ServiceUuid, true, sb => { sb.AddCharacteristic(Characteristic1Uuid, cb => cb .SetWrite(request => { this.LastWriteValue = Encoding.UTF8.GetString(request.Data, 0, request.Data.Length); this.LastWriteTime = DateTime.Now.ToString(); return(GattState.Success); }) .SetRead(request => { var ticks = DateTime.Now.Ticks; this.LastReadValue = ticks.ToString(); this.LastReadTime = DateTime.Now.ToString(); var data = BitConverter.GetBytes(ticks); return(ReadResult.Success(data)); }) ); this.simplePush = sb.AddCharacteristic(Characteristic2Uuid, cb => cb.SetNotification(cs => // main thread this.SubscribersSimple = cs.Characteristic.SubscribedCentrals.Count )); sb.AddCharacteristic(Characteristic3Uuid, cb => cb .SetWrite(request => { // main thread this.SpeedWrites++; return(GattState.Success); }) .SetRead(request => { //Interlocked.Increment(ref this.SpeedWrites); var data = BitConverter.GetBytes(DateTime.Now.Ticks); return(ReadResult.Success(data)); }) ); this.perfPush = sb.AddCharacteristic(Characteristic4Uuid, cb => cb.SetNotification(cs => this.SubscribersPerf = cs.Characteristic.SubscribedCentrals.Count )); }); await this.peripheralManager.StartAdvertising(new AdvertisementData { LocalName = "My GATT" }); // TODO: simple push of datetime Observable .Interval(TimeSpan.FromSeconds(2)) .SubOnMainThread(async _ => { // TODO: calc speeds await this.Send(); }) .DisposeWith(this.DeactivateWith); }
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); }