B4A Library GPSGNSS Library

After discovering several problems in the operation of GPS and GNSS libraries on SDK32 and SDK33, I decided to make one library that will be compatible with almost all versions of android (from SDK less than 24 to SDK33 and above). This library combines deprecated GPS classes with still up-to-date GNSS classes and, based on the android version, decides which one to use. Basically, if used on SDK less than 24, library will use GPS set of classes, otherwise GNSS set of classes. This allows developers to use one library to create an application which will work on almost all android SDK versions.

The working principle of this library is almost exactly the same as that of the native GPS library. The library contains the same methods and the same events. Of course, there are a few additional methods, properties and events, but the basic ones have remained unchanged.

The only major change is that instead of the "Location" class that is part of the GPS library, this library uses the "ALocation" class that is in a separate library called "ALocation" which is packaged together with the GPSGNSS library. The "ALocation" library must be checked together with the GPSGNSS library (picture below). This was done to avoid accidental class conflicts with the GPS or GNSS libraries. So your old project that uses the GPS library needs to be reworked so that wherever class "Location" is used, just put an "A" in front of "Location" = "ALocation". It shouldn't be that hard ;) .

LibScreen.png


Also, the "GPSEnabled" property has been replaced with "IsLocationEnabledInSettings" relative to the GPS library. Everything else is the same with a few added methods, events and properties.

An addition in this library compared to the old ones is a class called NETPOS (NETwork POSitioning). This class uses the network provider to determine the location (can be used when the GPS or GNSS signal is weak or unavailable). Both classes can be used in parallel so that locating is always available. The NETPOS class uses the same methods and properties as the GPSGNSS class, with the exception of events such as GpsStatus and NMEA, which are unavailable when using a network provider.


The METHODS contained in the GPSGNSS and NETPOS classes are:
  • GetCurrentLocation (new method) - This method asynchronously determines the current location. At the end of the procedure, the "CurrentLocationResult (Location1 As ALocation)" event is activated.
  • Initialize (the same as in GPS library).
  • IsInitialized (the same as in GPS library).
  • Start (the same as in GPS library).
  • Stop (the same as in GPS library).

The PROPERTIES contained in the GPSGNSS and NETPOS classes are:
  • IsLocationEnabledInSettings (Equivalent to the GPSEnabled property in the GPS library).
  • IsStarted (new property) - returns true if GPSGNSS or NETPOS is started and listening for events, otherwise false.
  • LastKnownLocation (new property) - returns the last remembered location without calculating the current location.
  • LocationSettingsIntent (the same as in GPS library).

The EVENTS contained in the GPSGNSS and NETPOS classes are:
  • LocationChanged (Location1 As ALocation) - (the same as in GPS library).
  • LocationChanged2 (Locations() As ALocation) 'Works on SDK31 and up - (new event).
  • UserEnabled (Enabled As Boolean) - (the same as in GPS library).
  • GpsStatus (Satellites As List) - (the same as in GPS library) available only in GPSGNSS class.
  • NMEA (TimeStamp As Long, Sentence As String) - (the same as in GPS library) available only in GPSGNSS class.
  • Started - (new event) will bi fired when GPSGNSS or NETPOS is started and starts to listening for events (Start method called).
  • Stopped - (new event) will bi fired when GPSGNSS or NETPOS stops to listening for events (Stop method called).
  • CurrentLocationResult (Location1 As ALocation) - (new event) will bi fired after calling GetCurrentLocation method.

GPSGNSS example code:
Sub Class_Globals
    Private Root As B4XView
    Private GPS As GPSGNSS
    Private rp As RuntimePermissions
    Private lblNumOfSattelites As Label
    Private lblAverageSignal As Label
    Private lblLatitude As Label
    Private lblLongitude As Label
End Sub

Public Sub Initialize
    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
   
    'Requesting Location's runtime permissions
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_COARSE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result=False Then
        ExitApplication
    End If
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result=False Then
        ExitApplication
    End If
   
    If GPS.IsInitialized=False Then
        GPS.Initialize("GPS")
    End If
End Sub

Private Sub B4XPage_Disappear
    If GPS.IsInitialized Then
        If GPS.IsStarted Then
            GPS.Stop
        End If
    End If
    ExitApplication
End Sub

Private Sub btnStartGPSGNSS_Click
   
    If GPS.IsStarted=False Then
        GPS.Start(0,0)
    End If
End Sub

Private Sub btnStopGPSGNSS_Click
    If GPS.IsStarted=True Then
        GPS.Stop
    End If
End Sub

Private Sub Msgbox_Result (Result As Int)
   
End Sub

Private Sub btnGetLastKnownLocation_Click
    Dim loc As ALocation=GPS.LastKnownLocation
    If loc.IsInitialized Then
        MsgboxAsync("Your last known location is:" & CRLF & "Latitude=" & loc.Latitude & CRLF & "Longitude=" & loc.Longitude,"Last Known Location")
    End If
End Sub

