Exemple #1
0
        public ManagerContext(IServiceProvider services,
                              BleConfiguration config,
                              ILogger <IBleManager> logger)
        {
            this.Services = services;
            this.logger   = logger;

            this.managerLazy = new Lazy <CBCentralManager>(() =>
            {
                if (!AppleExtensions.HasPlistValue("NSBluetoothPeripheralUsageDescription"))
                {
                    this.logger.LogCritical("NSBluetoothPeripheralUsageDescription needs to be set - you will likely experience a native crash after this log");
                }

                var background = services.GetService(typeof(IBleDelegate)) != null;
                if (!background)
                {
                    return(new CBCentralManager(this, null));
                }

                if (!AppleExtensions.HasPlistValue("NSBluetoothAlwaysUsageDescription", 13))
                {
                    this.logger.LogCritical("NSBluetoothAlwaysUsageDescription needs to be set - you will likely experience a native crash after this log");
                }

                var opts = new CBCentralInitOptions
                {
                    ShowPowerAlert    = config.iOSShowPowerAlert,
                    RestoreIdentifier = config.iOSRestoreIdentifier ?? "shinyble"
                };

                return(new CBCentralManager(this, null, opts));
            });
        }
Exemple #2
0
        public ManagerContext(ShinyCoreServices services, BleConfiguration config)
        {
            this.Configuration = config;
            this.services      = services;

            this.devices           = new ConcurrentDictionary <string, Peripheral>();
            this.peripheralSubject = new Subject <NamedMessage <Peripheral> >();
            this.Manager           = services.Android.GetBluetooth();
            //this.StatusChanged
            //    .Skip(1)
            //    .SubscribeAsync(status => Log.SafeExecute(
            //        async () => await this.sdelegate.Value?.OnAdapterStateChanged(status)
            //    ));
        }
Exemple #3
0
        public CentralContext(IServiceProvider serviceProvider,
                              AndroidContext context,
                              IMessageBus messageBus,
                              BleConfiguration config)
        {
            this.Android       = context;
            this.Configuration = config;
            this.Manager       = context.GetBluetooth();

            this.sdelegate         = new Lazy <IBleDelegate>(() => serviceProvider.Resolve <IBleDelegate>());
            this.devices           = new ConcurrentDictionary <string, Peripheral>();
            this.peripheralSubject = new Subject <NamedMessage <Peripheral> >();
            this.messageBus        = messageBus;

            this.StatusChanged
            .Skip(1)
            .SubscribeAsync(status => Log.SafeExecute(
                                async() => await this.sdelegate.Value?.OnAdapterStateChanged(status)
                                ));
        }
Exemple #4
0
        public PerformanceViewModel(IBleManager centralManager, BleConfiguration configuration)
        {
            this.centralManager = centralManager;

            if (this.IsAndroid)
            {
                this.AndroidUseInternalSyncQueue = configuration.AndroidUseInternalSyncQueue;
                this.AndroidUseMainThread        = configuration.AndroidShouldInvokeOnMainThread;

                this.WhenAnyValue(x => x.AndroidUseMainThread)
                .Skip(1)
                .Subscribe(x => configuration.AndroidShouldInvokeOnMainThread = x);

                this.WhenAnyValue(x => x.AndroidUseInternalSyncQueue)
                .Skip(1)
                .Subscribe(x => configuration.AndroidUseInternalSyncQueue = x);
            }

            this.WhenAnyValue(x => x.IsRunning)
            .Skip(1)
            .Subscribe(x =>
            {
                if (!x)
                {
                    this.speedSub?.Dispose();
                }
                else
                {
                    this.speedSub = Observable.Interval(TimeSpan.FromSeconds(2)).Subscribe(_ =>
                    {
                        this.Speed = (this.bytes / 2).Bytes().Humanize("0.0");
                        Interlocked.Exchange(ref this.bytes, 0);
                    });
                }
            });

            this.Permissions = ReactiveCommand.CreateFromTask(async() =>
                                                              this.Status = await this.centralManager.RequestAccess().ToTask()
                                                              );
            this.WriteTest = this.DoWrite(true);
            this.WriteWithoutResponseTest = this.DoWrite(false);
            this.ReadTest = this.DoWork("Read", async(ch, ct) =>
            {
                var read = await ch.Read().ToTask(ct);
                return(read.Data?.Length ?? 0);
            });

            this.NotifyTest = ReactiveCommand.CreateFromTask(
                async() =>
            {
                this.IsRunning = true;
                this.Errors    = 0;
                this.Packets   = 0;

                var characteristic = await this.SetupCharacteristic(this.cancelSrc.Token);
                this.Info          = "Running Notify Test";

                this.notifySub = characteristic
                                 .Notify(true)
                                 .Where(x => x.Type == CharacteristicResultType.Notification)
                                 .Subscribe(x =>
                {
                    Interlocked.Add(ref this.bytes, x.Data?.Length ?? 0);
                    this.Packets++;
                });
            },
                this.CanRun()
                );

            this.Stop = ReactiveCommand.Create(
                () =>
            {
                this.IsRunning = false;
                this.peripheral?.CancelConnection();
                this.Info = "Test Stopped";
                this.cancelSrc?.Cancel();
                this.notifySub?.Dispose();
                this.notifySub = null;
            },
                this.WhenAny(
                    x => x.IsRunning,
                    x => x.GetValue()
                    )
                );
        }