コード例 #1
0
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            return(Observable.Create <IScanResult>(ob =>
            {
                this.IsScanning = true;

                var sub = this
                          .WhenRadioReady()
                          .Where(rdo => rdo != null)
                          .Select(_ => this.DoScan(config))
                          .Switch()
                          .Subscribe(ob.OnNext);

                return () =>
                {
                    this.IsScanning = false;
                    sub.Dispose();
                };
            }));
        }
コード例 #2
0
ファイル: Adapter.cs プロジェクト: fleetcomplete/bluetoothle
        public IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            var observer = Observable.Create <IScanResult>(ob =>
            {
                var sub         = this.ScanListen().Subscribe(ob.OnNext);
                this.IsScanning = true; // this will actually fire off the scanner

                return(() =>
                {
                    this.IsScanning = false;
                    sub.Dispose();
                });
            });

            if (config?.ServiceUuid != null)
            {
                observer = observer.Where(x => x.AdvertisementData?.ServiceUuids.Contains(config.ServiceUuid.Value) ?? false);
            }

            return(observer);
        }
コード例 #3
0
        protected virtual IObservable <IScanResult> DoScan(ScanConfig config) => Observable.Create <IScanResult>(ob =>
        {
            this.context.Clear();

            return(this.context
                   .CreateAdvertisementWatcher(config)
                   .Subscribe(async args => // CAREFUL
            {
                var device = this.context.GetDevice(args.BluetoothAddress);
                if (device == null)
                {
                    var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                    if (btDevice != null)
                    {
                        device = this.context.AddDevice(args.BluetoothAddress, btDevice);
                    }
                }
                if (device != null)
                {
                    var adData = new AdvertisementData(args);
                    var scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData);
                    ob.OnNext(scanResult);
                }
            }));
        });
コード例 #4
0
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            config = config ?? new ScanConfig();

            if (this.Status != AdapterStatus.PoweredOn)
            {
                throw new ArgumentException("Your adapter status is " + this.Status);
            }

            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an existing scan");
            }

            if (config.ScanType == BleScanType.Background && (config.ServiceUuids == null || config.ServiceUuids.Count == 0))
            {
                throw new ArgumentException("Background scan type set but not ServiceUUID");
            }

            return(Observable.Create <IScanResult>(ob =>
            {
                this.context.Clear();
                var scan = this.context
                           .ScanResultReceived
                           .AsObservable()
                           .Subscribe(ob.OnNext);

                if (config.ServiceUuids == null || config.ServiceUuids.Count == 0)
                {
                    this.context.Manager.ScanForPeripherals(null, new PeripheralScanningOptions {
                        AllowDuplicatesKey = true
                    });
                }
                else
                {
                    var uuids = config.ServiceUuids.Select(o => o.ToCBUuid()).ToArray();
                    if (config.ScanType == BleScanType.Background)
                    {
                        this.context.Manager.ScanForPeripherals(uuids);
                    }
                    else
                    {
                        this.context.Manager.ScanForPeripherals(uuids, new PeripheralScanningOptions {
                            AllowDuplicatesKey = true
                        });
                    }
                }
                this.ToggleScanStatus(true);

                return () =>
                {
                    this.context.Manager.StopScan();
                    scan.Dispose();
                    this.ToggleScanStatus(false);
                };
            }));
        }
コード例 #5
0
ファイル: Adapter.cs プロジェクト: Miyuyami/bluetoothle
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            config = config ?? new ScanConfig();
            return(this.context.Scan(config));
        }
コード例 #6
0
ファイル: Adapter.cs プロジェクト: zyx5256/bluetoothle
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            return(Observable.Create <IScanResult>(ob =>
            {
                this.IsScanning = true;
                this.context.Clear();

                var sub = this
                          .WhenRadioReady()
                          .Where(rdo => rdo != null)
                          .Select(_ => this.CreateScanner(config))
                          .Switch()
                          .Subscribe(
                    async args =>     // CAREFUL
                {
                    var device = this.context.GetDevice(args.BluetoothAddress);
                    if (device == null)
                    {
                        var btDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
                        if (btDevice != null)
                        {
                            device = this.context.AddOrGetDevice(btDevice);
                        }
                    }
                    if (device != null)
                    {
                        var adData = new AdvertisementData(args);
                        var scanResult = new ScanResult(device, args.RawSignalStrengthInDBm, adData);
                        ob.OnNext(scanResult);
                    }
                },
                    ob.OnError
                    );

                var stopSub = this.scanSubject.Subscribe(_ =>
                {
                    this.IsScanning = false;
                    sub?.Dispose();
                    ob.OnCompleted();
                });

                return () =>
                {
                    this.IsScanning = false;
                    sub?.Dispose();
                    stopSub?.Dispose();
                };
            }));
        }
コード例 #7
0
ファイル: Adapter.cs プロジェクト: nviswanath24/bluetoothle
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            this.isScanning = true;
            return(this.context
                   .Scan(config ?? new ScanConfig())
                   .Finally(() => this.isScanning = false));
        }
