Reputation: 1
Apparently my app does not recognize the NFC plugin or it does not know how to install it correctly.
I'm trying to make an app with angular Ionic + capacitor. One of the components must be able to read NFC messages.
Since I am a student and I have no previous experience working with Ionic and my experience with Angular is very limited, I will try to comment step by step on what I did.
Searching the internet I found this: https://www.npmjs.com/package/@awesome-cordova-plugins/nfc
so I did the following:
npm install @awesome-cordova-plugins/nfc
npm install @awesome-cordova-plugins/core
and in NfcReaderComponent I try the following:
import { NFC } from '@awesome-cordova-plugins/nfc/ngx';
...
constructor(
private nfc: NFC, <--
private ticketService: TicketService,
private transportService: TransportService,
private router: Router
) { }
...
Now when I try to open said component this error appears:
ERROR NullInjectorError: NullInjectorError: No provider for [object Object]!
at NullInjector.get (core.mjs:1660:27)
at R3Injector.get (core.mjs:3106:33)
at R3Injector.get (core.mjs:3106:33)
at R3Injector.get (core.mjs:3106:33)
at ChainedInjector.get (core.mjs:16754:36)
at lookupTokenUsingModuleInjector (core.mjs:5747:39)
at getOrCreateInjectable(core.mjs:5795:12)
at Module.ɵɵdirectiveInject (core.mjs:11331:19)
at NodeInjectorFactory.NfcReaderComponent_Factory [as factory] (nfc-reader.component.ts:25:32)
at getNodeInjectable (core.mjs:6007:44)
i also get this error on android studio: plugin_not_installed
link to the repo: https://github.com/ManuSureda/SUBD-T
I think I'm not installing or provisioning the plugin correctly, but I don't really understand how to fix this.
Update:
Apparently I needed to install phonegap-nfc too
npm i phonegap-nfc
now it lets me inject NFC into app.module.ts
...
import { NFC } from '@awesome-cordova-plugins/nfc/ngx';
@NgModule({
declarations: [AppComponent],
imports: [...],
providers: [
...
NFC
],
bootstrap: [AppComponent],
})
export class AppModule {}
with 'ionic serve' seems to work just fine but i share this warning just in case that it matters: Native: tried calling NFC.enabled, but Cordova is not available. Make sure to include cordova.js or run in a device/simulator
now the new problem is:
when i try to run the app through android studio i got this error:
FAILURE: Build failed with an exception.
Compilation failed; see the compiler error output for details.
Run with --info option to get more log output. Run with --scan to get full insights.
Task :capacitor-cordova-android-plugins:compileDebugJavaWithJavac FAILED C:\Users\Manuel\Desktop\Seminario-UTN\Frontend\SUBD-transport\android\capacitor-cordova-android-plugins\src\main\java\com\chariotsolutions\nfc\plugin\NfcPlugin.java:550: error: cannot find symbol nfcAdapter.setNdefPushMessage(p2pMessage, getActivity()); ^ symbol: method setNdefPushMessage(NdefMessage,Activity) location: variable nfcAdapter of type NfcAdapter
Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: C:\Users\Manuel\Desktop\Seminario-UTN\Frontend\SUBD-transport\android\capacitor-cordova-android-plugins\src\main\java\cordova\plugins\Diagnostic.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 9 errors
Upvotes: 0
Views: 308
Reputation: 1
The calls that the package makes on Android are all deprecated as of Android 10. I'm currently looking for a package that's available (and free) to use myself. I have found one that you need to subscribe to here https://github.com/capawesome-team/capacitor-plugins/tree/main/packages/nfc if that is an option for you.
UPDATE: The issue is with phonegap-nfc. I have found another repo here: https://github.com/EYALIN/community-cordova-plugin-nfc that is under active development and works to scan NFC tags on Android 14 without any error/warnings appearing in logcat
Upvotes: 0
Reputation: 178
Did you add the provider like that in app.module.ts ? 😉
@NgModule({
imports: [
BrowserModule,
...
],
declarations: [ AppComponent ],
providers: [ NFC ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
Upvotes: 0