New Google Play services stuff like low power location

warwound

Expert
Licensed User
Longtime User
That looks interesting - I'd like to try wrapping it myself when I have time.

Martin.

Sent from my GT-I9300 using Tapatalk 2
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
I think it is interesting because it solves all the battery issues it seems.

One company already implemented it and you can actually notice when someone parks his car :)

Cheers,
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I've created a new Eclipse project and am starting to wrap some of the classes.
Should have something working within a few days and i'll post again then.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Here's a little progress report to keep you all (both of you!) interested...

If you take a look here you'll see some reference for the parts of the Location APIs that i have so far wrapped.

A simple program to retrieve the current location:

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   Dim LocationClient1 As LocationClient
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)

End Sub

Sub LocationClient1_Connected
   Log("LocationClient1_Connected")
   Dim LocationRequest1 As LocationRequest
   LocationRequest1.Initialize
   LocationRequest1.SetPriority(LocationRequest1.PRIORITY_BALANCED_POWER_ACCURACY)
   
   Dim LocationListener1 As LocationListener
   LocationListener1.Initialize("LocationListener1")
   
   LocationClient1.RequestLocationUpdates(LocationRequest1, LocationListener1)
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

And the log shows:

LogCat connected to: 192.168.1.91:5555
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
GooglePlayServicesHelper IsGooglePlayServicesAvailable service available
** Activity (main) Resume **
LocationClient1_Connected
LocationListener1_LocationChanged
52.7561723, 0.3975852

It works and is fast to get that first location fix.

I'll be busy tomorrow so it's likely to be nearer the weekend before i have time to work more on the library.

The demo project and library files are attached if anyone wants to give them a try - you won't be able to do much more than get the current location but you can experiment with the different LocationRequest methods such as SetInterval, SetSmallestDisplacement etc.

You will need a copy of the latest version of google-play-services.jar in your b4a additional libraries folder - that .jar file is 578KBs in size so i couldn't include it in the attached files.
Setup Google Play Services SDK | Android Developers.


Martin.
 
Last edited:
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
What do you mean by GPS library enabled? Do you mean the GPS setting in Android?

Does it use a fallback method like CellID when GPS is not on?
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
What do you mean by GPS library enabled? Do you mean the GPS setting in Android?

Does it use a fallback method like CellID when GPS is not on?

I just mean that to use the Location object in b4a you much check the GPS library otherwise the b4a compiler will not recognise the Location object.
As the LocationListener event LocationChanged passes a Location object to b4a you will need the GPS library to work with the Location object.

It looks as though you use the LocationRequest SetPriority method to select a 'preferred' method to obtain the location: LocationRequest | Android Developers.
There are 3 possible priorities to choose from.
(I don't think you can explicitly choose to get the location from say hardware GPS or network location, it's implied in the priority setting).

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

Going to test a few things, now updating my SDK. Pretty impressive stuff if we get it working.

I know a company that has a find my car app. that automatically detects parking with the API's so you don't need to set your last parked location by hand. Pretty smart stuff!
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Hi,

The library does not seem to get my location. Which permissions did you add manually? On the first run it complained about permissions.

Cheers.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The LocationClient has the android.permission.ACCESS_FINE_LOCATION hardcoded into it.
When you use the LocationClient your b4a project will have that permission automatically added to it's manifest.

The demo project i posted has no additional permissions set.
On my Galaxy Tab2 (android 4.1.1) it runs as expected with no permission problems.

On the first run it complained about permissions.

Are these compile time or run time errors - can you post the errors?

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Running your sample project

Hi,

I just run your sample project out of the box and it gives me the runtime error, attached a screenshot.

GPS and your lib are enabled.
 

Attachments

  • error.png
    error.png
    39.2 KB · Views: 677
Upvote 0

warwound

Expert
Licensed User
Longtime User
What device and android version are you having this permissions problem with?

Compiling the example project in release mode and opening the manifest i see:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="uk.co.martinpearman.b4a.locationapidemo"
   android:versionCode="1"
   android:versionName=""
   android:installLocation="internalOnly">
   <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
   <supports-screens android:largeScreens="true" 
       android:normalScreens="true" 
       android:smallScreens="true" 
       android:anyDensity="true"/>
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
   <application
      android:icon="@drawable/icon"
      android:label="LocationAPI demo">
      <activity
         android:windowSoftInputMode="stateHidden"
         android:launchMode="singleTop"
         android:name=".main"
         android:label="LocationAPI demo"
         android:screenOrientation="unspecified">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Does you manifest contain the android.permission.ACCESS_FINE_LOCATION entry after compiling the project?

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Galaxy Nexus

Hi,

Galaxy Nexus and I guess the latest Android (4.2.2)

Nope, after compilation the permission is not there.

Cheers,
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Can you use the manifest editor and manually add the permission:

B4X:
'End of default text.
AddPermission(android.permission.ACCESS_FINE_LOCATION)

Does the example now work?

Martin.
 
Upvote 0

bluedude

Well-Known Member
Licensed User
Longtime User
Sure, did that before. So the Connection event happens but the location listener does not return anything.

Cheers,
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
The biggest problem with using the (extremely valuable) GPS is rapid battery drain - when not hard wired to power supply.

Am I reading this correctly that this new API will significantly reduce draw with GPS sensor enabled (possibly using other lower powered devices to detect movement activity)?

Due to the power draw issue, I set prefs based on user settings - GPS Always ON (connected to power source) or Ask to enable / disable (each time GPS is needed - user is prompted to turn on/off).

It would be sooooo nice to leave GPS Enabled and have hardware turn ON / OFF as needed by motion detection > than x meters (or x elapsed seconds)... But how is this accomplished without GPS?

In my case, it is kilometers before I require another fix from last position.

Looking forward to your efforts - Martin and all else.

Thanks so much again.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
@Harris

The documentation for the new Location APIs simplay states:

The location APIs make it easy for you to build location-aware applications, without needing to focus on the details of the underlying location technology. They also let you minimize power consumption by using all of the capabilities of the device hardware.

I think we'll only know if the claimed power savings are real or hype once i get the library working and a number of us have given it a thorough test.

@bluedude and brelto85

I've just run the example code on my old ZTE Blade - it's running CyanogenMod Gingerbread 2.3.7.
The example works perfectly, quickly finding my location:

B4X:
LogCat connected to: CM7-Blade
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
GooglePlayServicesHelper IsGooglePlayServicesAvailable service available
** Activity (main) Resume **
LocationClient1_Connected
LocationListener1_LocationChanged
52.7561556, 0.3975667

Then i tried my Samsung Galaxy S3 running Jelly Bean 4.1.2, again it works perfectly.

B4A (v2.70) is compiling the APK with the required permission, i'm not manually editing the manifest to add the permission, and the log shows everything just as expected.
The example does not check whether the device's available location services are enabled but i doubt you are both running the example with GPS disabled!

So i'm not sure what to suggest - i'll be working more on the library over the next couple of days so if i have any ideas i'll post again.

Martin.
 
Upvote 0
Top