feat(mobile): add NFC plugin (#830)

* feat: scaffold NFC plugin, initial iOS code

* adjust script paths (api example)

* update entitlements & plist

* update class name

* update api

* sketch api, remove desktop

* update response data

* add write fn

* remove commands

* fixes for write mode

* check nfc state before using the APIs

* fix(example): downgrade internal-ip to v7

* feat: typed iOS arguments

* update swift requirement

* android updates

* update tauri

* fix icon

* update example

* fix build

* fix notification example

* fix clipboard

* fix ios notification build

* fix info.plist

* update tauri

* add change file

* fmt

* update to new args class syntax :( [skip ci]

* add lang code handling in RTD_TEXT helper (written payload is broken without it)

* update nfc to latest tauri. use tauri from git

* update lockfile

* android: add initial nfc writer implementation

* check sdk version for pendingintent flag

* quicksaving basic ndef reading that doesn't crash

* add basic ndef reading (android)

* small cleanup

* change pending action type

* validate available state

* gradle 8.0.0

* use session class

* implement keep session alive

* fix notification crash??

* remove dox feature, fix breaking changes

* update dependencies

* fix shell tests [skip ci]

* fmt [skip ci]

* type safe args

* scan kind options

* commit .idea files

* update api

* update example

* fix app check

* alertmessage options for iOS

* default to tag on example

* fix(ios): always close session on write, remove keepsessionalive option

* add kind input to write options

* empty records if message not found

* fill tag metadata for ndef read

* add contributors

* update vite

* covector setup

* tauri/dox removed

* docs and examples

* fmt [skip ci]

---------

Co-authored-by: FabianLars-crabnebula <fabianlars@crabnebula.dev>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Lucas Nogueira
2023-12-19 10:50:31 -03:00
committed by GitHub
parent 7c59e3785b
commit fe79adb5c7
53 changed files with 2586 additions and 370 deletions
+7 -1
View File
@@ -20,6 +20,7 @@
import { onMount } from "svelte";
import { ask } from "@tauri-apps/plugin-dialog";
import Nfc from "./views/Nfc.svelte";
const appWindow = getCurrent();
@@ -107,6 +108,11 @@
component: Scanner,
icon: "i-ph-scan",
},
isMobile && {
label: "NFC",
component: Nfc,
icon: "i-ph-nfc",
},
];
let selected = views[0];
@@ -221,7 +227,7 @@
let isWindows;
onMount(async () => {
isWindows = (await os.platform()) === "win32";
isWindows = (await os.platform()) === "windows";
});
// mobile
+126
View File
@@ -0,0 +1,126 @@
<script>
import { onMount } from "svelte";
import { write, scan, textRecord, uriRecord } from "@tauri-apps/plugin-nfc";
import * as os from "@tauri-apps/plugin-os";
export let onMessage;
const decoder = new TextDecoder();
let kind = "tag";
let writeToNfc = false;
let text = "";
let uri = "";
let scheme = "";
let host = "";
let pathPrefix = "";
let mimeType = "";
let isAndroid;
onMount(async () => {
isAndroid = (await os.platform()) === "android";
});
async function _readNfc() {
onMessage(`NFC scanning ${kind}`);
const tagResponse = await scan(
{
type: kind,
uri: {
scheme: scheme || null,
host: host || null,
pathPrefix: pathPrefix || null,
},
mimeType: mimeType || null,
},
{
keepSessionAlive: writeToNfc,
message: "Hold your iPhone near an NFC tag",
successMessage: "Tag successfully read",
}
);
onMessage({
id: decoder.decode(new Uint8Array(tagResponse.id)),
kind: tagResponse.kind,
records: tagResponse.records.map((record) => ({
id: decoder.decode(new Uint8Array(record.id)),
kind: decoder.decode(new Uint8Array(record.kind)),
payload: decoder.decode(new Uint8Array(record.payload)),
tnf: record.tnf,
})),
});
if (writeToNfc) {
const records = [];
if (text) {
records.push(textRecord(text, "tauriTextId"));
}
if (uri) {
records.push(uriRecord(uri, "tauriUriId"));
}
await write(records, {
successMessage: "Data written to tag",
});
onMessage("Wrote to tag");
}
}
function readNfc() {
_readNfc().catch(onMessage);
}
</script>
<div>
<div class="flex gap-2 children:grow items-center">
<div>
<input type="checkbox" id="nfc-write" bind:checked={writeToNfc} />
<label for="nfc-write">Write data</label>
</div>
<select class="input" id="request-method" bind:value={kind}>
<option value="tag">TAG</option>
<option value="ndef">NDEF</option>
</select>
</div>
{#if isAndroid}
<div class="flex flex-col gap-2 children:grow">
<p>Filters</p>
<div class="flex gap-2">
<input
class="input"
placeholder="Scheme"
style="width: 33%"
bind:value={scheme}
/>
<input
class="input"
placeholder="Host"
style="width: 33%"
bind:value={host}
/>
<input
class="input"
placeholder="Path prefix"
style="width: 33%"
bind:value={pathPrefix}
/>
</div>
<div class="flex gap-2">
<input class="input" placeholder="Mime type" bind:value={mimeType} />
</div>
</div>
{/if}
<div class="flex flex-col gap-2 children:grow">
<p>Write Records</p>
<div class="flex">
<input class="input" placeholder="Text record" bind:value={text} />
<input class="input" placeholder="URI record" bind:value={uri} />
</div>
</div>
<button class="btn" on:click={readNfc}>Scan</button>
</div>