New Google Play services stuff like low power location

Discussion in 'Android Questions' started by bluedude, May 28, 2013.

  1. bluedude

    bluedude Well-Known Member Licensed User

    Hi,

    Is anyone working on a new library for the stuff that Google released?

    Location APIs | Android Developers

    - Fused location;
    - Geofencing;
    - Activity recognition

    Cheers,
     
    gudino jose luis likes this.
  2. warwound

    warwound Expert Licensed 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
     
  3. bluedude

    bluedude Well-Known Member Licensed 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,
     
  4. warwound

    warwound Expert Licensed 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.
     
  5. dealsmonkey

    dealsmonkey Active Member Licensed User

    Thanks Warwound, that would be very nice :sign0142:
     
  6. warwound

    warwound Expert Licensed 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:

    Code:
    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:

    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: Jun 6, 2013
    Peter Simpson likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    I've moved this thread. When your library is ready please create a new thread in the Additional Libraries forum.
     
  8. bluedude

    bluedude Well-Known Member Licensed 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?
     
  9. warwound

    warwound Expert Licensed User

    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.
     
  10. bluedude

    bluedude Well-Known Member Licensed 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!
     
  11. bluedude

    bluedude Well-Known Member Licensed 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.
     
  12. warwound

    warwound Expert Licensed 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.

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

    Martin.
     
  13. bluedude

    bluedude Well-Known Member Licensed 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.
     

    Attached Files:

  14. warwound

    warwound Expert Licensed 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:

    Code:
    <?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.
     
  15. bluedude

    bluedude Well-Known Member Licensed User

    Galaxy Nexus

    Hi,

    Galaxy Nexus and I guess the latest Android (4.2.2)

    Nope, after compilation the permission is not there.

    Cheers,
     
  16. warwound

    warwound Expert Licensed User

    Can you use the manifest editor and manually add the permission:

    Code:
    'End of default text.
    AddPermission(android.permission.ACCESS_FINE_LOCATION)
    Does the example now work?

    Martin.
     
  17. bluedude

    bluedude Well-Known Member Licensed User

    Sure, did that before. So the Connection event happens but the location listener does not return anything.

    Cheers,
     
  18. brelto85

    brelto85 Active Member Licensed User

    same behavior..
     
  19. Harris

    Harris Well-Known Member Licensed 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.
     
  20. warwound

    warwound Expert Licensed User

    @Harris

    The documentation for the new Location APIs simplay states:

    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:

    Code:
    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.75615560.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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice