Hi, I try this:
using Tizen.Location;
CheckPrivilegesGPS();
Tizen.Location.Locator locator;
locator = new Locator(LocationType.Gps);
locator.Start();
Tizen.Location.Location location = locator.GetLocation();
if (location != null)
{
latitude.Text = location.Latitude.ToString();
longitude.Text = location.Longitude.ToString();
}
but I gat catch error: service is not available. In my watch settings I have ON location service, I have location privilege in manifext.xml and I check privileges like:
CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/location");
But still get the same error. Do someone know what is issue?
CheckPermission()
only checks if the permission is already granted to your app. You have to also run RequestPermission()
to actually request the permission. You can wrap the whole thing in a simple Task-based async method:
public async Task<bool> CheckPrivilege(string privilege)
{
switch (PrivacyPrivilegeManager.CheckPermission(privilege))
{
case CheckResult.Allow:
return true;
case CheckResult.Deny:
return false;
case CheckResult.Ask:
if (!PrivacyPrivilegeManager.GetResponseContext(privilege).TryGetTarget(out var context))
return false;
var tcs = new TaskCompletionSource<bool>();
context.ResponseFetched += (s, e) =>
{
if (e.cause == CallCause.Answer)
tcs.SetResult(e.result == RequestResult.AllowForever);
else
tcs.SetResult(false);
};
PrivacyPrivilegeManager.RequestPermission(privilege);
return await tcs.Task;
default:
return false;
}
}
and invoke it like this:
bool granted = await CheckPrivilege("http://tizen.org/privilege/location");
Sorry, this is what I have, my all code:
private void CheckPrivilegesGPS()
{
try
{
/// Check location permission
CheckResult result = PrivacyPrivilegeManager.CheckPermission("http://tizen.org/privilege/location");
switch (result)
{
case CheckResult.Allow:
break;
case CheckResult.Deny:
break;
case CheckResult.Ask:
/// Request to privacy popup
PrivacyPrivilegeManager.RequestPermission("http://tizen.org/privilege/location");
break;
}
}
catch (Exception ex)
{
DisplayAlert("Chyba0", ""+ex, "OK");
}
}
public async void GetGPS()
{
try
{
CheckPrivilegesGPS();
Tizen.Location.Locator locator;
locator = new Locator(LocationType.Gps);
locator.Start();
Tizen.Location.Location location = locator.GetLocation();
if (location != null)
{
latitude.Text = location.Latitude.ToString();
longitude.Text = location.Longitude.ToString();
}
else
{
await DisplayAlert("Chyba0", "location is null", "OK");
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
await DisplayAlert("Chyba1", fnsEx.Message, "OK");
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
await DisplayAlert("Chyba2", fneEx.Message, "OK");
}
catch (PermissionException pEx)
{
// Handle permission exception
await DisplayAlert("Chyba3", pEx.Message, "OK");
}
catch (Exception ex)
{
// Unable to get location
await DisplayAlert("Chyba4", ex.Message, "OK");
}
}
And everytime I get the last catch - Unable to get location
Sorry for misunderstanding your question.
I guess the log message literally means that the location data is not available from the GPS sensor (due to e.g. weak signal). You may try one of the following:
- Move your device to another location and try again
- Change the
LocationType
to LocationType.Hybrid
(less accurate)
- Use
await GetLocationAsync()
with a timeout as an argument (null is returned after timeout)
Thank you very much! This work for me, combination hybrid + await GetLocationAsync()
but somehow it wont work with my other functions
public MainPage()
{
InitializeComponent();
//StartAccelerometer(); // with this I get segmenation faul...
//AccelerometerReadData();
GetGPS(); // this work :)
}
public void StartAccelerometer()
{
SensorSpeed speed = SensorSpeed.Default; // Set speed delay for monitoring changes.
Xamarin.Essentials.Accelerometer.Start(speed);
}
private void AccelerometerReadData()
{
Xamarin.Essentials.Accelerometer.ReadingChanged += Accelerometer_ReadingChanged;
}
private void Accelerometer_ReadingChanged(object sender, AccelerometerChangedEventArgs e)
{
var data = e.Reading;
var accX = data.Acceleration.X.ToString();
var accY = data.Acceleration.Y.ToString();
var accZ = data.Acceleration.Z.ToString();
}
OK I dont know what I do, but on second build and test on real device it work