Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
Cordova diagnostic plugin
=========================
* [Overview](#overview)
* [Installation](#installation)
* [Usage](#usage)
* [Android and iOS](#android-and-ios)
- [isLocationEnabled()](#islocationenabled)
- [isWifiEnabled()](#iswifienabled)
- [isCameraEnabled()](#iscameraenabled)
- [isBluetoothEnabled()](#isbluetoothenabled)
* [Android only](#android-only)
- [isGpsLocationEnabled()](#isgpslocationenabled)
- [isNetworkLocationEnabled()](#isnetworklocationenabled)
- [switchToLocationSettings()](#switchtolocationsettings)
- [switchToMobileDataSettings()](#switchtomobiledatasettings)
- [switchToBluetoothSettings()](#switchtobluetoothsettings)
- [switchToWifiSettings()](#switchtowifisettings)
* [iOS only](#ios-only)
- [isLocationEnabledSetting()](#isLocationEnabledSetting)
- [isLocationAuthorized()](#islocationauthorized)
- [switchToSettings()](#switchtosettings)
* [Example project](#example-project)
* [Credits](#credits)
# Overview
This Cordova/Phonegap plugin for iOS and Android is used to check the state of the following device settings:
- Location
- WiFi
- Camera
- Bluetooth
The plugin also enables an app to show the relevant settings screen, to allow users to change the above device settings.
The plugin is registered in the [the Cordova Registry](http://plugins.cordova.io/#/package/cordova.plugins.diagnostic)(Cordova CLI 3/4) and on [npm](https://www.npmjs.com/package/cordova.plugins.diagnostic) (Cordova CLI 5+) as `cordova.plugins.diagnostic`
# Installation
## Using the Cordova/Phonegap [CLI](http://docs.phonegap.com/en/edge/guide_cli_index.md.html)
$ cordova plugin add cordova.plugins.diagnostic
$ phonegap plugin add cordova.plugins.diagnostic
## Using [Cordova Plugman](https://github.com/apache/cordova-plugman)
$ plugman install --plugin=cordova.plugins.diagnostic --platform=<platform> --project=<project_path> --plugins_dir=plugins
For example, to install for the Android platform
$ plugman install --plugin=cordova.plugins.diagnostic --platform=android --project=platforms/android --plugins_dir=plugins
## PhoneGap Build
Add the following xml to your config.xml to use the latest version of this plugin from [the Cordova Registry](http://plugins.cordova.io/#/package/cordova.plugins.diagnostic):
<gap:plugin name="cordova.plugins.diagnostic" source="plugins.cordova.io" />
or from [npm](https://www.npmjs.com/package/cordova.plugins.diagnostic):
<gap:plugin name="cordova.plugins.diagnostic" source="npm" />
# Usage
The plugin is exposed via the `cordova.plugins.diagnostic` object and provides the following functions:
## Android and iOS
### isLocationEnabled()
Checks if app is able to access device location.
cordova.plugins.diagnostic.isLocationEnabled(successCallback, errorCallback);
On iOS this returns true if both the device setting for Location Services is ON, AND the application is authorized to use location.
When location is enabled, the locations returned are by a mixture GPS hardware, network triangulation and Wifi network IDs.
On Android, this returns true if Location mode is enabled and any mode is selected (e.g. Battery saving, Device only, High accuracy)
When location is enabled, the locations returned are dependent on the location mode:
* Battery saving = network triangulation and Wifi network IDs (low accuracy)
* Device only = GPS hardware only (high accuracy)
* High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isLocationEnabled(function(enabled){
console.log("Location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### isWifiEnabled()
Checks if Wifi is connected/enabled.
On iOS this returns true if the device is connected to a network by WiFi.
On Android this returns true if the WiFi setting is set to enabled.
cordova.plugins.diagnostic.isWifiEnabled(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isWifiEnabled(function(enabled){
console.log("WiFi is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### isCameraEnabled()
Checks if the device has a camera (same on Android and iOS)
cordova.plugins.diagnostic.isCameraEnabled(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isCameraEnabled(function(exists){
console.log("Device " + (exists ? "does" : "does not") + " have a camera");
}, function(error){
console.error("The following error occurred: "+error);
});
### isBluetoothEnabled()
Checks if the device has Bluetooth capabilities and if so that Bluetooth is switched on (same on Android and iOS)
cordova.plugins.diagnostic.isBluetoothEnabled(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isBluetoothEnabled(function(enabled){
console.log("Bluetooth is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
## Android only
### isGpsLocationEnabled()
Checks if location mode is set to return high-accuracy locations from GPS hardware.
cordova.plugins.diagnostic.isGpsLocationEnabled(successCallback, errorCallback);
Returns true if Location mode is enabled and is set to either:
* Device only = GPS hardware only (high accuracy)
* High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isGpsLocationEnabled(function(enabled){
console.log("GPS location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### isNetworkLocationEnabled()
Checks if location mode is set to return low-accuracy locations from network triangulation/WiFi access points.
cordova.plugins.diagnostic.isNetworkLocationEnabled(successCallback, errorCallback);
Returns true if Location mode is enabled and is set to either:
* Battery saving = network triangulation and Wifi network IDs (low accuracy)
* High accuracy = GPS hardware, network triangulation and Wifi network IDs (high and low accuracy)
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isNetworkLocationEnabled(function(enabled){
console.log("Network location is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### switchToLocationSettings()
Displays the device location settings to allow user to enable location services/change location mode.
cordova.plugins.diagnostic.switchToLocationSettings();
### switchToMobileDataSettings()
Displays mobile settings to allow user to enable mobile data.
cordova.plugins.diagnostic.switchToMobileDataSettings();
### switchToBluetoothSettings()
Displays Bluetooth settings to allow user to enable Bluetooth.
cordova.plugins.diagnostic.switchToBluetoothSettings();
### switchToWifiSettings()
Displays WiFi settings to allow user to enable WiFi.
cordova.plugins.diagnostic.switchToWifiSettings();
## iOS only
### isLocationEnabledSetting()
Returns true if the device setting for location is on.
cordova.plugins.diagnostic.isLocationEnabledSetting(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isLocationEnabledSetting(function(enabled){
console.log("Location setting is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### isLocationAuthorized()
Returns true if the application is authorized to use location AND the device setting for location is on.
cordova.plugins.diagnostic.isLocationAuthorized(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when diagnostic is successful.
This callback function is passed a single boolean parameter with the diagnostic result.
- {Function} errorCallback - The callback which will be called when diagnostic encounters an error.
This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.isLocationAuthorized(function(enabled){
console.log("Location authorization is " + (enabled ? "enabled" : "disabled"));
}, function(error){
console.error("The following error occurred: "+error);
});
### switchToSettings()
Switch to Settings app. Opens settings page for this app. This works only on iOS 8+. iOS 7 and below will invoke the errorCallback.
cordova.plugins.diagnostic.switchToSettings(successCallback, errorCallback);
#### Parameters
- {Function} successCallback - The callback which will be called when switch to settings is successful.
- {Function} errorCallback - The callback which will be called when switch to settings encounters an error. This callback function is passed a single string parameter containing the error message.
#### Example usage
cordova.plugins.diagnostic.switchToSettings(function(){
console.log("Successfully switched to Settings app"));
}, function(error){
console.error("The following error occurred: "+error);
});
# Example project
An example project illustrating use of this plugin can be found here: [https://github.com/dpa99c/cordova-diagnostic-plugin-example](https://github.com/dpa99c/cordova-diagnostic-plugin-example)
# Credits
Forked from: [https://github.com/mablack/cordova-diagnostic-plugin](https://github.com/mablack/cordova-diagnostic-plugin)
Orignal Cordova 2 implementation by: AVANTIC ESTUDIO DE INGENIEROS ([www.avantic.net](http://www.avantic.net/))