Reputation: 1
NullInjectorError: No provider for Magnetometer My goal is to read sensordata, like magnetometer. Using Ionic 5, Angular, capacitor android.
You can find my project at https://github.com/kpproce/ionicMagnoMeter
I can't get access to the magnetometer, because the object is empty. I did manage to access the camera, but this is standard available in the web version also...
I understand that the magnetometer probably doesn't exist in the web app version. There are many instructions about plugins, but could not get them working
followed instructions: https://ionicframework.com/docs/native/magnetometer
To prepare: In the terminal VScode:
--- npm install --save @ionic-native/core
--- npm install cordova-plugin-magnetometer
--- npm install @ionic-native/magnetometer
--- npx cap add android
Code used:
import { Component } from '@angular/core';
import { Plugins } from '@capacitor/core';
import { Magnetometer, MagnetometerReading } from '@ionic-native/Magnetometer/ngx';
...
export class HomePage {
constructor(private magnetometer: Magnetometer) {} // this seems to be the problem, empty object
see error NullInjectorError: No provider for Magnetometer!
Upvotes: 0
Views: 595
Reputation: 11
See: https://forum.ionicframework.com/t/getting-error-no-provider-for-mediaplugin/53570/5
Usually you add ionic native plugins like that: add the plugin to your app with cli command
ionic plugin add --save PLUGINNAME
install the ionic native plugin from npm
npm install --save @ionic-native/PLUGINNAME
import it in your page/provider and in your app.module.ts
import { PLUGINNAME } from '@ionic-native/PLUGINNAME';
add it to your app.module.ts
providers: [ PLUGINNAME ]
use DI in your constructor in your page/provider
constructor(private pluginName: PLUGINNAME) { }
then you can use it in your code
Upvotes: 0