Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NdefInterface.cs
1using Android.Nfc;
2using Android.Nfc.Tech;
6using System.Text;
7using Waher.Content;
9
11{
17 public class NdefInterface(Tag Tag, Ndef Technology)
18 : NfcInterface(Tag, Technology), INdefInterface
19 {
20 private readonly Ndef ndef = Technology;
21
25 public Task<bool> CanMakeReadOnly()
26 {
27 return Task.FromResult(this.ndef.CanMakeReadOnly());
28 }
29
33 public Task<bool> IsWritable()
34 {
35 return Task.FromResult(this.ndef.IsWritable);
36 }
37
41 public Task<INdefRecord[]> GetMessage()
42 {
43 NdefMessage? Message = this.ndef.NdefMessage ?? throw UnableToReadDataFromDevice();
44 NdefRecord[]? Records = Message.GetRecords() ?? throw UnableToReadDataFromDevice();
45 List<INdefRecord> Result = [];
46
47 foreach (NdefRecord Record in Records)
48 {
49 switch (Record.Tnf)
50 {
51 case NdefRecord.TnfAbsoluteUri:
52 Result.Add(new UriRecord(Record));
53 break;
54
55 case NdefRecord.TnfMimeMedia:
56 Result.Add(new MimeTypeRecord(Record));
57 break;
58
59 case NdefRecord.TnfWellKnown:
60 byte[] Bin = Record.GetTypeInfo() ?? throw UnableToReadDataFromDevice();
61 string TypeInfo = Encoding.UTF8.GetString(Bin);
62
63 if (TypeInfo == "U")
64 Result.Add(new UriRecord(Record));
65 else
66 Result.Add(new WellKnownTypeRecord(Record));
67 break;
68
69 case NdefRecord.TnfExternalType:
70 Result.Add(new ExternalTypeRecord(Record));
71 break;
72
73 case NdefRecord.TnfEmpty:
74 case NdefRecord.TnfUnchanged:
75 case NdefRecord.TnfUnknown:
76 default:
77 break;
78 }
79 }
80
81 return Task.FromResult<INdefRecord[]>([.. Result]);
82 }
83
89 public async Task<bool> SetMessage(params object[] Items)
90 {
91 try
92 {
93 NdefMessage Message = await CreateMessage(Items);
94 await this.ndef.WriteNdefMessageAsync(Message);
95
96 return true;
97 }
98 catch (Exception)
99 {
100 return false;
101 }
102 }
103
110 public static async Task<NdefMessage> CreateMessage(params object[] Items)
111 {
112 List<NdefRecord> Records = [];
113
114 foreach (object Item in Items)
115 {
116 if (Item is null)
117 continue;
118
119 if (Item is Uri Uri)
120 {
121 if (Uri.IsAbsoluteUri)
122 Records.Add(NdefRecord.CreateUri(Uri.AbsoluteUri) ?? throw new ArgumentException("Unable to create URI record.", nameof(Items)));
123 else
124 throw new ArgumentException("URI not absolute.", nameof(Items));
125 }
126 else if (Item is string s)
127 Records.Add(NdefRecord.CreateTextRecord(null, s) ?? throw new ArgumentException("Unable to create text record.", nameof(Items)));
128 else
129 {
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)));
132 else
133 {
134 if (!InternetContent.Encodes(Item, out Grade _, out IContentEncoder Encoder))
135 throw new ArgumentException("Unable to encode objects of type " + Item.GetType().FullName + ".", nameof(Items));
136
137 ContentResponse Encoded = await Encoder.EncodeAsync(Item, Encoding.UTF8, null);
138 Encoded.AssertOk();
139
140 Records.Add(NdefRecord.CreateMime(Encoded.ContentType, Encoded.Encoded)
141 ?? throw new ArgumentException("Unable to create MIME record.", nameof(Items)));
142 }
143 }
144 }
145
146 return new(Records.ToArray());
147 }
148
149 }
150}
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
Definition: INdefRecord.cs:7
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Definition: ImplTypes.g.cs:58
Grade
Grade enumeration
Definition: Grade.cs:7