public SmartSwitchViewModel() { // instantiate/retrieve Bluetooth manager _btManager = DependencyService.Get <IBluetoothManager>(); _devices = new List <DeviceInfo>(); _devices.Add(SelectedDevice); // refresh command Refresh = new Command(async() => { try { // disconnect the Bluetooth mnager from any existing connections _btManager.Disconnect(); ErrorMessage = string.Empty; // get the paired devices and select the first one Devices = await _btManager.GetPairedDevices(); } catch (Exception ex) { ErrorMessage = ex.Message; } if (Devices.Count > 0) { // This was changed for Xamarin.Forms // SelectedDevice = Devices[0]; // SelectedDeviceIndex = -1; ; SelectedDeviceIndex = 0; } // Notify the listeners of the affected properties RaisePropertyChanged("CanConnect"); RaisePropertyChanged("IsConnected"); }); // Connect command Connect = new Command(async() => await ConnectToBT(), () => CanConnect); // there is no command for OnOff as WindowsPhone does not support it for toggleSwitch // instead IsOn property is used. // if you wish to use plug-in and a command to launch web browser, this may be helpful // http://stackoverflow.com/questions/16616774/mvvmcross-how-to-navigate-to-something-besides-a-viewmodel }
public SmartSwitchViewModel() { // instantiate/retrieve Bluetooth manager _btManager = DependencyService.Get<IBluetoothManager>(); _devices = new List<DeviceInfo>(); _devices.Add(SelectedDevice); // refresh command Refresh = new Command( async () => { try { // disconnect the Bluetooth mnager from any existing connections _btManager.Disconnect(); ErrorMessage = string.Empty; // get the paired devices and select the first one Devices = await _btManager.GetPairedDevices(); } catch (Exception ex) { ErrorMessage = ex.Message; } if (Devices.Count > 0) { // This was changed for Xamarin.Forms // SelectedDevice = Devices[0]; // SelectedDeviceIndex = -1; ; SelectedDeviceIndex = 0; } // Notify the listeners of the affected properties RaisePropertyChanged("CanConnect"); RaisePropertyChanged("IsConnected"); }); // Connect command Connect = new Command(async () => await ConnectToBT(), () => CanConnect); // there is no command for OnOff as WindowsPhone does not support it for toggleSwitch // instead IsOn property is used. // if you wish to use plug-in and a command to launch web browser, this may be helpful // http://stackoverflow.com/questions/16616774/mvvmcross-how-to-navigate-to-something-besides-a-viewmodel }
void Discconect() { _Manager.Disconnect(); }