Private Sub btnCurrentLocation_Click
    ProgressDialogShow("Collecting information...")
    Sleep(20)
    GPS.GetCurrentLocation
    Wait For GPS_CurrentLocationResult (Location1 As ALocation)
    ProgressDialogHide
    If Location1.IsInitialized Then
        MsgboxAsync("Your current location is:" & CRLF & "Latitude=" & Location1.Latitude & CRLF & "Longitude=" & Location1.Longitude,"Current Location")
    End If
End Sub

Private Sub GPS_GpsStatus (Satellites As List)
    If GPS.IsStarted Then
        Dim NumOfSat As Int=0
        Dim Signal As Float=0
        For Each sat As GPSSatellite In Satellites
            If sat.UsedInFix Then
'            Log("--------SATTELITE " & Satellites.IndexOf(sat) & "-----------------------")
'            Log("Azimuth=" & sat.Azimuth)
'            Log("Elevation=" & sat.Elevation)
'            Log("Prn=" & sat.Prn)
'            Log("Snr=" & sat.Snr)
'            Log("HasAlmanac=" & sat.HasAlmanac)
'            Log("HasEphemeris=" & sat.HasEphemeris)
'            Log("UsedInFix=" & sat.UsedInFix)
'            Log("-------------------------------------------------------------------------")
                NumOfSat=NumOfSat+1
                Signal=Signal+sat.Snr
            End If
        Next
        lblNumOfSattelites.Text="Satelites in use: " & NumOfSat
        lblAverageSignal.Text="Average signal: " & Round2(Signal/NumOfSat,2)
    End If
End Sub

Private Sub GPS_LocationChanged (Location1 As ALocation)
    If GPS.IsStarted Then
        lblLatitude.Text="Latitude: " & Round2(Location1.Latitude,8)
        lblLongitude.Text="Longitude: " & Round2(Location1.Longitude,8)
    End If  
End Sub

Private Sub GPS_UserEnabled (Enabled As Boolean)
    Log("LocationEnabled = " & Enabled)
'    If Enabled=False Then
'        StartActivity(GPS.LocationSettingsIntent)
'    End If
End Sub

Private Sub GPS_NMEA (TimeStamp As Long, Sentence As String)
    'Log("NMEA: Sentence=" & Sentence & "    TimeStamp=" & TimeStamp)
End Sub

Private Sub GPS_LocationChanged2 (Locations() As ALocation) 'Works on SDK31 and up
    'Log(Locations.Length)
End Sub

Private Sub GPS_Started
    Log("GPS_Started    GPS.IsStarted=" & GPS.IsStarted)
    lblNumOfSattelites.Text="Satelites in use: calculting"
    lblAverageSignal.Text="Average signal: calculting"
    lblLatitude.Text="Latitude: calculting"
    lblLongitude.Text="Longitude: calculting"
End Sub

Private Sub GPS_Stopped
    Log("GPS_Stopped    GPS.IsStarted=" & GPS.IsStarted)
    lblNumOfSattelites.Text="Satelites in use: unknown"
    lblAverageSignal.Text="Average signal: unknown"
    lblLatitude.Text="Latitude: unknown"
    lblLongitude.Text="Longitude: unknown"
End Sub

This library was tested on devices with SDK23, SDK27, SDK28, SDK29, SDK31 and SDK33.

If this library makes your work easier and saves time in creating your application, please make a donation.
 

Attachments

  • GPSGNSS_Lib.zip
    23.7 KB · Views: 110
  • GPSGNSSExample.zip
    11.2 KB · Views: 106

Theera

Well-Known Member
Licensed User
Longtime User
After I've tested,I have the error. How do I do continue,please.

B4A Version: 12.50
Parsing code. (0.07s)
Java Version: 8
Building folders structure. (0.05s)
Running custom action. Error
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Friday, November 3, 2023 1:06:10 PM
Source : C:\Users\salea\DOWNLO~1\GPSGNS~2\Shared Files\
Dest : C:\Users\salea\DOWNLO~1\GPSGNS~2\B4A\Files\
Files : *.*

Options : *.* /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
2023/11/03 13:06:10 ERROR 2 (0x00000002) Accessing Source Directory C:\Users\salea\DOWNLO~1\GPSGNS~2\Shared Files\
The system cannot find the file specified.
 

Ivica Golubovic

Active Member
Licensed User
After I've tested,I have the error. How do I do continue,please
The problem is in the example project. I did the ExportProject but I didn't check if it works, I just uploaded it. Try to create a new project, copy the layout to the new project and copy the program code. I'll check out the sample project when I have time.
 

ciginfo

Well-Known Member
Licensed User
Longtime User
I cannot import the libraries ALocation.jar, ALocation.xml, GPSGNSS.jar, GPSGNSS.xml. They appear in my "libraries" folder in Explorer but not in the libraries manager when I use B4A. However, the path of this repertoire is the right one. This is the first time this has happened to me when I add a library.
An idea ?
THANKS
librarie1.jpg
librarie2.jpg
librarie3.jpg
 
Top