The diagnostic plugin allows you to check different device settings in your PhoneGap application.
A simple use case would be:
- Your app require geolocation and you want check if the location services are enabled in device settings.
- Your app require the Wi-Fi enabled for the wireless network location and you want check if the Wi-Fi is enabled in device settings.
- Your app require the bluetooth enabled and you want check if the bluetooth is enabled in device settings.
- You want that the user enable the location services, Wi-Fi or bluetooth in device settings.
Using this plugin requires a PhoneGap project for Android: Get Started Guide.
-
To install the plugin, move diagnostic.js to your project's www folder and include a reference to it in your html file after cordova.js:
<script type="text/javascript" charset="utf-8" src="cordova-X.X.X.js"></script> <script type="text/javascript" charset="utf-8" src="diagnostic.js"></script>
-
Create a directory within your project called src/net/avantic/diagnosticPlugin and move Diagnostic.java into it.
-
In your res/xml/plugins.xml file add the following line:
<plugin name="Diagnostic" value="net.avantic.diagnosticPlugin.Diagnostic" />
-
And in the AndroidManifest.xml add:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.BLUETOOTH" />
The plugin creates the object:
window.plugins.diagnostic
To use, call one of the following, available methods:
- isLocationEnabled:
/** * Checks device settings for location. * * @param successCallback The callback which will be called when diagnostic of location is successful. * This callback function have a boolean param with the diagnostic result. * @param errorCallback The callback which will be called when diagnostic of location encounters an error. * This callback function have a string param with the error. */ isLocationEnabled()
Usage:
window.plugins.diagnostic.isLocationEnabled(locationEnabledSuccessCallback, locationEnabledErrorCallback); function locationEnabledSuccessCallback(result) { if (result) alert("Location ON"); else alert("Location OFF"); } function locationEnabledErrorCallback(error) { console.log(error); }
- switchToLocationSettings:
/** * Requests that the user enable the location services in device settings. */ switchToLocationSettings()
Usage:
alert("You must enable the location services in device settings."); window.plugins.diagnostic.switchToLocationSettings();
- isGpsEnabled:
/** * Checks device settings for GPS. * * @param successCallback The callback which will be called when diagnostic of GPS is successful. * This callback function have a boolean param with the diagnostic result. * @param errorCallback The callback which will be called when diagnostic of GPS encounters an error. * This callback function have a string param with the error. */ isGpsEnabled()
Usage:
window.plugins.diagnostic.isGpsEnabled(gpsEnabledSuccessCallback, gpsEnabledErrorCallback); function gpsEnabledSuccessCallback(result) { if (result) alert("GPS ON"); else alert("GPS OFF"); } function gpsEnabledErrorCallback(error) { console.log(error); }
- isWirelessNetworkLocationEnabled:
/** * Checks device settings for wireless network location (Wi-Fi and/or mobile networks). * * @param successCallback The callback which will be called when diagnostic of wireless network location is successful. * This callback function have a boolean param with the diagnostic result. * @param errorCallback The callback which will be called when diagnostic of wireless network location encounters an error. * This callback function have a string param with the error. */ isWirelessNetworkLocationEnabled()
Usage:
window.plugins.diagnostic.isWirelessNetworkLocationEnabled(wirelessNetworkLocationEnabledSuccessCallback, wirelessNetworkLocationEnabledErrorCallback); function wirelessNetworkLocationEnabledSuccessCallback(result) { if (result) alert("Wireless network location ON"); else alert("Wireless network location OFF"); } function wirelessNetworkLocationEnabledErrorCallback(error) { console.log(error); }
- isWifiEnabled:
/** * Checks device settings for Wi-Fi. * * @param successCallback The callback which will be called when diagnostic of Wi-Fi is successful. * This callback function have a boolean param with the diagnostic result. * @param errorCallback The callback which will be called when diagnostic of Wi-Fi encounters an error. * This callback function have a string param with the error. */ isWifiEnabled()
Usage:
window.plugins.diagnostic.isWifiEnabled(wifiEnabledSuccessCallback, wifiEnabledErrorCallback); function wifiEnabledSuccessCallback(result) { if (result) alert("Wi-Fi ON"); else alert("Wi-Fi OFF"); } function wifiEnabledErrorCallback(error) { console.log(error); }
- switchToWifiSettings:
/** * Requests that the user enable the Wi-Fi in device settings. */ switchToWifiSettings()
Usage:
alert("You must enable the Wi-Fi in device settings."); window.plugins.diagnostic.switchToWifiSettings();
- isBluetoothEnabled:
/** * Checks device settings for bluetooth. * * @param successCallback The callback which will be called when diagnostic of bluetooth is successful. * This callback function have a boolean param with the diagnostic result. * @param errorCallback The callback which will be called when diagnostic of bluetooth encounters an error. * This callback function have a string param with the error. */ isBluetoothEnabled()
Usage:
window.plugins.diagnostic.isBluetoothEnabled(bluetoothEnabledSuccessCallback, bluetoothEnabledErrorCallback); function bluetoothEnabledSuccessCallback(result) { if (result) alert("Bluetooth ON"); else alert("Bluetooth OFF"); } function bluetoothEnabledErrorCallback(error) { console.log(error); }
- switchToBluetoothSettings:
/** * Requests that the user enable the bluetooth in device settings. */ switchToBluetoothSettings()
Usage:
alert("You must enable the Bluetooth in device settings."); window.plugins.diagnostic.switchToBluetoothSettings();
Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
The MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.