I have been wasting a lot of time trying to make the permission for location work correctly, apparently I need to restart my app after I give the permissions, I don’t know how to make it ‘clean’ or transparent for the user, after I give the permissions I got this errors:
[SplashPage].location perms enabled
PositionSource: unknown source error
[MainItem].[PositionSource].onActiveChanged:true
PositionSource: closed error
what Im doing wrong?
on SplashPage.qml
Component.onCompleted: {
//this function check if there are any location permission using Android extras libraries
// if there aren't the app request permissions and wait until the user choose, this triggers: onPermResult
// if there are permits, trigger onPermResult, no problems here.
androidPerms.requestFineLocationPermission()
}
Perm.Permissions{
id: androidPerms
onPermResult: {
if(permRes){
console.debug("[SplashPage].location perms enabled")
// the problem is the pos service is not starting as expected, only when I restart the app, after permissions were given.
posSrc.start()
}else{
grantLocationPopUp.open() //this just show a warning to set ask for localization permissions for the app to work
resetButton()
}
}
}
posService:
PositionSource {
id: posSrc
updateInterval: constants.posUpdateInterval
active: false //false as default, active after permissions are granted
signal hasError() //trigger on error has occurred
signal coordFetched() //triggerd after the position has changed
property var coord. //value that stores the coordinates
property bool printPos: true //just a value to print pos only once on the console
onActiveChanged: {
console.debug( "[MainItem].[PositionSource].onActiveChanged:" +posSrc.active )
}
onPositionChanged: {
console.debug( "[MainItem].[onPositionChanged]")
if (!posSrc.active) {
console.debug( "[MainItem].[onPositionChanged].pos not active")
return
}
if(posSrc.printPos){
console.debug("*******[POS CHANGED]********")
console.debug("pos_active:" + posSrc.active)
console.debug("pos_valid:" + posSrc.valid)
console.debug("lon:" + posSrc.position.coordinate.longitude)
console.debug("lat:" + posSrc.position.coordinate.latitude)
console.debug("****************************")
posSrc.printPos = false
}
coord = posSrc.position.coordinate
var lonlat = models.getLonLat( posSrc.position.coordinate.longitude , posSrc.position.coordinate.latitude )
app.settings.setValue( properties.mCOORDINATES , lonlat )
coordFetched()
console.debug("[MainItem].[onPositionChanged].finish")
}
onSourceErrorChanged: {
var error = false;
switch(sourceError){
case PositionSource.AccessError:
console.error("PositionSource: access error")
error = true
break;
case PositionSource.ClosedError:
console.error("PositionSource: closed error")
error = true
break;
case PositionSource.UnknownSourceError:
console.error("PositionSource: unknown source error")
error = true
break;
case PositionSource.SocketError:
console.error("PositionSource: socket error")
error = true
break;
case PositionSource.NoError:
default:
console.debug("pos ok")
break;
}
if( error){
app.settings.setValue( properties.mCOORDINATES, models.getLonLat(0, 0) )
app.settings.setValue( properties.mZONE, models.getEmptyZone() )
hasError()
}
}
}