GPS or other localization service is on/off?

Hi there,

I tried https://github.com/dpa99c/cordova-diagnostic-plugin to recognize, if GPS is active or not, before going on with another task for geolocalization …

I tried this plugin on iOS and it works fine. But the build for Android won’t work after installing this plugin. Removing the plugin resolved the Android-nobuild-issue, but now there’s no routine to check if GPS is active or not.

Using …
Ionic: 1.7.12
Cordova: 5.4.1
npm: 2.11.3
Android-SDK-Components: ALL selected and installed over the “Android SDK Manager”.

Question: Is there perhaps another recomended Ionic plugin for detecting GPS is active or not? Or is there an even easier way to get the GPS-State?

And here comes the error message when building for Android:

:processDebugResourcesT:\Coding\myApp\platforms\android\build\intermediates\res\debug\values-v23\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.
Widget.Button.Inverse’. FAILED

T:\Coding\myApp\platforms\android\build\intermediates\res\debug\values-v23\values.xml:26: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processDebugResources'.

BUILD FAILED

Total time: 47.683 secs
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\me\AppData\Local\Android\sdk\build-tools\23.0.2\aapt.exe package -f --no-crunch -I C:\Users\me\AppData\Local\Android\sdk\platforms\android-22\android.jar -M T:\Coding\myApp\platforms\android\build\int
ermediates\manifests\full\debug\AndroidManifest.xml -S T:\Coding\myApp\platforms\android\build\intermediates\res\debug -A T:\Coding\myApp\platforms\android\build\intermediates\assets\debug -m -J T:\Coding\myApp\platform
s\android\build\generated\source\r\debug -F T:\Coding\myApp\platforms\android\build\intermediates\res\resources-debug.ap_ --debug-mode --custom-package de.rmg_ratingen.ratingenapp -0 apk --output-text-symbols T:\Coding\myApp\
platforms\android\build\intermediates\symbols\debug
Error Code:
1
Output:
T:\Coding\myApp\platforms\android\build\intermediates\res\debug\values-v23\values.xml:5: error: Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.
Inverse'.

T:\Coding\myApp\platforms\android\build\intermediates\res\debug\values-v23\values.xml:26: error: Error retrieving parent for item: No resource found that matches the given name 'android:Widget.Material.Button.Colored'.



* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

T:\Coding\myApp\platforms\android\cordova\node_modules\q\q.js:126
            throw e;
                  ^
Error code 1 for command: cmd with args: /s /c "T:\Coding\myApp\platforms\android\gradlew cdvBuildDebug -b T:\Coding\myApp\platforms\android\build.gradle -Dorg.gradle.daemon=true"
ERROR building one of the platforms: Error: cmd: Command failed with exit code 1
You may not have the required environment or OS to build this project
Error: cmd: Command failed with exit code 1

I don’t use that diagnostic thing, standard geolocation plugin will tell you if GPS is working or not.
And your processDebugResources error sounds like you have resources defined in config.xml and you shouldn’t have to do that because ionic resources command will do everything for you.

Yeah right! Using the plugin ionic plugin add cordova-plugin-geolocation I can run the method $cordovaGeolocation.getCurrentPosition().

But BEFORE running that method I want to check if GPS is active, so I don’t need to wait for a timeout. By the way the rejected error object of $cordovaGeolocation.getCurrentPosition() is { "message": "Timeout expired", "code": 3 }, so it does not tell me anything about the GPS active state.

Something like:
if (gps.isActive()) $cordovaGeolocation.getCurrentPosition().then(…);

But http://ngcordova.com/docs/plugins/geolocation/ shows me, that $cordovaGeolocation has no methods to check GPS-active-state.

So back to my question: How to check the active state of the GPS on a device (without need of waiting for a timeout)? Or am I missing something?

The thing is, GPS doesn’t have to be active/turned on to be able to find accurate position. Some devices (some iPads) don’t have GPS but they are returning correct location via other methods.

I know … like Network.

But if I turn everything off (e.g. setting Flight Mode on), so no connection to the outer world exists, then I still get the error { "message": "Timeout expired", "code": 3 }.

If you want so I can change my question:
Old one: How to check the active state of the GPS on a device (without need of waiting for a timeout)?
New one: How to check the active state of a localization service, like GPS/Network, on a device (without need of waiting for a timeout)?

Has no one any clue?

build with android 5.0.0 platform
ionic platform add android@5.0.0
then build android project.

Use Cordova plugin diagnostic.
https://www.npmjs.com/package/cordova.plugins.diagnostic

in order to get the GPS state , do this:

cordova.plugins.diagnostic.isLocationEnabled(function(enabled) { console.log("Location is " + (enabled ? "enabled" : "disabled")); console.log (enabled); }, function(error) { console.error("The following error occurred:"+error); });

1 Like

Hmm … very strange. That’s the same Plugin I used at the first post.

Now using …
Ionic: 1.7.14
Cordova: 6.1.1

Thanks so … it works today :slight_smile:

I get the following error with that cordova plugin. Anyone know why?

Please see:

What versions of the frameworks you’re using?

Is this what you are asking for:

package.json

{
  "dependencies": {
    "@angular/common": "^2.0.0-rc.4",
    "@angular/compiler": "^2.0.0-rc.4",
    "@angular/core": "^2.0.0-rc.4",
    "@angular/forms": "^0.2.0",
    "@angular/http": "^2.0.0-rc.4",
    "@angular/platform-browser": "^2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "^2.0.0-rc.4",
    "accounts-base-client-side": "^0.1.1",
    "accounts-phone": "0.0.1",
    "angular2-meteor": "^0.6.2",
    "angular2-moment": "^0.8.2",
    "es6-shim": "^0.35.0",
    "ionic-angular": "^2.0.0-beta.11",
    "ionic-native": "1.3.2",
    "ionicons": "3.0.0",
    "meteor-client-side": "^1.3.4",
    "moment": "^2.14.1",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "socket.io": "^1.4.8",
    "zone.js": "^0.6.12"
  },
  "devDependencies": {
    "del": "2.2.0",
    "gulp": "3.9.1",
    "gulp-watch": "4.3.5",
    "ionic-gulp-fonts-copy": "^1.0.0",
    "ionic-gulp-html-copy": "^1.0.0",
    "ionic-gulp-sass-build": "^1.0.0",
    "ionic-gulp-scripts-copy": "^2.0.0",
    "ionic-gulp-tslint": "^1.0.0",
    "ionic-gulp-webpack": "^2.0.0",
    "lodash.camelcase": "^4.3.0",
    "lodash.upperfirst": "^4.3.1",
    "run-sequence": "1.1.5",
    "ts-loader": "^0.8.2",
    "tslint-ionic-rules": "^0.0.3",
    "webpack": "^1.13.2"
  },
  "name": "theWhoZoo",
  "description": "theWhoZoo: An Ionic project",
  "cordovaPlugins": [
    "cordova-plugin-device",
    "cordova-plugin-console",
    "cordova-plugin-whitelist",
    "cordova-plugin-splashscreen",
    "cordova-plugin-statusbar",
    "ionic-plugin-keyboard",
    {
      "variables": {
        "SENDER_ID": "xxxxxxxxx"
      },
      "locator": "phonegap-plugin-push",
      "id": "phonegap-plugin-push"
    }
  ],
  "cordovaPlatforms": []
}

Not directly … Type in console:

ionic --version
cordova --version

Try allthough:
ionic plugins list
or
cordova plugins list

It should show up something like:
cordova.plugins.diagnostic 3.1.1 "Diagnostic"

But hey … wheres your cordova.plugins.diagnostic? It seems like you do not have it integrated in your project. It has to be in the cordovaPlugins-array in your package.json. Please check if you have a folder called cordova.plugins.diagnostic in your plugins folder of your project.

