Ejemplo n.º 1
0
 /// <summary>
 /// Scan for nearby BLE device advertisements that match <paramref name="filter" />. Stop scanning when
 /// <paramref name="ct" /> is cancelled.
 /// </summary>
 /// <param name="adapter">The adapter to use for scanning</param>
 /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
 /// <param name="filter">
 /// Scan filter that will ignore broadcast advertisements that do not match.
 /// <c>new ScanFilter.Factory(){}.CreateFilter()</c>
 /// </param>
 /// <param name="ct">Scan will run continuously until this token is cancelled</param>
 public static Task ScanForBroadcasts(this IBluetoothLowEnergyAdapter adapter, ScanFilter filter,
                                      Action <IBlePeripheral> advertisementDiscovered, CancellationToken ct)
 {
     Contract.Requires <ArgumentNullException>(adapter != null);
     // ReSharper disable once PossibleNullReferenceException
     return(adapter.ScanForBroadcasts(filter, Observer.Create(advertisementDiscovered), ct));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Scan for nearby BLE device advertisements. Stop scanning after <paramref name="timeout" />
 /// </summary>
 /// <param name="adapter">The adapter to use for scanning</param>
 /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
 /// <param name="timeout">cancel scan after this length of time</param>
 public static Task ScanForBroadcasts(this IBluetoothLowEnergyAdapter adapter,
                                      IObserver <IBlePeripheral> advertisementDiscovered, TimeSpan timeout)
 {
     Contract.Requires <ArgumentNullException>(adapter != null);
     // ReSharper disable once PossibleNullReferenceException
     return(adapter.ScanForBroadcasts(advertisementDiscovered, new CancellationTokenSource(timeout).Token));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Attempt to connect to the first device that passes the filter of <paramref name="settings" />, and continue the attempt
        /// until
        /// cancellation token is triggered.
        /// </summary>
        public static async Task <BlePeripheralConnectionRequest> FindAndConnectToDevice(
            [NotNull] this IBluetoothLowEnergyAdapter adapter, ScanSettings settings, CancellationToken ct,
            IProgress <ConnectionProgress> progress = null)
        {
            if (settings.Filter == null)
            {
                throw new ArgumentNullException(
                          nameof(settings),
                          "Scan settings must have a non-null ScanFilter when calling FindAndConnectToDevice");
            }

            var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);

            progress?.Report(ConnectionProgress.SearchingForDevice);
            var device = adapter.DiscoveredPeripherals.FirstOrDefault(p => settings.Filter.Passes(p.Advertisement));

            if (device == null)
            {
                await adapter.ScanForBroadcasts(
                    settings,
                    Observer.Create(
                        (Action <IBlePeripheral>)(p =>
                {
                    device = p;
                    cts.Cancel();
                })),
                    cts.Token).ConfigureAwait(false);
            }

            return(device == null
            ? ct.IsCancellationRequested
               ? new BlePeripheralConnectionRequest(ConnectionResult.ConnectionAttemptCancelled, null)
               : new BlePeripheralConnectionRequest(ConnectionResult.DeviceNotFound, null)
            : await adapter.ConnectToDevice(device, ct, progress).ConfigureAwait(false));
        }
        /// <summary>
        /// Scan for nearby BLE device advertisements. Stop scanning when <paramref name="ct" /> is cancelled.
        /// </summary>
        /// <param name="adapter">The adapter to use for scanning</param>
        /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
        /// <param name="ct">Scan until this token is cancelled</param>
        public static Task ScanForBroadcasts([NotNull] this IBluetoothLowEnergyAdapter adapter,
                                             Action <IBlePeripheral> advertisementDiscovered, CancellationToken ct)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(adapter.ScanForBroadcasts(Observer.Create(advertisementDiscovered), ct));
        }
        /// <summary>
        /// Scan for nearby BLE device advertisements. Stop scanning when <paramref name="ct" /> is cancelled.
        /// </summary>
        /// <param name="adapter">The adapter to use for scanning</param>
        /// <param name="scanSettings">
        /// Scan settings to configure scan mode and filter out certain advertisements, see:
        /// <see cref="ScanFilter" />
        /// </param>
        /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
        /// <param name="timeout">
        /// cancel scan after this length of time. If <c>null</c>, stop scanning after
        /// <see cref="BluetoothLowEnergyUtils.DefaultScanTimeout" />
        /// </param>
        public static Task ScanForBroadcasts([NotNull] this IBluetoothLowEnergyAdapter adapter,
                                             ScanSettings scanSettings, Action <IBlePeripheral> advertisementDiscovered,
                                             TimeSpan?timeout = null)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(adapter.ScanForBroadcasts(scanSettings, Observer.Create(advertisementDiscovered), timeout));
        }
        /// <summary>
        /// Scan for nearby BLE device advertisements that match <paramref name="filter" />.
        /// </summary>
        /// <param name="adapter">The adapter to use for scanning</param>
        /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
        /// <param name="filter">
        /// Scan filter that will ignore broadcast advertisements that do not match.
        /// <see cref="ScanFilter" />
        /// </param>
        /// <param name="ct">Scan will run continuously until this token is cancelled</param>
        public static Task ScanForBroadcasts([NotNull] this IBluetoothLowEnergyAdapter adapter, IScanFilter filter,
                                             IObserver <IBlePeripheral> advertisementDiscovered, CancellationToken ct)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(adapter.ScanForBroadcasts(new ScanSettings {
                Filter = filter
            }, advertisementDiscovered, ct));
        }
        /// <summary>
        /// Scan for nearby BLE device advertisements. Stop scanning after <paramref name="timeout" />
        /// </summary>
        /// <param name="adapter">The adapter to use for scanning</param>
        /// <param name="advertisementDiscovered">Callback to notify for each discovered advertisement</param>
        /// <param name="timeout">
        /// cancel scan after this length of time. If <c>null</c>, stop scanning after
        /// <see cref="BluetoothLowEnergyUtils.DefaultScanTimeout" />
        /// </param>
        public static Task ScanForBroadcasts([NotNull] this IBluetoothLowEnergyAdapter adapter,
                                             IObserver <IBlePeripheral> advertisementDiscovered,
                                             TimeSpan?timeout = null)
        {
            if (adapter == null)
            {
                throw new ArgumentNullException(nameof(adapter));
            }

            return(adapter.ScanForBroadcasts(
                       advertisementDiscovered,
                       new CancellationTokenSource(timeout ?? BluetoothLowEnergyUtils.DefaultScanTimeout).Token));
        }
Ejemplo n.º 8
0
        public async void BLE_Scan()
        {
            /* To be inserted in a fragment */
            LocationManager lm = (LocationManager)g_context.GetSystemService(Context.LocationService);

            if (lm.IsProviderEnabled(LocationManager.GpsProvider) && lm.IsProviderEnabled(LocationManager.NetworkProvider))
            {
                g_BLEAdapterObj_scanResponsesObserver = OnBLEAdapter_ScanResponses;
                await g_BLEAdapterObj.ScanForBroadcasts(Observer.Create <IBlePeripheral>(g_BLEAdapterObj_scanResponsesObserver), TimeSpan.FromSeconds(10));
            }
            else
            {
                Library.CloseApplication(g_activity);
            }
        }
Ejemplo n.º 9
0
        public static async Task ScanForDevices()
        {
            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
            await bleAdapter.ScanForBroadcasts(
                // providing ScanSettings is optional
                new ScanSettings()
            {
                // Setting the scan mode is currently only applicable to Android and has no effect on other platforms.
                // If not provided, defaults to ScanMode.Balanced
                Mode = ScanMode.LowPower,

                // Optional scan filter to ensure that the observer will only receive peripherals
                // that pass the filter. If you want to scan for everything around, omit the filter.
                Filter = new ScanFilter()
                {
                    // peripherals must advertise at-least-one of any GUIDs in this list
                    //This is how we know its one of our rover
                    AdvertisedServiceIsInList = new List <Guid>()
                    {
                        Service
                    }
                },

                // ignore repeated advertisements from the same device during this scan
                IgnoreRepeatBroadcasts = true
            },
                // Your IObserver<IBlePeripheral> or Action<IBlePeripheral> will be triggered for each discovered
                // peripheral based on the provided scan settings and filter (if any).
                (IBlePeripheral peripheral) =>
            {
                devices.AddAll(BleService.bleAdapter.DiscoveredPeripherals.Where(p => (!string.IsNullOrEmpty(p.Advertisement.DeviceName)) && IsUniqueAddress(p.Address)));
            },
                // Provide a CancellationToken to stop the scan, or use the overload that takes a TimeSpan.
                // If you omit this argument, the scan will timeout after BluetoothLowEnergyUtils.DefaultScanTimeout
                cts.Token
                );
        }