Example #1
0
        /// <summary>
        /// Start an auto-connection sequence
        /// </summary>
        private async void StartAutoConnectionRoutine()
        {
            Console.WriteLine("Started auto connect !!");
            if (ConnectedDevice != null)
            {
                await ConnectedDevice.Disconnect();

                ConnectedDevice = null;
            }

            while (ConnectedDevice == null && List.Count > 0)
            {
                ConnectedDevice = await BacCommunication.CurrentRepository.StartBluetoothLeAutoConnection(List);

                if (ConnectedDevice != null)
                {
                    var wheelDiameter = await ConnectedDevice.Read(227);

                    if (Math.Abs(wheelDiameter - 279.4) > 0.1)
                    {
                        try
                        {
                            await ConnectedDevice.Write(227, (short)279.4);
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                            UnsubscribeToDevice();
                            HandleConnectionState();
                        }
                    }

                    var gearRatio = await ConnectedDevice.Read(226) / 256;

                    if (gearRatio != 1)
                    {
                        try
                        {
                            await ConnectedDevice.Write(226, 1);
                        }
                        catch (Exception exception)
                        {
                            Console.WriteLine(exception);
                            UnsubscribeToDevice();
                            HandleConnectionState();
                        }
                    }
                }
            }
            if (ConnectedDevice == null)
            {
                return;
            }
            App.LocalDevice = ConnectedDevice;
            HandleConnectionState();
            SubscribeToConnectedDevice();
        }