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
//
// Diagnostic.m
// Plugin diagnostic
//
// Copyright (c) 2012 AVANTIC ESTUDIO DE INGENIEROS
//
#import "Diagnostic.h"
#import <CoreLocation/CoreLocation.h>
#import <arpa/inet.h> // For AF_INET, etc.
#import <ifaddrs.h> // For getifaddrs()
#import <net/if.h> // For IFF_LOOPBACK
@implementation Diagnostic
- (void)pluginInitialize {
NSLog(@"RFduino Cordova Plugin");
NSLog(@"(c)2013-2015 Don Coleman");
[super pluginInitialize];
self.bluetoothManager = [[CBCentralManager alloc]
initWithDelegate:self
queue:dispatch_get_main_queue()
options:@{CBCentralManagerOptionShowPowerAlertKey: @(NO)}];
}
- (void) isLocationEnabled: (CDVInvokedUrlCommand*)command
{
NSLog(@"Loading Location status...");
CDVPluginResult* pluginResult;
if([self isLocationEnabled] && [self isLocationAuthorized]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void) isLocationEnabledSetting: (CDVInvokedUrlCommand*)command
{
NSLog(@"Loading Location status...");
CDVPluginResult* pluginResult;
if([self isLocationEnabled]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void) isLocationAuthorized: (CDVInvokedUrlCommand*)command
{
NSLog(@"Loading Location authentication...");
CDVPluginResult* pluginResult;
if([self isLocationAuthorized]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (BOOL) isLocationEnabled
{
if([CLLocationManager locationServicesEnabled]) {
NSLog(@"Location is enabled.");
return true;
} else {
NSLog(@"Location is disabled.");
return false;
}
}
- (BOOL) isLocationAuthorized
{
if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {
NSLog(@"This app is authorized to use location.");
return true;
} else {
NSLog(@"This app is not authorized to use location.");
return false;
}
}
- (void) isWifiEnabled: (CDVInvokedUrlCommand*)command
{
NSLog(@"Loading WiFi status...");
CDVPluginResult* pluginResult;
if([self connectedToWifi]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (BOOL) connectedToWifi // Don't work on iOS Simulator, only in the device
{
struct ifaddrs *addresses;
struct ifaddrs *cursor;
BOOL wiFiAvailable = NO;
if (getifaddrs(&addresses) != 0) {
return NO;
}
cursor = addresses;
while (cursor != NULL) {
if (cursor -> ifa_addr -> sa_family == AF_INET && !(cursor -> ifa_flags & IFF_LOOPBACK)) // Ignore the loopback address
{
// Check for WiFi adapter
if (strcmp(cursor -> ifa_name, "en0") == 0) {
NSLog(@"Wifi ON");
wiFiAvailable = YES;
break;
}
}
cursor = cursor -> ifa_next;
}
freeifaddrs(addresses);
return wiFiAvailable;
}
- (void) isCameraEnabled: (CDVInvokedUrlCommand*)command
{
NSLog(@"Loading camera status...");
CDVPluginResult* pluginResult;
if([self isCameraEnabled]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
}
else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (BOOL) isCameraEnabled
{
BOOL cameraAvailable =
[UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
if(cameraAvailable) {
NSLog(@"Camera enabled");
return true;
}
else {
NSLog(@"Camera disabled");
return false;
}
}
- (void) isBluetoothEnabled: (CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;
if(self.bluetoothEnabled) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
#pragma mark - CBCentralManagerDelegate
- (void) centralManagerDidUpdateState:(CBCentralManager *)central {
if ([central state] == CBCentralManagerStatePoweredOn) {
NSLog(@"Bluetooth enabled");
self.bluetoothEnabled = true;
}
else {
NSLog(@"Bluetooth disabled or unavailable");
self.bluetoothEnabled = false;
}
}
- (void) switchToSettings: (CDVInvokedUrlCommand*)command
{
CDVPluginResult* pluginResult;
if (UIApplicationOpenSettingsURLString != nil){
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: UIApplicationOpenSettingsURLString]];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
}else{
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported below iOS 8"];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end