Skip to content

Instantly share code, notes, and snippets.

@tmyt
Created September 6, 2015 17:10
Show Gist options
  • Save tmyt/5e3268fda15c61bf4c62 to your computer and use it in GitHub Desktop.
Save tmyt/5e3268fda15c61bf4c62 to your computer and use it in GitHub Desktop.
WinRTでNDEF書いてみる感じ
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using App18;
namespace Ndef
{
public class NdefRecord
{
public bool MB { get; set; }
public bool ME { get; set; }
public bool CF { get; set; }
public bool SR { get; set; }
public bool IL { get; set; }
public byte TNF { get; set; }
public byte[] Type { get; set; }
public byte[] Id { get; set; }
public byte[] Payload { get; set; }
public NdefRecord()
{
}
public NdefRecord(byte tnf, string type, string payload)
{
TNF = tnf;
Type = Encoding.UTF8.GetBytes(type);
Payload = Encoding.UTF8.GetBytes(payload);
}
public NdefRecord(byte tnf, string type, byte[] payload)
{
TNF = tnf;
Type = Encoding.UTF8.GetBytes(type);
Payload = payload;
}
public byte[] Build()
{
// update flag
SR = Payload.Length <= 255;
// build content
using (var mem = new MemoryStream())
{
mem.WriteByte(
(byte)((MB ? 0x80 : 0) |
(ME ? 0x40 : 0) |
(CF ? 0x20 : 0) |
(SR ? 0x10 : 0) |
(IL ? 0x08 : 0) |
(TNF & 0x7))
);
mem.WriteByte((byte)Type.Length);
if (SR)
{
mem.WriteByte((byte)(Payload.Length & 0xff));
}
else
{
var l = (ulong)Payload.Length;
mem.WriteByte((byte)((l & 0xff000000) >> 24));
mem.WriteByte((byte)((l & 0xff0000) >> 16));
mem.WriteByte((byte)((l & 0xff00) >> 8));
mem.WriteByte((byte)((l & 0xff) >> 0));
}
if (IL) mem.WriteByte((byte)(Id.Length & 0xff));
if (Type.Length > 0) mem.Write(Type, 0, Type.Length);
if (IL && Id.Length > 0) mem.Write(Id, 0, Id.Length);
if (Payload.Length > 0) mem.Write(Payload, 0, Payload.Length);
return mem.ToArray();
}
}
}
public class NdefMessage : IEnumerable<NdefRecord>
{
private List<NdefRecord> _records;
public NdefMessage()
{
_records = new List<NdefRecord>();
}
public void Add(NdefRecord record)
{
_records.Add(record);
}
public void Add(params NdefRecord[] records)
{
_records.AddRange(records);
}
public byte[] Build()
{
using(var mem = new MemoryStream())
{
for (var i = 0; i < _records.Count; ++i)
{
_records[i].MB = i == 0;
_records[i].ME = i + 1 == _records.Count;
var bytes = _records[i].Build();
mem.Write(bytes, 0, bytes.Length);
}
return mem.ToArray();
}
}
public IEnumerator<NdefRecord> GetEnumerator()
{
return _records.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Proximity;
using Ndef;
namespace App18
{
class Sample
{
private void WriteNdef()
{
// Build
var ndef = new NdefMessage
{
new NdefRecord(0x02, "text/plain", "ひとつめ"),
new NdefRecord(0x02, "text/plain", "ふたつめ"),
new NdefRecord(0x04, "android.com:pkg", "com.android.settings")
};
// Publish
var device = ProximityDevice.GetDefault();
var publishId = device.PublishBinaryMessage("NDEF:WriteTag", ndef.Build().AsBuffer(), (provider, msg) =>
{
provider.StopPublishingMessage(msg);
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment