Forum Discussion

MadhuraKhare's avatar
MadhuraKhare
New Contributor
5 years ago
Solved

Set Mock location on Android

In geolocation testing on a TC56 device, Android Agent is not getting set as mock location app.

"Select mock location app" setting in developer options is also disabled, I have attached a screenshot for the same.

I am trying to access the script and show a dialog based on it.

Below is code snippet which I used to set mock location.

function MockGPSLocation()
{
var Longt, Lat, Alt, Acc, Gps;

// Enable GPS
Mobile.Device().GPS.GPSEnabled = true;

// Enable the "Allow mock locations" property
Mobile.Device().GPS.AllowMockLocations = true;

Longt = 174.8367068133983;
Lat = -36.87015422083155;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;

// Specify a mock location
Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

// Output the location data
Longt = Mobile.Device().GPS.Location.Longitude;
Lat = Mobile.Device().GPS.Location.Latitude;
Alt = Mobile.Device().GPS.Location.Altitude;
Acc = Mobile.Device().GPS.Location.Accuracy;
Gps = Mobile.Device().GPS.GPSEnabled;
Log.Message("Longitude: "+Longt);
Log.Message("Latitude: "+Lat);
Log.Message("Altitude: "+Alt);
Log.Message("Accuracy: "+Acc);
Log.Message("GPSEnabled: "+Gps);

// Disable mock locations
Mobile.Device().GPS.AllowMockLocations = false;
}

 

 

  • The issue got fixed after performing the below steps:-

    1. Downloaded the GPS test app the link which was given by one of the SmartBear representatives.

    Link - https://play.google.com/store/apps/details?id=com.chartcross.gpstest 

    2. Check the In View and In Use count.

    3. If the count is 0 then killing the adb instance. So that count becomes greater than 0.

    4. Then executing the script.

    5. Created a separate script to disable the mock location rather than disabling in the same script.

    6. Disabling the mock location in the same script was just setting the location first and then disabling it and on UI you couldn't see any change.

    7. So in short, killing the adb instance and checking for the fix in GPS test app before executing the script fixed the issue.

     

    Code snippet:-

    function MockGPSLocation()
    {
    var Longt, Lat, Alt, Acc, Gps;

    // Enable GPS
    Mobile.Device().GPS.GPSEnabled = true;

    Longt = Mobile.Device().GPS.Location.Longitude;
    Lat = Mobile.Device().GPS.Location.Latitude;
    Log.Message("Original Longitude: "+Longt);
    Log.Message("Original Latitude: "+Lat);

    // Enable the "Allow mock locations" property
    Mobile.Device().GPS.AllowMockLocations = true;

    Longt = 174.8367068133983;
    Lat = -36.87015422083155;
    Alt = Mobile.Device().GPS.Location.Altitude;
    Acc = Mobile.Device().GPS.Location.Accuracy;

    // Specify a mock location
    Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

    // Output the location data
    Longt = Mobile.Device().GPS.Location.Longitude;
    Lat = Mobile.Device().GPS.Location.Latitude;
    Alt = Mobile.Device().GPS.Location.Altitude;
    Acc = Mobile.Device().GPS.Location.Accuracy;
    Gps = Mobile.Device().GPS.GPSEnabled;
    Log.Message("Longitude: "+Longt);
    Log.Message("Latitude: "+Lat);
    Log.Message("Altitude: "+Alt);
    Log.Message("Accuracy: "+Acc);
    Log.Message("GPSEnabled: "+Gps);

    }

     

    function DisableMockLocation()
    {
    Mobile.Device().GPS.AllowMockLocations = false;
    }

2 Replies

    • MadhuraKhare's avatar
      MadhuraKhare
      New Contributor

      The issue got fixed after performing the below steps:-

      1. Downloaded the GPS test app the link which was given by one of the SmartBear representatives.

      Link - https://play.google.com/store/apps/details?id=com.chartcross.gpstest 

      2. Check the In View and In Use count.

      3. If the count is 0 then killing the adb instance. So that count becomes greater than 0.

      4. Then executing the script.

      5. Created a separate script to disable the mock location rather than disabling in the same script.

      6. Disabling the mock location in the same script was just setting the location first and then disabling it and on UI you couldn't see any change.

      7. So in short, killing the adb instance and checking for the fix in GPS test app before executing the script fixed the issue.

       

      Code snippet:-

      function MockGPSLocation()
      {
      var Longt, Lat, Alt, Acc, Gps;

      // Enable GPS
      Mobile.Device().GPS.GPSEnabled = true;

      Longt = Mobile.Device().GPS.Location.Longitude;
      Lat = Mobile.Device().GPS.Location.Latitude;
      Log.Message("Original Longitude: "+Longt);
      Log.Message("Original Latitude: "+Lat);

      // Enable the "Allow mock locations" property
      Mobile.Device().GPS.AllowMockLocations = true;

      Longt = 174.8367068133983;
      Lat = -36.87015422083155;
      Alt = Mobile.Device().GPS.Location.Altitude;
      Acc = Mobile.Device().GPS.Location.Accuracy;

      // Specify a mock location
      Mobile.Device().GPS.SetLocation(Longt,Lat,Alt,Acc);

      // Output the location data
      Longt = Mobile.Device().GPS.Location.Longitude;
      Lat = Mobile.Device().GPS.Location.Latitude;
      Alt = Mobile.Device().GPS.Location.Altitude;
      Acc = Mobile.Device().GPS.Location.Accuracy;
      Gps = Mobile.Device().GPS.GPSEnabled;
      Log.Message("Longitude: "+Longt);
      Log.Message("Latitude: "+Lat);
      Log.Message("Altitude: "+Alt);
      Log.Message("Accuracy: "+Acc);
      Log.Message("GPSEnabled: "+Gps);

      }

       

      function DisableMockLocation()
      {
      Mobile.Device().GPS.AllowMockLocations = false;
      }