If not then install it :slight_smile:

Thanks. That is the output once I remove it. Sorry.

But I have decided not to use it, rater just to wait for a timeout on the native Geolocation instead before I display an error.

        let options = {
            timeout: 10000,
            enableHighAccuracy: true
        };
        Geolocation.getCurrentPosition(options).then((position) => {
            this.latitude = position.coords.latitude;
            this.longitude = position.coords.longitude;
            this.getJobRangeSearch(this.searchQuery);
        }).catch((error)=> {
            this.doAlert(error+' /n/n Please ensure your devices Location Service is turned on');
        });

No! DON’T! I did the same … It’s not good programming to let the user wait for 10 seconds until he realizes, that he cannot use geolocation. There IS a way to ask directly if geolocation is possible, without any timeouts. You’ll find it in the diagnostic plugin, its called isLocationEnabled.

You’ll be happier with your work, if you get it running. So … do you have the plugin integrated to your project? Is there that folder for the plugin in your plugins directory?

Give it a try!

Okay, thanks for the advise:

ionic --version
2.0.0-beta.37

cordova --version
6.3.1

cordova plugin add cordova.plugins.diagnostic

ionic plugins list
cordova-plugin-camera 2.2.0 "Camera"
cordova-plugin-compat 1.0.0 "Compat"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-crop 0.1.0 "CropPlugin"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-geolocation 2.2.0 "Geolocation"
cordova-plugin-googlemaps 1.3.9 "phonegap-googlemaps-plugin"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
cordova.plugins.diagnostic 3.2.1 "Diagnostic"
ionic-plugin-keyboard 2.2.1 "Keyboard"
phonegap-plugin-push 1.8.2 “PushPlugin”

search.ts

  private checkLocationIsOn(): void {
    if (window.cordova && cordova.plugins) {
      cordova.plugins.diagnostic.isLocationEnabled(function (enabled) {
        alert("Location is " + (enabled ? "enabled" : "disabled"));
      }, function (error) {
        alert("The following error occurred: " + error);
      });
    }
  }

CLI on start up:

ERROR in ./app/pages/search/search.ts
(81,23): error TS2339: Property 'diagnostic' does not exist on type 'CordovaPlugins'

Is it in the package.json? There’s an array called cordovaPlugins. It should have an item (string) like "cordova.plugins.diagnostic"

package.json:

{
  "name": "myapp",
  "version": "1.1.1",
  ...
  "cordovaPlugins": [
    ...
    "cordova.plugins.diagnostic",
    ...
  ],
  "cordovaPlatforms": [
    "android",
    "ios"
  ]
}

Nope:

  "cordovaPlugins": [
    "cordova-plugin-device",
    "cordova-plugin-console",
    "cordova-plugin-whitelist",
    "cordova-plugin-splashscreen",
    "cordova-plugin-statusbar",
    "ionic-plugin-keyboard",
    {
      "variables": {
        "SENDER_ID": "XXXXXXXX"
      },
      "locator": "phonegap-plugin-push",
      "id": "phonegap-plugin-push"
    }
  ],
  "cordovaPlatforms": []

Shouldn’t it have been added when I ran the following?

cordova plugin add cordova.plugins.diagnostic

Must I add it manually?

UPDATE

When I add it manually to package.json, restart ionic serve, I still get the same error.

"cordova.plugins.diagnostic",

Yes, it should be written in automatically. It SHOULD not be inserted by hand, right. But I was confused, why it was missing. So … you still have the problem. Its very strange that none of your cordova plugins are listed here.

Oh … you serve your project? Try it on an emulator or real hardware by
ionic run android --device
Then it should work … It won’t work on your browser.

Forget about this ionic serve.

If you still want to debug your views you can use in Chrome the url “chrome://inspect” with a running emulator or your real device attached, so you’ll never wan’t to work with the serve mode again. Enjoy!

1 Like

Thanks, but it still doesn’t explain why some cordova plugins work, and this one doesn’t.