Esempio n. 1
0
        public override async void OnAppearing()
        {
            // if we're budy or have a valid connection, then no-op
            if (IsBusy || (m_device != null && m_device.State != ConnectionState.Disconnected))
            {
                return;
            }

            IsBusy = true;
            CloseConnection();
            Log.Debug("Connecting to device. address={0}", m_peripheral.Address);
            m_device = await m_ble.ConnectToDevice(m_peripheral.Model);

            RaisePropertyChanged(nameof(DeviceConnectionState));
            m_device.Subscribe(
                Observer.Create(
                    ( ConnectionState c ) =>
            {
                Log.Info("Device state changed. address={0} status={1}", m_peripheral.Address, c);
                RaisePropertyChanged(nameof(DeviceConnectionState));
            }));
            Log.Debug("Connected to device. address={0} status={1}", m_peripheral.Address, m_device.State);
            if (m_device.State == ConnectionState.Connected || m_device.State == ConnectionState.Connecting)
            {
                var services = (await m_device.ListAllServices()).ToList();
                foreach (var serviceId in services)
                {
                    if (Services.Any(viewModel => viewModel.Guid.Equals(serviceId)))
                    {
                        continue;
                    }
                    Services.Add(new BleGattServiceViewModel(serviceId, m_device, m_dialog));
                }
                if (services.Count == 0)
                {
                    m_dialog.Toast("No services found");
                }
            }
            else
            {
                Log.Warn(
                    "Error connecting to device. address={0} state={1}",
                    m_device.Address.EncodeToBase16String(),
                    m_device.State);
                m_dialog.Toast("Error connecting to device");
            }
            Log.Debug("Read services. address={0} status={1}", m_peripheral.Address, m_device.State);
            IsBusy = false;
            RaisePropertyChanged(nameof(DeviceConnectionState));
        }
        public async Task OpenConnection()
        {
            // if we're busy or have a valid connection, then no-op
            if (IsBusy || m_gattServer != null && m_gattServer.State != ConnectionState.Disconnected)
            {
                //Log.Debug( "OnAppearing. state={0} isbusy={1}", m_gattServer?.State, IsBusy );
                return;
            }

            CloseConnection();
            IsBusy = true;

            var connection = await m_bleAdapter.ConnectToDevice(
                device : m_peripheral.Model,
                timeout : TimeSpan.FromSeconds(CONNECTION_TIMEOUT_SECONDS),
                progress : progress => { Connection = progress.ToString(); });

            if (connection.IsSuccessful())
            {
                m_gattServer = connection.GattServer;
                Connection   = "Reading Services";
                Log.Debug("Connected to device. id={0} status={1}", m_peripheral.Id, m_gattServer.State);

                m_gattServer.Subscribe(
                    c =>
                {
                    if (c == ConnectionState.Disconnected)
                    {
                        m_dialogManager.Toast("Device disconnected");
                        CloseConnection();
                    }
                    Connection = c.ToString();
                });

                // small possibility the device could disconnect between connecting and getting services and throw somewhere along here
                var services = (await m_gattServer.ListAllServices()).ToList();
                foreach (var serviceId in services)
                {
                    if (Services.Any(viewModel => viewModel.Guid.Equals(serviceId)))
                    {
                        continue;
                    }
                    Services.Add(new BleGattServiceViewModel(serviceId, m_gattServer, m_dialogManager));
                }
                if (Services.Count == 0)
                {
                    m_dialogManager.Toast("No services found");
                }
                Connection = m_gattServer.State.ToString();
            }
            else
            {
                String errorMsg;
                if (connection.ConnectionResult == ConnectionResult.ConnectionAttemptCancelled)
                {
                    errorMsg = "Connection attempt cancelled after {0} seconds (see {1})".F(
                        CONNECTION_TIMEOUT_SECONDS,
                        GetType().Name + ".cs");
                }
                else
                {
                    errorMsg = "Error connecting to device: {0}".F(connection.ConnectionResult);
                }
                Log.Info(errorMsg);
                m_dialogManager.Toast(errorMsg, TimeSpan.FromSeconds(5));
            }
            IsBusy = false;
        }