Example #1
0
        public async Task <bool> ScanAsync(CancellationToken token)
        {
            using (await _asyncLock.LockAsync())
            {
                IsScanning = true;

                try
                {
                    var infraScan     = _infraredDeviceManager.ScanAsync(FoundDevice, token);
                    var bluetoothScan = _bluetoothDeviceManager.ScanAsync(FoundDevice, token);

                    await Task.WhenAll(infraScan, bluetoothScan);

                    return(infraScan.Result && bluetoothScan.Result);
                }
                catch (Exception)
                {
                    return(false);
                }
                finally
                {
                    IsScanning = false;
                }
            }

            async Task FoundDevice(DeviceType deviceType, string deviceName, string deviceAddress, byte[] deviceData)
            {
                using (await _foundDeviceLock.LockAsync())
                {
                    if (Devices.Any(d => d.DeviceType == deviceType && d.Address == deviceAddress))
                    {
                        return;
                    }

                    var device = _deviceFactory(deviceType, deviceName, deviceAddress, deviceData);
                    if (device != null)
                    {
                        await _deviceRepository.InsertDeviceAsync(device.DeviceType, device.Name, device.Address, deviceData);

                        await _uiThreadService.RunOnMainThread(() => Devices.Add(device));
                    }
                }
            }
        }