コード例 #8
0
 /// <summary>
 /// This method wraps the traditional scan, but waits for the adapter to be ready before initiating scan
 /// </summary>
 /// <param name="adapter">The adapter to scan with</param>
 /// <param name="restart">Stops any current scan running</param>
 /// <param name="config">ScanConfig parameters you would like to use</param>
 /// <returns></returns>
 public static IObservable <IScanResult> ScanExtra(this IAdapter adapter, ScanConfig config = null, bool restart = false) => adapter
 .WhenStatusChanged()
 .Where(x => x == AdapterStatus.PoweredOn)
 .Select(_ =>
 {
     if (restart && adapter.IsScanning)
     {
         adapter.StopScan();     // need a pause to wait for scan to end
     }
     return(adapter.Scan(config));
 })
 .Switch();
コード例 #9
0
ファイル: Adapter.cs プロジェクト: nviswanath24/bluetoothle
        public override IObservable <IScanResult> Scan(ScanConfig config = null) => Observable.Create <IScanResult>(ob =>
        {
            this.deviceManager.Clear();
            var handler = new EventHandler <AdapterLeScanResultChangedEventArgs>((sender, args) =>
            {
                var device = this.deviceManager.GetDevice(args.DeviceData);
                ob.OnNext(new ScanResult(args.DeviceData, device));
            });
            BluetoothAdapter.ScanResultChanged += handler;
            BluetoothAdapter.StartLeScan();

            return(() =>
            {
                BluetoothAdapter.StopLeScan();
                BluetoothAdapter.ScanResultChanged -= handler;
            });
        });
コード例 #10
0
        IObservable <BluetoothLEAdvertisementReceivedEventArgs> CreateScanner(ScanConfig config)
        => Observable.Create <BluetoothLEAdvertisementReceivedEventArgs>(ob =>
        {
            this.context.Clear();
            config = config ?? new ScanConfig {
                ScanType = BleScanType.Balanced
            };

            var adWatcher = new BluetoothLEAdvertisementWatcher();
            if (config.ServiceUuids != null)
            {
                foreach (var serviceUuid in config.ServiceUuids)
                {
                    adWatcher.AdvertisementFilter.Advertisement.ServiceUuids.Add(serviceUuid);
                }
            }

            switch (config.ScanType)
            {
            case BleScanType.Balanced:
                adWatcher.ScanningMode = BluetoothLEScanningMode.Active;
                break;

            case BleScanType.Background:
            case BleScanType.LowLatency:
            case BleScanType.LowPowered:
                adWatcher.ScanningMode = BluetoothLEScanningMode.Passive;
                break;
            }
            var handler = new TypedEventHandler <BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs>
                              ((sender, args) => ob.OnNext(args)
                              );

            adWatcher.Received += handler;
            adWatcher.Start();

            return(() =>
            {
                adWatcher.Stop();
                adWatcher.Received -= handler;
            });
        });
コード例 #11
0
        public override IObservable <IScanResult> Scan(ScanConfig config)
        {
            if (this.IsScanning)
            {
                throw new ArgumentException("There is already an active scan");
            }

            config = config ?? new ScanConfig();
            return(Observable.Create <IScanResult>(ob =>
            {
                this.context.Devices.Clear();

                var scan = this.ScanListen().Subscribe(ob.OnNext);
                this.context.StartScan(config);
                this.scanStatusChanged.OnNext(true);

                return () =>
                {
                    this.context.StopScan();
                    scan.Dispose();
                    this.scanStatusChanged.OnNext(false);
                };
            }));
        }
コード例 #12
0
 public abstract IObservable <IScanResult> Scan(ScanConfig config = null);
コード例 #13
0
 public override IObservable <IScanResult> Scan(ScanConfig config = null)
 {
     throw new NotImplementedException();
 }
コード例 #14
0
        //public static IObservable<IScanResult> ScanTimed(this IAdapter adapter, TimeSpan scanTime, ScanConfig config = null) => adapter
        //    .Scan(config)
        //    .Take(scanTime);

        /// <summary>
        /// Scans only for distinct devices instead of repeating each device scan response - this will only give you devices, not RSSI or ad packets
        /// </summary>
        /// <param name="adapter"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static IObservable <IDevice> ScanForUniqueDevices(this IAdapter adapter, ScanConfig config = null) => adapter
        .Scan(config)
        .Distinct(x => x.Device.Uuid)
        .Select(x => x.Device);
コード例 #15
0
 /// <summary>
 /// Runs BLE scan for a set timespan then pauses for configured timespan before starting again
 /// </summary>
 /// <param name="adapter"></param>
 /// <param name="scanTime"></param>
 /// <param name="scanPauseTime"></param>
 /// <param name="config"></param>
 /// <returns></returns>
 public static IObservable <IScanResult> ScanInterval(this IAdapter adapter, TimeSpan scanTime, TimeSpan scanPauseTime, ScanConfig config = null) => Observable.Create <IScanResult>(ob =>
コード例 #16
0
ファイル: Adapter.cs プロジェクト: LuanNg/bluetoothle-1
 public override IObservable <IScanResult> Scan(ScanConfig config = null) => Observable.Create <IScanResult>(ob =>
 {
     return(() =>
     {
     });
 });