New Google Play services stuff like low power location

bluedude

Well-Known Member
Licensed User
Longtime User
Still need to add permissions

Hi,

Now compiling with 2.7 but it does not add the correct permission. I also think it should work without GPS according to the documentation.

They state that the underlying technology isn't important, that is the reason they created this.

So for me no luck unfortunately, it gets connected but it does not kick in the listener and location.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Also - i don't think it matters whether you compile with the debug key or whether you compile with a private sign key.
But if you can try compiling again and instead of using the debug key, use a private key store - see if that changes anything.

I tried both debug key and private key and it works the same with both so i suspect this is unimportant but posted anyway.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Retrieving the Current Location | Android Developers

Apps that use Location Services must request location permissions. Android has two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION. The permission you choose controls the accuracy of the current location. If you request only coarse location permission, Location Services obfuscates the returned location to an accuracy that's roughly equivalent to a city block.

Requesting ACCESS_FINE_LOCATION implies a request for ACCESS_COARSE_LOCATION.

So at least one permission is required.

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

I never use debug keys, always privately signed. And I also added permission and tried GPS on or off settings. Also tried to change priority but no location gets retrieved unfortunately, even not when on PRIORITY_HIGH_ACCURACY

Cheers,
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I can only think it's a problem with the Google Play Services related stuff.

Here's the google-play-services.jar library i am using - downloaded using the SDK Manager just a couple of days ago.

The IsGooglePlayServicesAvailable method does this:

B4X:
   /**
    * Verifies that Google Play services is installed and enabled on this device,
    * and that the version installed on this device is no older than the one required by this client.
    */
   public static int IsGooglePlayServicesAvailable(BA pBA){
      return com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(pBA.context);
   }

I can't see that method returning GooglePlayServicesHelper.SUCCESS if the required service is not available.

If you visit the Play Store on your device is there an available update for the Google Play Services installed on the device?

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

I downloaded the latest yesterday and I have the new play store. My play jar is the same as yours and even newer.

In the logs it also tells me the play services stuff is correct.

Cheers,
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
@bluedude

Have you got the example working yet?
Can you modify the example so that it calls the LocationClient GetLastLocation method after the Connected event is raised and see if that reports a location?

B4X:
Sub Process_Globals

End Sub

Sub Globals
   Dim LocationClient1 As LocationClient
   Dim LocationListener1 As LocationListener
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim GooglePlayServicesHelper1 As GooglePlayServicesHelper
   Dim ServiceStatus As Int=GooglePlayServicesHelper1.IsGooglePlayServicesAvailable
   
   Select ServiceStatus
      Case GooglePlayServicesHelper1.SUCCESS
         Log("GooglePlayServicesHelper IsGooglePlayServicesAvailable service available")
         LocationClient1.Initialize("LocationClient1")
         LocationClient1.Connect
      Case Else
         Log("GooglePlayServicesHelper IsGooglePlayServicesAvailable returned: "&ServiceStatus)
   End Select
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
   If LocationListener1.IsInitialized Then
      LocationClient1.RemoveLocationUpdates(LocationListener1)
   End If
   
   If LocationClient1.IsInitialized AND (LocationClient1.IsConnected OR LocationClient1.IsConnecting) Then 
      LocationClient1.Disconnect
   End If
End Sub

Sub LocationClient1_Connected
   Log("LocationClient1_Connected")
   Dim LocationRequest1 As LocationRequest
   LocationRequest1.Initialize
   LocationRequest1.SetPriority(LocationRequest1.PRIORITY_BALANCED_POWER_ACCURACY)
   
   If LocationListener1.IsInitialized=False Then
      LocationListener1.Initialize("LocationListener1")
   End If
   
   LocationClient1.RequestLocationUpdates(LocationRequest1, LocationListener1)
   
   Dim LastLocation As Location=LocationClient1.GetLastLocation
   If LastLocation.IsInitialized Then
      Log(LastLocation.Latitude&", "&LastLocation.Longitude)
   Else
      Log("LastLocation is not initialized")
   End If
End Sub

Sub LocationClient1_ConnectionFailed(ErrorCode As Int)
   Log("LocationClient1_ConnectionFailed ErrorCode="&ErrorCode)
End Sub

Sub LocationClient1_Disconnected
   Log("LocationClient1_Disconnected")
End Sub

'   ** the GPS library must be enabled to work with Location objects **
Sub LocationListener1_LocationChanged(Location1 As Location)
   Log("LocationListener1_LocationChanged")
   Log(Location1.Latitude&", "&Location1.Longitude)
End Sub

Change your device orientation after the Connected event is raised - does GetLastLocation now return an initialized Location?

Martin.
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Another little progress report here, i've managed to get some of the Geofence functionality working...
But it seems as though the location service is only using the wifi network location and not hardware GPS.
I've attached a project and the updated library.

