protected async Task TeardownBluetooth()
        {
            if (_axLEManager != null)
            {
                await _axLEManager.StopScan();

                _axLEManager = null;
            }

            if (_bluetoothManager != null)
            {
                _bluetoothManager.Dispose();
                _bluetoothManager = null;
            }
        }
Exemplo n.º 2
0
        async Task Run()
        {
            this.started = DateTime.Now;

            if (imageTest)
            {
                Bitmap bitmap = GetBitmap();
                GetBitmapData(bitmap);
                Console.WriteLine("DEBUG: Stopped after image test - press ENTER to exit...");
                Console.In.Read();
                return;
            }

            _bluetoothManager = new BluetoothManager();
            _axLEManager      = new AxLEManager(_bluetoothManager)
            {
                RssiFilter = 0 // 40 Scan only nearby AxLEs
            };
            _axLEManager.DeviceFound += AxLEManager_DeviceFound;

            // Start the scanner
            try
            {
                await _axLEManager.StartScan();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: Problem starting scanner: " + e.Message);
                return;
            }

            try
            {
                while (!await MainTasks())
                {
                }
            }
            finally
            {
                await _axLEManager.StopScan();
            }
        }
        async Task SetupAxLEManager()
        {
            if (_axLEManager != null)
            {
                await _axLEManager.StopScan();
            }

            if (_bluetoothManager != null)
            {
                _bluetoothManager.Dispose();
            }

            _bluetoothManager = new OpenMovement.AxLE.Comms.Bluetooth.Mobile.Android.BluetoothManager(CrossBluetoothLE.Current);
            _axLEManager      = new AxLEManager(_bluetoothManager, 30000);

            _axLEManager.DeviceFound += (object sender, string macAddress) => {
                try
                {
                    //Console.WriteLine($"DeviceFound:{macAddress}");

                    _observedDevices.Add(macAddress);
                    DeploymentUser deploymentUser = _deploymentUsers.SingleOrDefault(du => du.device.macAddress == macAddress);

                    // if deploymentUser is found
                    if (deploymentUser != default(DeploymentUser))
                    {
                        RunOnUiThread(() =>
                        {
                            Console.WriteLine($"CONNECTION: SHOW AVATAR");
                            _webViewClient.DeviceJoined(_webView, deploymentUser);
                        });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error:" + e.ToString());
                    Crashes.TrackError(e);
                }
            };

            _axLEManager.DeviceLost += (object sender, string macAddress) =>
            {
                try
                {
                    _observedDevices.Remove(macAddress);
                    DeploymentUser deploymentUser = _deploymentUsers.SingleOrDefault(du => du.device.macAddress == macAddress);
                    // if deploymentUser is found
                    if (deploymentUser != default(DeploymentUser))
                    {
                        RunOnUiThread(() =>
                        {
                            Console.WriteLine($"CONNECTION: HIDE AVATAR");
                            _webViewClient.DeviceLeft(_webView, deploymentUser);
                        });
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error:" + e.ToString());
                    Crashes.TrackError(e);
                }
            };

            _axLEManager.SwitchToHighPowerScan();
            _axLEManager.StartScan();
        }