community-cordova-plugin-magnetometer 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +37 -0
- package/package.json +1 -1
- package/src/android/Magnetometer.java +40 -5
- package/src/ios/CDVMagnetometer.m +29 -10
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.0.3] - 2025-02-04
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- Error handling now returns structured error objects with `code` and `message` properties
|
|
13
|
+
- Error code `3` (NOT_AVAILABLE) is now consistently returned when magnetometer sensor is unavailable
|
|
14
|
+
- Improved error detection for "sensor not available" scenarios across both Android and iOS
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
|
|
18
|
+
- Consistent error format across platforms (Android and iOS now both return `{code: 3, message: "..."}` for unavailable sensor)
|
|
19
|
+
|
|
8
20
|
## [1.0.0] - 2025-01-20
|
|
9
21
|
|
|
10
22
|
### Added
|
package/README.md
CHANGED
|
@@ -242,6 +242,43 @@ Get the total magnetic field strength (magnitude).
|
|
|
242
242
|
|
|
243
243
|
**Returns:** Field strength in microteslas (μT).
|
|
244
244
|
|
|
245
|
+
## Error Handling
|
|
246
|
+
|
|
247
|
+
When errors occur, the plugin returns structured error objects:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
interface MagnetometerError {
|
|
251
|
+
code: number;
|
|
252
|
+
message: string;
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Error Codes
|
|
257
|
+
|
|
258
|
+
| Code | Constant | Description |
|
|
259
|
+
|------|----------|-------------|
|
|
260
|
+
| `3` | `NOT_AVAILABLE` | Magnetometer sensor is not available on the device |
|
|
261
|
+
|
|
262
|
+
### Example Error Handling
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
MagnetometerPlugin.watchReadings(
|
|
266
|
+
(reading) => {
|
|
267
|
+
console.log('Reading:', reading);
|
|
268
|
+
},
|
|
269
|
+
(error) => {
|
|
270
|
+
if (error.code === 3) {
|
|
271
|
+
// Sensor not available - show user-friendly message
|
|
272
|
+
console.log('This device does not have a magnetic sensor');
|
|
273
|
+
} else {
|
|
274
|
+
// Other error
|
|
275
|
+
console.error('Magnetometer error:', error.message);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
{ frequency: 100 }
|
|
279
|
+
);
|
|
280
|
+
```
|
|
281
|
+
|
|
245
282
|
## Interfaces
|
|
246
283
|
|
|
247
284
|
### IMagnetometerReading
|
package/package.json
CHANGED
|
@@ -20,6 +20,9 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
20
20
|
|
|
21
21
|
private static final String LOG_TAG = "Magnetometer";
|
|
22
22
|
|
|
23
|
+
// Error codes - matching DeviceOrientation plugin convention
|
|
24
|
+
private static final int ERROR_NOT_AVAILABLE = 3;
|
|
25
|
+
|
|
23
26
|
private SensorManager sensorManager;
|
|
24
27
|
private Sensor magnetometer;
|
|
25
28
|
private Sensor rotationVector;
|
|
@@ -96,7 +99,7 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
96
99
|
|
|
97
100
|
private void getReading(final CallbackContext callbackContext) {
|
|
98
101
|
if (magnetometer == null) {
|
|
99
|
-
callbackContext
|
|
102
|
+
sendError(callbackContext, ERROR_NOT_AVAILABLE, "Magnetometer not available");
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
102
105
|
|
|
@@ -146,7 +149,7 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
146
149
|
|
|
147
150
|
private void getHeading(final CallbackContext callbackContext) {
|
|
148
151
|
if (magnetometer == null) {
|
|
149
|
-
callbackContext
|
|
152
|
+
sendError(callbackContext, ERROR_NOT_AVAILABLE, "Magnetometer not available");
|
|
150
153
|
return;
|
|
151
154
|
}
|
|
152
155
|
|
|
@@ -217,7 +220,7 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
217
220
|
|
|
218
221
|
private void watchReadings(CallbackContext callbackContext, final int frequency) {
|
|
219
222
|
if (magnetometer == null) {
|
|
220
|
-
callbackContext
|
|
223
|
+
sendError(callbackContext, ERROR_NOT_AVAILABLE, "Magnetometer not available");
|
|
221
224
|
return;
|
|
222
225
|
}
|
|
223
226
|
|
|
@@ -246,7 +249,7 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
246
249
|
|
|
247
250
|
private void watchHeading(CallbackContext callbackContext, final int frequency) {
|
|
248
251
|
if (magnetometer == null) {
|
|
249
|
-
callbackContext
|
|
252
|
+
sendError(callbackContext, ERROR_NOT_AVAILABLE, "Magnetometer not available");
|
|
250
253
|
return;
|
|
251
254
|
}
|
|
252
255
|
|
|
@@ -388,7 +391,7 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
388
391
|
|
|
389
392
|
private void getFieldStrength(final CallbackContext callbackContext) {
|
|
390
393
|
if (magnetometer == null) {
|
|
391
|
-
callbackContext
|
|
394
|
+
sendError(callbackContext, ERROR_NOT_AVAILABLE, "Magnetometer not available");
|
|
392
395
|
return;
|
|
393
396
|
}
|
|
394
397
|
|
|
@@ -507,6 +510,38 @@ public class Magnetometer extends CordovaPlugin implements SensorEventListener {
|
|
|
507
510
|
}
|
|
508
511
|
}
|
|
509
512
|
|
|
513
|
+
/**
|
|
514
|
+
* Send a structured error with code and message
|
|
515
|
+
*/
|
|
516
|
+
private void sendError(CallbackContext callbackContext, int code, String message) {
|
|
517
|
+
try {
|
|
518
|
+
JSONObject error = new JSONObject();
|
|
519
|
+
error.put("code", code);
|
|
520
|
+
error.put("message", message);
|
|
521
|
+
callbackContext.error(error);
|
|
522
|
+
} catch (JSONException e) {
|
|
523
|
+
callbackContext.error(message);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Send a structured error with code and message, keeping callback alive
|
|
529
|
+
*/
|
|
530
|
+
private void sendErrorKeepCallback(CallbackContext callbackContext, int code, String message) {
|
|
531
|
+
try {
|
|
532
|
+
JSONObject error = new JSONObject();
|
|
533
|
+
error.put("code", code);
|
|
534
|
+
error.put("message", message);
|
|
535
|
+
PluginResult result = new PluginResult(PluginResult.Status.ERROR, error);
|
|
536
|
+
result.setKeepCallback(true);
|
|
537
|
+
callbackContext.sendPluginResult(result);
|
|
538
|
+
} catch (JSONException e) {
|
|
539
|
+
PluginResult result = new PluginResult(PluginResult.Status.ERROR, message);
|
|
540
|
+
result.setKeepCallback(true);
|
|
541
|
+
callbackContext.sendPluginResult(result);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
510
545
|
@Override
|
|
511
546
|
public void onReset() {
|
|
512
547
|
if (watchCallbackContext != null) {
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
#import "CDVMagnetometer.h"
|
|
2
2
|
|
|
3
|
+
// Error codes - matching DeviceOrientation plugin convention
|
|
4
|
+
static const int ERROR_NOT_AVAILABLE = 3;
|
|
5
|
+
|
|
3
6
|
@implementation CDVMagnetometer
|
|
4
7
|
|
|
5
8
|
- (void)pluginInitialize {
|
|
@@ -10,6 +13,27 @@
|
|
|
10
13
|
self.calibrationNeeded = NO;
|
|
11
14
|
}
|
|
12
15
|
|
|
16
|
+
#pragma mark - Error Helpers
|
|
17
|
+
|
|
18
|
+
- (void)sendErrorWithCode:(int)code message:(NSString *)message callbackId:(NSString *)callbackId {
|
|
19
|
+
NSDictionary *error = @{
|
|
20
|
+
@"code": @(code),
|
|
21
|
+
@"message": message
|
|
22
|
+
};
|
|
23
|
+
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:error];
|
|
24
|
+
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (void)sendErrorWithCode:(int)code message:(NSString *)message callbackId:(NSString *)callbackId keepCallback:(BOOL)keepCallback {
|
|
28
|
+
NSDictionary *error = @{
|
|
29
|
+
@"code": @(code),
|
|
30
|
+
@"message": message
|
|
31
|
+
};
|
|
32
|
+
CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:error];
|
|
33
|
+
[result setKeepCallbackAsBool:keepCallback];
|
|
34
|
+
[self.commandDelegate sendPluginResult:result callbackId:callbackId];
|
|
35
|
+
}
|
|
36
|
+
|
|
13
37
|
#pragma mark - Availability Check
|
|
14
38
|
|
|
15
39
|
- (void)isAvailable:(CDVInvokedUrlCommand *)command {
|
|
@@ -25,8 +49,7 @@
|
|
|
25
49
|
- (void)getReading:(CDVInvokedUrlCommand *)command {
|
|
26
50
|
[self.commandDelegate runInBackground:^{
|
|
27
51
|
if (!self.motionManager.magnetometerAvailable) {
|
|
28
|
-
|
|
29
|
-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
52
|
+
[self sendErrorWithCode:ERROR_NOT_AVAILABLE message:@"Magnetometer not available" callbackId:command.callbackId];
|
|
30
53
|
return;
|
|
31
54
|
}
|
|
32
55
|
|
|
@@ -66,8 +89,7 @@
|
|
|
66
89
|
- (void)getHeading:(CDVInvokedUrlCommand *)command {
|
|
67
90
|
[self.commandDelegate runInBackground:^{
|
|
68
91
|
if (![CLLocationManager headingAvailable]) {
|
|
69
|
-
|
|
70
|
-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
92
|
+
[self sendErrorWithCode:ERROR_NOT_AVAILABLE message:@"Heading not available" callbackId:command.callbackId];
|
|
71
93
|
return;
|
|
72
94
|
}
|
|
73
95
|
|
|
@@ -108,8 +130,7 @@
|
|
|
108
130
|
|
|
109
131
|
- (void)watchReadings:(CDVInvokedUrlCommand *)command {
|
|
110
132
|
if (!self.motionManager.deviceMotionAvailable) {
|
|
111
|
-
|
|
112
|
-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
133
|
+
[self sendErrorWithCode:ERROR_NOT_AVAILABLE message:@"Device motion not available" callbackId:command.callbackId];
|
|
113
134
|
return;
|
|
114
135
|
}
|
|
115
136
|
|
|
@@ -169,8 +190,7 @@
|
|
|
169
190
|
|
|
170
191
|
- (void)watchHeading:(CDVInvokedUrlCommand *)command {
|
|
171
192
|
if (![CLLocationManager headingAvailable]) {
|
|
172
|
-
|
|
173
|
-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
193
|
+
[self sendErrorWithCode:ERROR_NOT_AVAILABLE message:@"Heading not available" callbackId:command.callbackId];
|
|
174
194
|
return;
|
|
175
195
|
}
|
|
176
196
|
|
|
@@ -288,8 +308,7 @@
|
|
|
288
308
|
- (void)getFieldStrength:(CDVInvokedUrlCommand *)command {
|
|
289
309
|
[self.commandDelegate runInBackground:^{
|
|
290
310
|
if (!self.motionManager.magnetometerAvailable) {
|
|
291
|
-
|
|
292
|
-
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
|
311
|
+
[self sendErrorWithCode:ERROR_NOT_AVAILABLE message:@"Magnetometer not available" callbackId:command.callbackId];
|
|
293
312
|
return;
|
|
294
313
|
}
|
|
295
314
|
|