On my Galaxy Tab2 Jelly Bean it correctly tells me when i am within a Geofence(when i am at home conencted to my home wifi), i took a walk and it failed to tell me when i left the Geofence, but on returning it again told me i was within a Geofence.

Sounds like it's not using hardware GPS to me.

Anyway, it's a lovely bright day here in the UK and i'm going out, so i'll work more on the library over the next few days.

Martin.
 
Last edited:
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
LastLocation works

Hi,

It seems lastlocation works, need to drive around little more to see how locationlistener performs.

Will start testing Geofencing.

Cheers,
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Listener locationchange does not work

Hi,

Tested a few things and LastLocation works. Listener also gets initialized but does not generate the change event when driving around.

It seems for High accuracy you need to have GPS on and it does not seem to use an alternative when GPS is off.

So I guess the Google API isn't that smart yet :)
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The library is taking shape, after a bit of research i have a new example project that better shows usage of the LocationRequest.

I've moved all of the location api code into a foreground service and used the AHPreferenceActivity to enable you to choose the parameters used for the LocationRequest.

It seems as though the problem previously reported by bluedude that the LocationListener LocationChanged event was not being raised was because some default properties of the LocationRequest were being used.
I started by setting the ExpirationDuration property to a large number of milliseconds and now the event was being raised as expected.
Without setting this property or incorrectly setting the Geofence NEVER_EXPIRE constant (as i posted previously i think) the LocationRequest was expiring before any LocationChanged events were raised.

Anyway - the 'find my location' features are now looking good.
I'm gonna take a drive shortly and see how the example code performs.

I had a search too for info on the Geofence, and found some disappointing reports where just like my previous Geofence code the device was not using hardware GPS to detect if it entered or exited a Geofence.
Instead it seemed to only use wifi (maybe 3g too) to establish if the device has entered or exited a Geofence:

gps - New Android Geofence Api - Sample code does not alert/notify when at location - Stack Overflow

how to use android geofencing api? - Stack Overflow

maps - Bug in Android Geofence Sample code? Can anyone please confirm? - Stack Overflow

These reports are saying that the official google demo code for Geofencing simply does not work (as expected), it fails to use the hardware GPS and instead uses wifi location resulting in a failure to detect entering or exiting a Geofence.

My next update is to detect entering or exiting Geofences while also listening for a LocationRequest with PRIORITY_HIGH_ACCURACY set to try and force the use of hardware GPS for Geofencing.
I hope to work on that later and will post again with my results.

[edit]See next post for updated code and library files[/edit]

Martin.
 
Last edited:
Upvote 0

warwound

Expert
Licensed User
Longtime User
Good news...

I have a new demo project and it is successfully detecting me entering and exiting a Geofence. In fact i set a 20 metre geofence around my home and strolled up and down the road a few times, my S3 detected me entering and leaving the geofence 3 or 4 times.
I have all GPS stuff enabled on my S3 - hardware GPS and network location etc.

I've removed the code and library from my last post and attached all new files to this post.
And i'll leave everyone that's following this thread to take a look and post their results, comments and any requests.

The geofencing aspect of the new location apis seems pretty vague and not thoroughly documented, here's another article i found while debugging my previous problem with the geofencing: how to use android geofencing api? | BlogoSfera.
The main confusion is what location provider(s) does geofencing actually use?
Does it use only wifi? wifi and 3G?
Does it ever use hardware GPS?
So far i have found these questiones asked again and again with not a single real (authorative) answer.

So give the latest project a try, use the Settings option to enable/disable LocationRequest and/or Geofence and set LocationRequest parameters and enjoy an evening stroll!
(You'll have to hardcode a Geofence location and radius in the LocationManager service at line ~107).
Be sure to connect your device to your computer and take a look at the log when you return, recent logs should still display and you should see everything happening.

If you all report that it is generally working as expected i'll move on to activity detection next and we should see the library complete and ready for use by next week.
If you make any reports please include details of the device being used and which location providers you have enabled in your device settings.

Martin.
 

Attachments

  • LocationAPI_demo_20130606a.zip
    35.7 KB · Views: 426
Upvote 0

warwound

Expert
Licensed User
Longtime User
Update...

So i started the demo app as posted in my previous post, enabled a geofence (around my home 10m radius), but disabled LocationRequest.
The app tells me i've entered the geofence - i was in the geofence when i started the app so this is logical.

I took a drive to the out of town supermarket, parked my car and took a look at the app.
It said i was still within the geofence :(.
Did my shopping, restarted the app and drove home.
The app did nothing, it didn't tell me when i started my drive home that i wasn't in the geofence and when i got home it didn't detect that i had entered the geofence :(.

4 members have downloaded the example project so far - have any of you had any luck (good or bad)?

Martin.
 
Upvote 0
Top