File size: 3,161 Bytes
2d8be8f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | <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>
|