17 public class NdefInterface(Tag Tag, Ndef Technology)
20 private readonly Ndef ndef = Technology;
25 public Task<bool> CanMakeReadOnly()
27 return Task.FromResult(this.ndef.CanMakeReadOnly());
33 public Task<bool> IsWritable()
35 return Task.FromResult(this.ndef.IsWritable);
41 public Task<INdefRecord[]> GetMessage()
43 NdefMessage? Message = this.ndef.NdefMessage ??
throw UnableToReadDataFromDevice();
44 NdefRecord[]? Records = Message.GetRecords() ??
throw UnableToReadDataFromDevice();
45 List<INdefRecord> Result = [];
47 foreach (NdefRecord Record
in Records)
51 case NdefRecord.TnfAbsoluteUri:
52 Result.Add(
new UriRecord(Record));
55 case NdefRecord.TnfMimeMedia:
56 Result.Add(
new MimeTypeRecord(Record));
59 case NdefRecord.TnfWellKnown:
60 byte[] Bin = Record.GetTypeInfo() ??
throw UnableToReadDataFromDevice();
61 string TypeInfo = Encoding.UTF8.GetString(Bin);
64 Result.Add(
new UriRecord(Record));
66 Result.Add(
new WellKnownTypeRecord(Record));
69 case NdefRecord.TnfExternalType:
70 Result.Add(
new ExternalTypeRecord(Record));
73 case NdefRecord.TnfEmpty:
74 case NdefRecord.TnfUnchanged:
75 case NdefRecord.TnfUnknown:
89 public async Task<bool> SetMessage(params
object[] Items)
93 NdefMessage Message = await CreateMessage(Items);
94 await this.ndef.WriteNdefMessageAsync(Message);
110 public static async Task<NdefMessage> CreateMessage(params
object[] Items)
112 List<NdefRecord> Records = [];
114 foreach (
object Item
in Items)
121 if (Uri.IsAbsoluteUri)
122 Records.Add(NdefRecord.CreateUri(Uri.AbsoluteUri) ??
throw new ArgumentException(
"Unable to create URI record.", nameof(Items)));
124 throw new ArgumentException(
"URI not absolute.", nameof(Items));
126 else if (Item is
string s)
127 Records.Add(NdefRecord.CreateTextRecord(
null, s) ??
throw new ArgumentException(
"Unable to create text record.", nameof(Items)));
130 if (Item is KeyValuePair<
byte[],
string> Mime)
131 Records.Add(NdefRecord.CreateMime(Mime.Value, Mime.Key) ??
throw new ArgumentException(
"Unable to create MIME record.", nameof(Items)));
135 throw new ArgumentException(
"Unable to encode objects of type " + Item.GetType().FullName +
".", nameof(Items));
137 ContentResponse Encoded = await Encoder.EncodeAsync(Item, Encoding.UTF8,
null);
141 ??
throw new ArgumentException(
"Unable to create MIME record.", nameof(Items)));
146 return new(Records.ToArray());
Contains information about a response to a content request.
byte[] Encoded
Encoded object.
string ContentType
Internet Content-Type of encoded object.
void AssertOk()
Asserts response is OK.
Static class managing encoding and decoding of internet content.
static bool Encodes(object Object, out Grade Grade, out IContentEncoder Encoder, params string[] AcceptedContentTypes)
If a given object can be encoded.
NDEF interface, for communication with an NFC Tag.
Interface for NDEF records
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...