コード例 #1
0
        public async void RefreshServices()
        {
            IsBusy = true;
            DetectedServices.Clear();
            try
            {
                IList <IService> services = await _microbit.GetServicesAsync();

                foreach (IService service in services)
                {
                    IMicrobitServiceProvider microbitServiceProvider =
                        IdToServiceProviderMappingProvider.ServiceProvider(service);
                    if (microbitServiceProvider != null)
                    {
                        DetectedServices.Add(microbitServiceProvider);
                    }
                }
            }
            catch (Exception e)
            {
                await Application.Current.MainPage.DisplayAlert("Exception", e.Message, "Ok");
            }
            finally
            {
                IsBusy = false;
            }
        }
コード例 #2
0
 public async void Disconnect()
 {
     IsBusy = true;
     try
     {
         if (_adapter.ConnectedDevices.Contains(_microbit))
         {
             await _adapter.DisconnectDeviceAsync(_microbit);
         }
     }
     catch (Exception e)
     {
         await Application.Current.MainPage.DisplayAlert("Exception", e.Message, "Ok");
     }
     finally
     {
         IsBusy = false;
     }
     DetectedServices.Clear();
 }
コード例 #3
0
        public DeviceServicesViewModel(IDevice device)
        {
            _microbit    = device;
            _bluetoothLe = CrossBluetoothLE.Current;
            _adapter     = _bluetoothLe.Adapter;

            _adapter.DeviceConnected      += HandleConnectionStateChanged;
            _adapter.DeviceDisconnected   += HandleConnectionStateChanged;
            _adapter.DeviceConnectionLost += HandleConnectionStateChanged;

            ToggleConnectionCommand = new Command(
                () =>
            {
                if (_microbit.State == DeviceState.Connected)
                {
                    Disconnect();
                }
                else if (_microbit.State == DeviceState.Disconnected)
                {
                    Connect();
                }
            },
                () => !IsBusy);

            ServicesCommand = new Command(async() =>
            {
                DetectedServices.Clear();
                IList <IService> services = await _microbit.GetServicesAsync();
                foreach (IService service in services)
                {
                    IMicrobitServiceProvider microbitServiceProvider = IdToServiceProviderMappingProvider.ServiceProvider(service);
                    if (microbitServiceProvider != null)
                    {
                        DetectedServices.Add(microbitServiceProvider);
                    }
                }
            }, () => !IsBusy
                                          );

            DetectedServices = new ObservableCollection <IMicrobitServiceProvider>();
        }