Mehmood Memon
Mehmood Memon

Reputation: 1139

Cordova diagnostic plugin not working

I am using diagnostic plugin to get information about whether the location is turned on or not. The plugin was working fine until I removed it due to some problem in code.

Now when I have added the plugin again in the same app, it's not working.

Same plugin is working in another app that I created for demo purpose.

I have also tried the legacy version of the diagnostic plugin.

function checkLocationEnabled(){
    cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
        alert("Location is " + (enabled ? "enabled" : "disabled"));
        alert(enabled);
        if(enabled == false){
            cordova.plugins.locationAccuracy.canRequest(function(canRequest){
                if(canRequest){
                    cordova.plugins.locationAccuracy.request(function(){
                        alert("GPS turned on");
                        setLatitudeLongitude();
                    }, function (error){
                        alert("Request failed");
                        if(error){
                            // Android only
                            alert("error code="+error.code+"; error message="+error.message);
                            if(error.code !== cordova.plugins.locationAccuracy.ERROR_USER_DISAGREED){
                                if(window.confirm("Failed to automatically set Location Mode to 'High Accuracy'. Would you like to switch to the Location Settings page and do this manually?")){
                                    cordova.plugins.diagnostic.switchToLocationSettings();
                                }
                            }
                        }
                    }, cordova.plugins.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY // iOS will ignore this
                    );
                }
            });
        } else if (enabled == true){
            setLatitudeLongitude();
        }
    }, function(error){
        alert("The following error occurred: "+error);
    });
}

entry in my config.xml file

<plugin name="cordova.plugins.diagnostic" spec="~3.6.5" />

Cordova version: 6.5.0

Installed platforms: android 6.2.3 browser 4.1.0 ios 4.3.1

<preference name="android-minSdkVersion" value="14" />

Tried to debug on chrome and the it shows the error 'cannot read property diagnostic' of undefined.

Also I am using Windows 7 and I have tested the same app on different versions of android 5, 5.1, 6.0. (Samsung + Sony)

It used to work before and display alert that 'Location is enabled/disabled'.

Upvotes: 3

Views: 7269

Answers (1)

DaveAlden
DaveAlden

Reputation: 30366

it shows the error 'cannot read property isLocationEnabled' of undefined.

This indicates the cordova.plugins.diagnostic object is not available.

This is most commonly caused by attempting to invoke a plugin before the deviceready event has fired, since Cordova dynamically loads the JS components of plugins at runtime.

Another potential cause is the plugin is not installed into the project properly - check in Chrome Dev tools if diagnostic.js is listed in Sources. If not present, do cordova platform rm android && cordova platform add android to rebuild the native Android project.

Upvotes: 4

Related Questions