-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: NFC support for canteen cards to query the balance (#48)
* feat: NFC support for canteen cards to query the balance * fix tests * fixes * revert changes * wip * fixes
- Loading branch information
Showing
32 changed files
with
557 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
save-exact=true | ||
|
||
@capawesome-team:registry=https://npm.pkg.github.com |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>com.apple.developer.nfc.readersession.formats</key> | ||
<array> | ||
<string>TAG</string> | ||
</array> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Injectable, NgZone } from '@angular/core'; | ||
import { Capacitor } from '@capacitor/core'; | ||
import { | ||
ConnectOptions, | ||
Nfc, | ||
NfcTag, | ||
PollingOption, | ||
TransceiveOptions, | ||
TransceiveResult, | ||
} from '@capawesome-team/capacitor-nfc'; | ||
import { Observable, Subject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CapacitorNfcService { | ||
private readonly scannedTagSubject = new Subject<NfcTag>(); | ||
private readonly sessionCanceledSubject = new Subject<void>(); | ||
|
||
constructor(private readonly ngZone: NgZone) { | ||
void Nfc.removeAllListeners().then(() => { | ||
void Nfc.addListener('nfcTagScanned', event => { | ||
this.ngZone.run(() => { | ||
this.scannedTagSubject.next(event.nfcTag); | ||
}); | ||
}); | ||
void Nfc.addListener('scanSessionCanceled', () => { | ||
this.ngZone.run(() => { | ||
this.sessionCanceledSubject.next(); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
public get scannedTag$(): Observable<NfcTag> { | ||
return this.scannedTagSubject.asObservable(); | ||
} | ||
|
||
public get sessionCanceled$(): Observable<void> { | ||
return this.sessionCanceledSubject.asObservable(); | ||
} | ||
|
||
public async startScanSession(): Promise<void> { | ||
await Nfc.startScanSession({ | ||
pollingOptions: [PollingOption.iso14443, PollingOption.iso15693], | ||
}); | ||
} | ||
|
||
public async stopScanSession(): Promise<void> { | ||
await Nfc.stopScanSession(); | ||
} | ||
|
||
public transceive(options: TransceiveOptions): Promise<TransceiveResult> { | ||
return Nfc.transceive(options); | ||
} | ||
|
||
public async connect(options: ConnectOptions): Promise<void> { | ||
const isAndroid = Capacitor.getPlatform() === 'android'; | ||
if (isAndroid) { | ||
await Nfc.connect(options); | ||
} | ||
} | ||
|
||
public async close(): Promise<void> { | ||
const isAndroid = Capacitor.getPlatform() === 'android'; | ||
if (isAndroid) { | ||
await Nfc.close(); | ||
} | ||
} | ||
|
||
public async isSupported(): Promise<boolean> { | ||
const { isSupported } = await Nfc.isSupported(); | ||
return isSupported; | ||
} | ||
|
||
public async isEnabled(): Promise<boolean> { | ||
const isAndroid = Capacitor.getPlatform() === 'android'; | ||
if (!isAndroid) { | ||
return true; | ||
} | ||
const { isEnabled } = await Nfc.isEnabled(); | ||
return isEnabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './capacitor-app/capacitor-app.service'; | ||
export * from './capacitor-nfc/capacitor-nfc.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './nfc-helper/nfc-helper.service'; | ||
export * from './nfc/nfc.service'; |
23 changes: 23 additions & 0 deletions
23
src/app/core/services/nfc/nfc-helper/nfc-helper.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NfcUtils } from '@capawesome-team/capacitor-nfc'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class NfcHelperService { | ||
private readonly nfcUtilsInstance = new NfcUtils(); | ||
|
||
constructor() {} | ||
|
||
public convertHexToBytes(hex: string): number[] { | ||
return this.nfcUtilsInstance.convertHexToBytes({ hex }).bytes; | ||
} | ||
|
||
public convertBytesToHex(bytes: number[]): string { | ||
return this.nfcUtilsInstance.convertBytesToHex({ bytes }).hex; | ||
} | ||
|
||
public convertHexToNumber(hex: string): number { | ||
return this.nfcUtilsInstance.convertHexToNumber({ hex }).number; | ||
} | ||
} |
Oops, something went wrong.