/// <summary>
        /// Attempts to connect if not already connected
        /// </summary>
        /// <param name="peripheral"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static IObservable <IPeripheral> WithConnectIf(this IPeripheral peripheral, ConnectionConfig?config = null) => Observable.Create <IPeripheral>(ob =>
        {
            if (peripheral.IsConnected())
            {
                ob.Respond(peripheral);
                return(Disposable.Empty);
            }

            var sub1 = peripheral
                       .WhenConnected()
                       .Take(1)
                       .Subscribe(_ => ob.Respond(peripheral));

            var sub2 = peripheral
                       .WhenConnectionFailed()
                       .Subscribe(ob.OnError);

            peripheral.Connect(config);

            return(Disposable.Create(() =>
            {
                sub1.Dispose();
                sub2.Dispose();
                if (peripheral.Status != ConnectionState.Connected)
                {
                    peripheral.CancelConnection();
                }
            }));
        });
        public void ConnectPrinter(IPeripheral selectedPeripheral)
        {
            if (!selectedPeripheral.IsConnected())
            {
                selectedPeripheral.Connect();
            }

            _perifDisposable = selectedPeripheral.WhenAnyCharacteristicDiscovered().Subscribe((characteristic) =>
            {
                if (characteristic.CanWrite() && !characteristic.CanRead() && !characteristic.CanNotify())
                {
                    _savedCharacteristic = characteristic;

                    IsReady = true;

                    _perifDisposable.Dispose();
                }
            });
        }
Beispiel #3
0
        void ConnectPrinter(IPeripheral selectedPeripheral)
        {
            if (!selectedPeripheral.IsConnected())
            {
                selectedPeripheral.Connect();
            }

            _perifDisposable = selectedPeripheral.WhenAnyCharacteristicDiscovered().Subscribe((characteristic) =>
            {
                //System.Diagnostics.Debug.WriteLine(characteristic.Description); //this is not suppported at this momment, and no neccesary I guess
                if (characteristic.CanWrite() && !characteristic.CanRead() && !characteristic.CanNotify())
                {
                    IsReadyToPrint       = true;
                    _savedCharacteristic = characteristic;
                    System.Diagnostics.Debug.WriteLine($"Writing {characteristic.Uuid} - {characteristic.CanRead()} - {characteristic.CanIndicate()} - {characteristic.CanNotify()}");
                    _perifDisposable.Dispose();
                }
            });
        }
 public static bool IsDisconnected(this IPeripheral peripheral) => !peripheral.IsConnected();
 /// <summary>
 /// Continuously reads RSSI from a connected peripheral
 /// WARNING: you really don't want to run this with an Android GATT connection
 /// </summary>
 /// <param name="peripheral">The peripheral to read RSSI</param>
 /// <param name="readInterval">The interval to poll the RSSI - defaults to 1 second if not set</param>
 /// <returns>RSSI value every read interval</returns>
 public static IObservable <int> ReadRssiContinuously(this IPeripheral peripheral, TimeSpan?readInterval = null) => Observable
 .Interval(readInterval ?? TimeSpan.FromSeconds(1))
 .Where(_ => peripheral.IsConnected())
 .Select(_ => peripheral.ReadRssi())
 .Switch();