public override IObservable <CharacteristicResult> WhenNotificationReceived() { this.AssertNotify(); this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob => { var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) => { if (this.NativeEquals(args)) { 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.context.Callbacks.CharacteristicChanged += handler; return(() => this.context.Callbacks.CharacteristicChanged -= handler); }) .Publish() .RefCount(); return(this.notifyOb); }
public override IObservable <CharacteristicResult> WhenNotificationReceived() { this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob => { var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) => { if (this.Equals(args.Characteristic)) { if (args.Error != null) { ob.OnError(new ArgumentException(args.Error.ToString())); } else { this.Value = this.NativeCharacteristic.Value?.ToArray(); var result = new CharacteristicResult(this, CharacteristicEvent.Notification, this.Value); ob.OnNext(result); } } }); this.Peripheral.UpdatedCharacterteristicValue += handler; return(() => this.Peripheral.UpdatedCharacterteristicValue -= handler); }) .Publish() .RefCount(); return(this.notifyOb); }
public override IObservable <CharacteristicResult> Write(byte[] value) => Observable.Create <CharacteristicResult>(ob => { this.native.WriteValue(value); var result = new CharacteristicResult(this, CharacteristicEvent.Write, value); ob.Respond(result); return(Disposable.Empty); });
public override IObservable <CharacteristicResult> Read() => Observable.Create <CharacteristicResult>(ob => { var data = this.native.ReadValue(); var result = new CharacteristicResult(this, CharacteristicEvent.Read, data); ob.Respond(result); return(Disposable.Empty); });
public override IObservable <CharacteristicResult> Read() { this.AssertRead(); return(Observable.Create <CharacteristicResult>(ob => { var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) => { if (args.Characteristic.Uuid.Equals(this.native.Uuid)) { 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; })); }
public override IObservable <CharacteristicResult> WhenNotificationReceived() { this.AssertNotify(); this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob => this.context .Callbacks .CharacteristicChanged .Where(this.NativeEquals) .Subscribe(args => { 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); } }) ) .Publish() .RefCount(); return(this.notifyOb); }
public override IObservable <CharacteristicResult> Read() { this.AssertRead(); return(Observable.Create <CharacteristicResult>(ob => { var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) => { if (this.Equals(args.Characteristic)) { if (args.Error != null) { ob.OnError(new ArgumentException(args.Error.ToString())); } else { this.Value = this.NativeCharacteristic.Value.ToArray(); var result = new CharacteristicResult(this, CharacteristicEvent.Read, this.Value); ob.Respond(result); this.ReadSubject.OnNext(result); } } }); this.Peripheral.UpdatedCharacterteristicValue += handler; this.Peripheral.ReadValue(this.NativeCharacteristic); return () => this.Peripheral.UpdatedCharacterteristicValue -= handler; })); }
public override IObservable <CharacteristicResult> Write(byte[] value) { this.AssertWrite(false); return(Observable.Create <CharacteristicResult>(async ob => { CancellationTokenSource cancelSrc = null; var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) => { if (this.NativeEquals(args)) { if (cancelSrc != null) { this.context.Semaphore.Release(); } Log.Write("Incoming Characteristic Write Event - " + args.Characteristic.Uuid); 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)) { cancelSrc = new CancellationTokenSource(); Log.Write("Hooking for write response - " + this.Uuid); await this.context.Semaphore.WaitAsync(cancelSrc.Token); this.context.Callbacks.CharacteristicWrite += handler; this.RawWriteWithResponse(value); } else { Log.Write("Write with No Response - " + this.Uuid); this.RawWriteNoResponse(ob, value); } return () => { cancelSrc?.Dispose(); this.context.Callbacks.CharacteristicWrite -= handler; }; })); }
void InternalWriteNoResponse(IObserver <CharacteristicResult> ob, byte[] value) { var data = NSData.FromArray(value); this.Peripheral.WriteValue(data, this.NativeCharacteristic, 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) => this.context.Marshall(() => { this.native.SetValue(bytes); this.native.WriteType = GattWriteType.NoResponse; this.context.Gatt.WriteCharacteristic(this.native); this.Value = bytes; var result = new CharacteristicResult(this, CharacteristicEvent.Write, bytes); this.WriteSubject.OnNext(result); ob?.Respond(result); });
public override IObservable <CharacteristicResult> SubscribeToNotifications() { this.AssertNotify(); this.notificationOb = this.notificationOb ?? Observable.Create <CharacteristicResult>(async ob => { //var trigger = new GattCharacteristicNotificationTrigger(this.native); var handler = new TypedEventHandler <Native, GattValueChangedEventArgs>((sender, args) => { if (sender.Equals(this.Native)) { var bytes = args.CharacteristicValue.ToArray(); var result = new CharacteristicResult(this, CharacteristicEvent.Notification, bytes); ob.OnNext(result); this.NotifySubject.OnNext(result); } }); this.Native.ValueChanged += handler; await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync( CoreDispatcherPriority.Normal, async() => { var status = await this.Native.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); if (status != GattCommunicationStatus.Success) { ob.OnError(new Exception("Could not subscribe to notifications")); } } ); return(async() => { this.Native.ValueChanged -= handler; await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { try { await this.Native.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue.None); } catch (Exception e) { //System.Console.WriteLine(e); System.Diagnostics.Debug.WriteLine(e.ToString()); } }); }); }); return(this.notificationOb); }
public override IObservable <CharacteristicResult> Read() { this.AssertRead(); return(Observable.Create <CharacteristicResult>(async ob => { var cancelSrc = new CancellationTokenSource(); var handler = new EventHandler <GattCharacteristicEventArgs>((sender, args) => { if (this.NativeEquals(args)) { this.context.Semaphore.Release(); 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; await this.context.Semaphore.WaitAsync(cancelSrc.Token); this.context.Marshall(() => { try { this.context.Gatt.ReadCharacteristic(this.native); } catch (Exception ex) { ob.OnError(ex); } }); return () => this.context.Callbacks.CharacteristicRead -= handler; })); }
IObservable <object> RawWriteNoResponse(IObserver <CharacteristicResult> ob, byte[] bytes) => this.context.Marshall(() => { try { this.native.SetValue(bytes); this.native.WriteType = GattWriteType.NoResponse; this.context.Gatt.WriteCharacteristic(this.native); this.Value = bytes; var result = new CharacteristicResult(this, CharacteristicEvent.Write, bytes); this.WriteSubject.OnNext(result); ob?.Respond(result); } catch (Exception ex) { ob?.OnError(ex); } });
public override IObservable <CharacteristicResult> Read() => this.context.Lock(Observable.Create <CharacteristicResult>(async ob => { this.AssertRead(); var sub = this.context .Callbacks .CharacteristicRead .Where(this.NativeEquals) .Subscribe(args => { 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); } }); await this.context.Marshall(() => { try { if (!this.context.Gatt.ReadCharacteristic(this.native)) { ob.OnError(new Exception($"Failed to read characteristic.")); } } catch (Exception ex) { ob.OnError(ex); } }); return(sub); }));
public override IObservable <CharacteristicResult> WhenNotificationReceived() { this.notificationOb = this.notificationOb ?? Observable.Create <CharacteristicResult>(ob => { //var trigger = new GattCharacteristicNotificationTrigger(this.native); var handler = new TypedEventHandler <Native, GattValueChangedEventArgs>((sender, args) => { if (sender.Equals(this.Native)) { var bytes = args.CharacteristicValue.ToArray(); var result = new CharacteristicResult(this, CharacteristicEvent.Notification, bytes); ob.OnNext(result); } }); this.Native.ValueChanged += handler; return(() => this.Native.ValueChanged -= handler); }); return(this.notificationOb); }
public override IObservable <CharacteristicResult> Write(byte[] value) { this.AssertWrite(false); return(Observable.Create <CharacteristicResult>(ob => { var cancelSrc = new CancellationTokenSource(); var data = NSData.FromArray(value); var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) => { if (this.Equals(args.Characteristic)) { 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)) { this.Peripheral.WroteCharacteristicValue += handler; this.Peripheral.WriteValue(data, this.NativeCharacteristic, CBCharacteristicWriteType.WithResponse); } else { this.InternalWriteNoResponse(ob, value); } return () => this.Peripheral.WroteCharacteristicValue -= handler; })); }
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.Uuid.Equals(this.native.Uuid)) { 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; })); }
public override IObservable <CharacteristicResult> Write(byte[] value) { // TODO: reliable write this.AssertWrite(false); return(Observable.FromAsync(async ct => { var result = await this.Native .WriteValueAsync(value.AsBuffer(), GattWriteOption.WriteWithResponse) .AsTask(ct); if (result != GattCommunicationStatus.Success) { throw new Exception("Error writing characteristic"); } this.Value = value; var r = new CharacteristicResult(this, CharacteristicEvent.Write, value); this.WriteSubject.OnNext(r); return r; })); }
public override IObservable <CharacteristicResult> SubscribeToNotifications() { this.AssertNotify(); this.notifyOb = this.notifyOb ?? Observable.Create <CharacteristicResult>(ob => { var handler = new EventHandler <CBCharacteristicEventArgs>((sender, args) => { if (this.Equals(args.Characteristic)) { if (args.Error != null) { ob.OnError(new ArgumentException(args.Error.ToString())); } else { this.Value = this.NativeCharacteristic.Value.ToArray(); var result = new CharacteristicResult(this, CharacteristicEvent.Notification, this.Value); ob.OnNext(result); this.NotifySubject.OnNext(result); } } }); this.Peripheral.UpdatedCharacterteristicValue += handler; this.Peripheral.SetNotifyValue(true, this.NativeCharacteristic); return(() => { this.Peripheral.SetNotifyValue(false, this.NativeCharacteristic); // this is null this.Peripheral.UpdatedCharacterteristicValue -= handler; }); }) .Publish() .RefCount(); return(this.notifyOb); }
public override IObservable <CharacteristicResult> Write(byte[] value) => this.context.Lock(Observable.Create <CharacteristicResult>(async ob => { this.AssertWrite(false); Log.Debug("Characteristic", "past write gate"); var sub = this.context .Callbacks .CharacteristicWrite .Where(this.NativeEquals) .Subscribe(args => { Log.Debug("Characteristic", "write vent - " + args.Characteristic.Uuid); 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)) { Log.Debug("Characteristic", "Hooking for write response - " + this.Uuid); await this.RawWriteWithResponse(value); } else { Log.Debug("Characteristic", "Write with No Response - " + this.Uuid); await this.RawWriteNoResponse(ob, value); } return(sub); }));
public override IObservable <CharacteristicResult> Read() { this.AssertRead(); return(Observable.FromAsync(async ct => { var result = await this.Native .ReadValueAsync(BluetoothCacheMode.Uncached) .AsTask(ct); if (result.Status != GattCommunicationStatus.Success) { throw new Exception("Error reading characteristics - " + result.Status); } var bytes = result.Value.ToArray(); this.Value = bytes; var r = new CharacteristicResult(this, CharacteristicEvent.Read, bytes); this.ReadSubject.OnNext(r); return r; })); }