private async void connectButton_Click(object sender, RoutedEventArgs e) { connectButton.Content = "Connecting..."; await connectionManager.Connect().ContinueWith(i => { Dispatcher.BeginInvoke(() => { connectButton.Content = "Connect"; }); }); string threatString = "This area is potential to threats."; await connectionManager.SendCommand(threatString); }
/// <summary> /// Method to get current coordinate asynchronously so that the UI thread is not blocked. Updates MyCoordinate. /// Using Location API requires ID_CAP_LOCATION capability to be included in the Application manifest file. /// </summary> private async void GetCurrentCoordinate() { ShowProgressIndicator(AppResources.GettingLocationProgressText); if (App.Geolocator == null) { // Use the app's global Geolocator variable App.Geolocator = new Geolocator(); App.Geolocator.DesiredAccuracy = PositionAccuracy.High; } try { Geoposition currentPosition = await App.Geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10)); _accuracy = currentPosition.Coordinate.Accuracy; string latitude = currentPosition.Coordinate.Latitude.ToString("0.000000"); string longitude = currentPosition.Coordinate.Longitude.ToString("0.000000"); string accuracy = currentPosition.Coordinate.Accuracy.ToString(); string speed = currentPosition.Coordinate.Speed.ToString() == "NaN" ? "0" : (currentPosition.Coordinate.Speed).ToString(); string direction = currentPosition.Coordinate.Heading.ToString() == "NaN" ? "0" : currentPosition.Coordinate.Heading.ToString(); string locationMethod = currentPosition.Coordinate.PositionSource.ToString(); Dispatcher.BeginInvoke(async() => { MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude); DrawMapMarkers(); MyMap.SetView(MyCoordinate, 12, MapAnimationKind.Parabolic); sessionID = Guid.NewGuid().ToString(); Location location = new Location(); location.Latitude = Double.Parse(latitude); location.Longitude = Double.Parse(longitude); location.Accuracy = int.Parse(accuracy); location.Speed = float.Parse(speed); location.Direction = int.Parse(direction); location.LocationMethod = locationMethod; location.GpsTime = DateTimeOffset.Now.UtcDateTime; if (ConnectionHelper.isConnected) { ICollection <Threat> threats = await ConnectionHelper.locationHubProxy.Invoke <ICollection <Threat> >("SendLocation", location, userInfo.PhoneNumber); if (threats.Count != 0) { string threatString = "This area is potential to threats. This area is prone to the following threats.\n"; foreach (Threat threat in threats) { threatString += threat.Type + " "; } Dispatcher.BeginInvoke(() => { MessageBox.Show(threatString, "Alert!", MessageBoxButton.OK); }); SpeechSynthesizer speech = new SpeechSynthesizer(); await speech.SpeakTextAsync(threatString); BluetoothConnectionManager connectionManager = new BluetoothConnectionManager(); await connectionManager.Connect(); await connectionManager.SendCommand(threatString); } } }); } catch (Exception) { // Couldn't get current location - location might be disabled in settings MessageBox.Show(AppResources.LocationDisabledMessageBoxText, AppResources.ApplicationTitle, MessageBoxButton.OK); } HideProgressIndicator(); }