/// <summary> /// Handle the connect button. If connected disconnect. Otherwise connect. /// </summary> /// <param name="src">Source</param> /// <param name="e">EventArgs</param> void OnConnectClicked(object src, EventArgs e) { Debug.WriteLine("IsConnected: " + bluetooth.isConnected()); if (!bluetooth.isConnected()) { bluetooth.endEnumeration(); bluetooth.disconnect(); BtError error = bluetooth.checkBtSupport(); switch (error) { case BtError.Disabled: bluetooth.showEnableBtPrompt(AppResources.EnableBTTitle, AppResources.EnableBTMessage, AppResources.EnableBTConfirm, AppResources.EnableBTCancel); requestTime = Environment.TickCount; break; case BtError.None: // Do not allow this to be shown more than once at a time if (bluetoothDevicePage == null) { discoveredDevices.Clear(); deviceNames.Clear(); bluetoothDevicePage = new BluetoothDevicePage(); Navigation.PushModalAsync(bluetoothDevicePage); } break; default: UserDialogs.Instance.Alert(AppResources.AlertBtErrorMessage, AppResources.AlertBtErrorTitle, AppResources.AlertOk); break; } } else { UserDialogs.Instance.Confirm(new ConfirmConfig() { Title = AppResources.ConfirmDisconnectTitle, Message = AppResources.ConfirmDisconnectMessage.Replace("%DEV%", connectedDeviceName), OkText = AppResources.Yes, CancelText = AppResources.No, OnAction = (result) => { if (result) { bluetooth.endEnumeration(); manualDisconnect = true; bluetooth.disconnect(); } } }); } }
/// <summary> /// Called when starting the app (and loading this page) or when returning to this page from another page /// </summary> protected override void OnAppearing() { // Just returned from bluetooth device page that is no longer visible if (bluetoothDevicePage != null) { bluetoothDevicePage = null; bluetooth.endEnumeration(); if (deviceToConnectTo > -1) { if (deviceToConnectTo >= discoveredDevices.Count) { Debug.WriteLine("An invalid device was selected. Index was " + deviceToConnectTo + " but there were only " + discoveredDevices.Count + " devices."); return; } bluetooth.connect(discoveredDevices[deviceToConnectTo]); connectProgressDialog = UserDialogs.Instance.Progress( new ProgressDialogConfig() { Title = AppResources.Connecting + deviceNames[deviceToConnectTo], IsDeterministic = false, OnCancel = () => { bluetooth.cancelConnect(); bluetooth.disconnect(); }, CancelText = AppResources.Cancel } ); connectProgressDialog.Show(); connectedDeviceName = deviceNames[deviceToConnectTo]; deviceToConnectTo = -1; } else { Debug.WriteLine("Was told to connect to -1"); } } }