Skip to content

Commit 3b8566d

Browse files
kitcarson88Simone Colazzo
andauthored
feat(magnetometer): add magnetometer wrapper (#3887)
* feat(magnetometer): add magnetometer wrapper * fix(magnetomer): add wrapper parameters Co-authored-by: Simone Colazzo <[email protected]>
1 parent a04a70e commit 3b8566d

File tree

1 file changed

+87
-0
lines changed
  • src/@awesome-cordova-plugins/plugins/magnetometer

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Injectable } from '@angular/core';
2+
import { Plugin, Cordova, AwesomeCordovaNativePlugin } from '@awesome-cordova-plugins/core';
3+
import { Observable } from 'rxjs';
4+
5+
export interface MagnetometerReading {
6+
/**
7+
* X reading of magnetometer. (Number)
8+
*/
9+
x: number;
10+
/**
11+
* Y reading of magnetometer. (Number)
12+
*/
13+
y: number;
14+
/**
15+
* Z reading of magnetometer. (Number)
16+
*/
17+
z: number;
18+
/**
19+
* Calculated total - always positive of magnetometer. (Number)
20+
*/
21+
magnitude: number;
22+
}
23+
24+
/**
25+
* @name Device eMagnetometer
26+
* @description
27+
* Requires Cordova plugin: `cordova-plugin-magnetometer`. For more info, please see the [Device Orientation docs](https://github.com/sdesalas/cordova-plugin-magnetometer).
28+
*
29+
* @usage
30+
* ```typescript
31+
* // MagnetometerReading is an interface for compass
32+
* import { Magnetometer, MagnetometerReading } from '@awesome-cordova-plugins/device-orientation/ngx';
33+
*
34+
* constructor(private magnetometer: Magnetometer) { }
35+
*
36+
* ...
37+
*
38+
* // Get the device current compass heading
39+
* this.magnetometer.getReading().then(
40+
* (data: MagnetometerReading) => console.log(data),
41+
* (error: any) => console.log(error)
42+
* );
43+
*
44+
* // Watch the device compass heading change
45+
* var subscription = this.magnetometer.watchReadings().subscribe(
46+
* (data: MagnetometerReading) => console.log(data)
47+
* );
48+
*
49+
* // Stop watching heading change
50+
* subscription.unsubscribe();
51+
* ```
52+
* @interfaces
53+
* MagnetometerReading
54+
*/
55+
@Plugin({
56+
pluginName: 'Magnetometer',
57+
plugin: 'cordova-plugin-magnetometer',
58+
pluginRef: 'cordova.plugins.magnetometer',
59+
repo: 'https://github.com/sdesalas/cordova-plugin-magnetometer',
60+
platforms: ['Android', 'iOS'],
61+
})
62+
@Injectable()
63+
export class Magnetometer extends AwesomeCordovaNativePlugin {
64+
/**
65+
* Get the current compass reading.
66+
* @returns {Promise<MagnetometerReading>}
67+
*/
68+
@Cordova()
69+
getReading(): Promise<MagnetometerReading> {
70+
return;
71+
}
72+
/**
73+
* Get the device current heading at a regular interval
74+
*
75+
* Stop the watch by unsubscribing from the observable
76+
* @param {DeviceOrientationCompassOptions} [options] Options for compass. Frequency and Filter. Optional
77+
* @returns {Observable<DeviceOrientationCompassHeading>} Returns an observable that contains the compass heading
78+
*/
79+
@Cordova({
80+
callbackOrder: 'reverse',
81+
observable: true,
82+
clearFunction: 'stop',
83+
})
84+
watchReadings(): Observable<MagnetometerReading> {
85+
return;
86+
}
87+
}

0 commit comments

Comments
 (0)