private async Task <bool> ConnectDeviceAsync(DeviceListItemViewModel device, bool showPrompt = true) { if (showPrompt && !await _userDialogs.ConfirmAsync($"Connect to device '{device.Name}'?")) { return(false); } try { CancellationTokenSource tokenSource = new CancellationTokenSource(); ConnectParameters connectParameters = new ConnectParameters(); await Adapter.ConnectToDeviceAsync(device.Device, connectParameters, tokenSource.Token); _userDialogs.ShowSuccess($"Initializing Reader, Please Wait.", 8000); PreviousGuid = device.Device.Id; return(true); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Connection error"); Mvx.Trace(ex.Message); return(false); } finally { //_userDialogs.HideLoading(); device.Update(); } }
private async void ConnectAndDisposeDevice(DeviceListItemViewModel item) { try { using (item.Device) { //_userDialogs.ShowLoading($"Connecting to {item.Name} ..."); await Adapter.ConnectToDeviceAsync(item.Device); item.Update(); //_userDialogs.ShowSuccess($"Connected {item.Device.Name}"); //_userDialogs.HideLoading(); //for (var i = 5; i >= 1; i--) //{ // _userDialogs.ShowLoading($"Disconnect in {i}s..."); // await Task.Delay(1000); // _userDialogs.HideLoading(); //} } } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Failed to connect and dispose."); } finally { _userDialogs.HideLoading(); } }
private async void DisconnectDevice(DeviceListItemViewModel device) { if (BleMvxApplication._reader.Status != CSLibrary.HighLevelInterface.READERSTATE.DISCONNECT) { BleMvxApplication._reader.DisconnectAsync(); } try { if (!device.IsConnected) { return; } _userDialogs.ShowLoading($"Disconnecting {device.Name}..."); await Adapter.DisconnectDeviceAsync(device.Device); } catch (Exception ex) { _userDialogs.Alert(ex.Message, "Disconnect error"); } finally { device.Update(); _userDialogs.HideLoading(); } }
async void ConnectAndDisposeDevice(object o, EventArgs e) { var mi = ((MenuItem)o); DeviceListItemViewModel item = mi.CommandParameter as DeviceListItemViewModel; try { //await Adapter.ConnectToDeviceAsync(Devices.FirstOrDefault().Device); BLEProfileManager.bleprofile.Adapter.ConnectToDeviceAsync(item.Device); item.Update(); for (var i = 2; i >= 1; i--) { await Task.Delay(1000); } } catch (Exception ex) { } finally { // this.Devicesl.ItemsSource = this.Devices; } this.Devicesl.ItemsSource = null; DisplayAlert("Your connected to ", item.Device.Name, "Ok"); //BLEProfileManager.ScanForDevices(); App.BlegUID = item.Device.Id.ToString(); ((App)App.Current).SaveProfile(); this.Systconnocted.Text = "Device name: " + BLEProfileManager.bleprofile.Devices.FirstOrDefault().Device.Name; this.SyststateL.Text = "Connected: " + BLEProfileManager.bleprofile.Devices.FirstOrDefault().IsConnected.ToString(); this.Devicesl.ItemsSource = BLEProfileManager.bleprofile.Devices; }
async void connectClicked(object sender, EventArgs e) { bool answer; if (switchconnectbutton == 0) { answer = await DisplayAlert("Beskyddare", "Vill du ansluta till den här bluetooth enheten?", "Ja", "Nej"); } else { answer = await DisplayAlert("Beskyddare", "Vill du avsluta till den här bluetooth enheten?", "Ja", "Nej"); } if (answer == true) { //var mi = ((MenuItem)sender); // DeviceListItemViewModel item = mi.CommandParameter as DeviceListItemViewModel; var button = sender as Button; DeviceListItemViewModel item = button.BindingContext as DeviceListItemViewModel; try { if (switchconnectbutton == 0) { switchconnectbutton = 1; BLEProfileManager.bleprofile.Adapter.ConnectToDeviceAsync(item.Device); item.Update(); DisplayAlert("Your connected to ", item.Device.Name, "Ok"); for (var i = 2; i >= 1; i--) { await Task.Delay(1000); } //App.button.Text = "Avsluta"; App.isConnectedBLE = true; App.BlegUID = item.Device.Id.ToString(); ((App)App.Current).SaveProfile(); BLEProfileManager.getUnknownServiceAsync(); } else { //App.button.Text = "Anslut"; switchconnectbutton = 0; App.isConnectedBLE = false; //need to be implement BLEProfileManager.bleprofile.Adapter.DisconnectDeviceAsync(item.Device); DisplayAlert("Your disconnected from ", item.Device.Name, "Ok"); for (var i = 2; i >= 1; i--) { await Task.Delay(900); } } } catch (Exception ex) { DisplayAlert("Wrong ", "Something bad happeed", "Ok"); } finally { //BLEProfileManager.bleprofile.Devices.FirstOrDefault().Update(); BLEProfileManager.bleprofile.Devices.FirstOrDefault(d => d.Device.Id == item.Id).Update(); this.theBTunits.ItemsSource = this.BLEProfileManager.bleprofile.Devices; } } }