Android Question FusedLocationProvider get location from wifi problem

uniplan

Active Member
Licensed User
Longtime User
Hi, i want to keep my location (longitude and latitude) from wifi (not gps), i used the example from this link
https://www.b4x.com/android/forum/threads/fusedlocationprovider.50614/

but i don't display nothing, don't retrive the longitude and latitude.

this is my code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: FusedLocationProvider example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    
#End Region
#AdditionalJar: com.android.support:support-v4
#AdditionalJar: com.google.android.gms:play-services-location
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location
End Sub

Sub Globals
    Private LastLocationLabel As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    **    IMPORTANT see manifest for required entries    **
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
    
    Activity.LoadLayout("Main")

    If LastLocation.IsInitialized Then
        UpdateUI
    End If

End Sub

Sub Activity_Resume
    '    attempt to connect to the location services
    '    after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed")
    
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
    
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(1000)    '    1000 milliseconds
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
    LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
    FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
    
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
    
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")
    LastLocation=Location1
    UpdateUI
End Sub

Sub UpdateUI
    LastLocationLabel.Text=DateTime.Time(LastLocation.Time)&" ("&LastLocation.Latitude&", "&LastLocation.Longitude&")"
End Sub


and this is the manifest
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
'End of default text.

'    FusedLocationProvider:

'    required manifest entry required for Google Play Services
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)
    
'    you must add one of these two permissions
'    the permission you add will define the accuracy of location updates
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
 

Peter Simpson

Expert
Licensed User
Longtime User
I believe that you are missing the following line, I don't see it anywhere.
B4X:
     LastLocation.Initialize
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
You're right...i change the success in this mode...but nothing change...
how long should I wait to see the coordinates?

B4X:
Sub FusedLocationProvider1_ConnectionSuccess
    
    
    Try
        Log("FusedLocationProvider1_ConnectionSuccess")
        stato = "ConnectionSuccess"
        LastLocation.Initialize
        Dim LocationRequest1 As LocationRequest
        LocationRequest1.Initialize
        LocationRequest1.SetInterval(1000)    '    1000 milliseconds
        LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
        LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
        FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
        
    Catch
        Log(LastException)
        stato = "ConnectionSuccess_error"
    End Try
End Sub
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
It is a mistake to put this code in the activity. It should be in the starter service instead.

Have you added the location permission?

i moved it into Service_Start...
and this is my permission into manifest

B4X:
'    required manifest entry required for Google Play Services
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)
   
'    you must add one of these two permissions
'    the permission you add will define the accuracy of location updates
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.INTERNET)
AddManifestText(<uses-feature android:name="android.hardware.location.GPS" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.location.NETWORK" android:required="false" />)
AddManifestText(<uses-feature android:name="android.hardware.WIFI" android:required="false"/>)


but now i have this in log...

B4X:
Logger connesso a:  samsung SM-N9005
--------- beginning of main
java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:168)
    at anywheresoftware.b4a.shell.Shell.start(Shell.java:101)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:89)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.afterFirstLayout(main.java:94)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.access$000(main.java:17)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5938)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[])' on a null object reference
    at anywheresoftware.b4a.shell.ShellConnector.sendControlMessage(ShellConnector.java:61)
    at anywheresoftware.b4a.shell.Shell.virtualAssets(Shell.java:128)
    ... 13 more
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (gpsman) Create ***
** Service (gpsman) Start **
FusedLocationProvider1_ConnectionSuccess
** Activity (main) Pause, UserClosed = true **
** Service (gpsman) Destroy **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (gpsman) Create ***
** Service (gpsman) Start **
FusedLocationProvider1_ConnectionSuccess
 
Last edited:
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
where i can find a little working project where i can get location only from wifi or 3g and download it?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use File - Export as zip when building the zip file. You could have then uploaded it directly to the forum.

Works fine here:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (gpsman) Create ***
** Service (gpsman) Start **
FusedLocationProvider1_ConnectionSuccess
FusedLocationProvider1_LocationChanged
(Location) Location[fused 32.837266,35.269671 hAcc=10 et=+16h24m11s956ms alt=480.0 vel=0.0 vAcc=20 sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=52]}]
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (gpsman) Create ***
** Service (gpsman) Start **
FusedLocationProvider1_ConnectionSuccess
FusedLocationProvider1_LocationChanged
(Location) Location[fused 32.837262,35.269680 hAcc=10 et=+16h24m23s563ms alt=479.0 vel=0.0 vAcc=20 sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=52]}]
FusedLocationProvider1_LocationChanged
(Location) Location[fused 32.837253,35.269685 hAcc=10 et=+16h24m25s976ms alt=480.0 vel=0.0 vAcc=20 sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=52]}]
FusedLocationProvider1_LocationChanged
(Location) Location[fused 32.837261,35.269679 hAcc=10 et=+16h24m27s975ms alt=480.0 vel=0.0 vAcc=20 sAcc=??? bAcc=??? {Bundle[mParcelledData.dataSize=52]}]

I've added Log(Location1) to FusedLocationProvider1_LocationChanged
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
this code work for me only if i activate the gps
...my goal is to retrive the location only from wifi or gsm (not gps), i want that the gps is turn off.
I'm waiting but nothing eventlocationChange not raised.

It's possible?
 
Upvote 0
Top