public async Task <ScanResponseEventArgs> ExecuteAsync(ushort manufacturerId, CancellationToken cancellationToken, int timeout = DefaultTimeout)
        {
            Logger?.LogDebug($"Discover device by Manufacturer ID {manufacturerId}");

            var taskCompletionSource = new TaskCompletionSource <ScanResponseEventArgs>();

            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                cancellationTokenSource.CancelAfter(timeout);

                void OnScanResponse(object sender, ScanResponseEventArgs e)
                {
                    var advertisementData             = BleAdvertisingDataParser.Parse(e.data);
                    var manufacturerAdvertisementData = advertisementData.SingleOrDefault(x => x.Type == BleAdvertisingDataType.ManufacturerSpecificData);

                    if (manufacturerAdvertisementData?.GetManufacturerId() == manufacturerId)
                    {
                        taskCompletionSource.SetResult(e);
                    }
                }

                try
                {
                    BgLib.BLEEventGAPScanResponse += OnScanResponse;

                    using (cancellationTokenSource.Token.Register(() => taskCompletionSource.SetCanceled(), false))
                    {
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1));
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPDiscover(1));

                        return(await taskCompletionSource.Task.ConfigureAwait(false));
                    }
                }
                finally
                {
                    BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPEndProcedure());

                    BgLib.BLEEventGAPScanResponse -= OnScanResponse;
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <ScanResponseEventArgs> ExecuteAsync(byte[] serviceUuid, CancellationToken cancellationToken, int timeout = DefaultTimeout)
        {
            Logger?.LogDebug($"Discover device by service Uuid {serviceUuid.ToHexString()}");

            var taskCompletionSource = new TaskCompletionSource <ScanResponseEventArgs>();

            using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
            {
                cancellationTokenSource.CancelAfter(timeout);

                void OnScanResponse(object sender, ScanResponseEventArgs e)
                {
                    var advertisementData = BleAdvertisingDataParser.Parse(e.data);

                    if (advertisementData.Where(x => ServiceUuidAdvertisements.Contains(x.Type)).Any(x => x.Data.SequenceEqual(serviceUuid)))
                    {
                        taskCompletionSource.SetResult(e);
                    }
                }

                try
                {
                    BgLib.BLEEventGAPScanResponse += OnScanResponse;

                    using (cancellationTokenSource.Token.Register(() => taskCompletionSource.SetCanceled(), false))
                    {
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPSetScanParameters(0xC8, 0xC8, 1));
                        BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPDiscover(1));

                        return(await taskCompletionSource.Task.ConfigureAwait(false));
                    }
                }
                finally
                {
                    BgLib.SendCommand(BleModuleConnection.SerialPort, BgLib.BLECommandGAPEndProcedure());

                    BgLib.BLEEventGAPScanResponse -= OnScanResponse;
                }
            }
        }