コード例 #1
0
 public CentralContext(AndroidContext context, BleCentralConfiguration config = null)
 {
     this.Configuration = config ?? new BleCentralConfiguration();
     this.Manager       = (BluetoothManager)context.AppContext.GetSystemService(global::Android.App.Application.BluetoothService);
     this.Android       = context;
     this.devices       = new ConcurrentDictionary <string, IPeripheral>();
 }
コード例 #2
0
        /// <summary>
        /// Register the ICentralManager service that allows you to connect to other BLE devices
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static bool UseBleCentral(this IServiceCollection builder, BleCentralConfiguration config = null)
        {
#if NETSTANDARD
            return(false);
#else
            builder.RegisterModule(new BleCentralShinyModule(config ?? new BleCentralConfiguration()));
            return(true);
#endif
        }
コード例 #3
0
        public CentralContext(IServiceProvider serviceProvider,
                              AndroidContext context,
                              IMessageBus messageBus,
                              BleCentralConfiguration config)
        {
            this.Android       = context;
            this.Configuration = config;
            this.Manager       = context.GetBluetooth();

            this.sdelegate         = new Lazy <IBleCentralDelegate>(() => serviceProvider.Resolve <IBleCentralDelegate>());
            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)
                                ));
        }
コード例 #4
0
        public PerformanceViewModel(ICentralManager centralManager, BleCentralConfiguration 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()
                    )
                );
        }
コード例 #5
0
 /// <summary>
 /// Register the ICentralManager service that allows you to connect to other BLE devices - Delegates used here are intended for background usage
 /// </summary>
 /// <typeparam name="TCentralDelegate"></typeparam>
 /// <param name="services"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public static bool UseBleCentral <TCentralDelegate>(this IServiceCollection services, BleCentralConfiguration config = null) where TCentralDelegate : class, IBleCentralDelegate
 => services.UseBleCentral(typeof(TCentralDelegate), config);
コード例 #6
0
 /// <summary>
 /// Register the ICentralManager service that allows you to connect to other BLE devices - Delegates used here are intended for background usage
 /// </summary>
 /// <param name="services"></param>
 /// <param name="config"></param>
 /// <param name="delegateType"></param>
 /// <returns></returns>
 public static bool UseBleCentral(this IServiceCollection services, Type delegateType, BleCentralConfiguration config = null)
 {
     if (services.UseBleCentral(config))
     {
         if (delegateType != null)
         {
             services.AddSingleton(typeof(IBleCentralDelegate), delegateType);
         }
         return(true);
     }
     return(false);
 }
コード例 #7
0
 /// <summary>
 /// Register the ICentralManager service that allows you to connect to other BLE devices - Delegates used here are intended for background usage
 /// </summary>
 /// <typeparam name="TCentralDelegate"></typeparam>
 /// <param name="builder"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public static bool UseBleCentral <TCentralDelegate>(this IServiceCollection builder, BleCentralConfiguration config = null) where TCentralDelegate : class, IBleCentralDelegate
 {
     if (builder.UseBleCentral(config))
     {
         builder.AddSingleton <IBleCentralDelegate, TCentralDelegate>();
         return(true);
     }
     return(false);
 }
コード例 #8
0
        public CentralContext(IServiceProvider serviceProvider, AndroidContext context, BleCentralConfiguration config)
        {
            this.Android       = context;
            this.Configuration = config;
            this.Manager       = context.GetBluetooth();

            this.sdelegate         = new Lazy <IBleCentralDelegate>(() => serviceProvider.Resolve <IBleCentralDelegate>());
            this.devices           = new ConcurrentDictionary <string, IPeripheral>();
            this.statusSubject     = new Subject <AccessState>();
            this.peripheralSubject = new Subject <NamedMessage <IPeripheral> >();
        }