GithubHelp home page GithubHelp logo

nfcim / ndef Goto Github PK

View Code? Open in Web Editor NEW
11.0 6.0 7.0 224 KB

A Dart library to decode & encode NDEF records, supporting multiple types.

Home Page: https://pub.dev/packages/ndef

License: MIT License

Dart 100.00%
dart dart-library dart-package flutter nfc ndef ndef-library ndef-message ndef-formatting ndef-record

ndef's Introduction

ndef

pub version Test

ndef is a Dart library to decode & encode NDEF records, supporting multiple types including (grouped by Type Name Format):

  • NFC Well-known Records (TNF 1 / urn:nfc:wkt:), with:
    • Text (class T)
    • URI with well-known prefix (class U)
    • Digital signature (class Sig)
    • Smart poster (class Sp), including sub-record:
      • Action (class act)
      • Size (class s)
      • Type (class t)
    • Connection handover (class Hr/Hs/Hm/Hi/Hc/ac/cr)
  • Media Records (TNF 2, containing MIME data), with:
    • Bluetooth easy pairing (class application/vnd.bluetooth.ep.oob)
    • Bluetooth low energy (class application/vnd.bluetooth.le.oob)
  • Absolute URI Records (TNF 3)
  • External Records (TNF 4 / urn:nfc:ext:), with:
    • Android application record (class android.com:pkg)

This library is still under active development and subject to breaking API changes and malfunction. Pull requests and issues are most welcomed, especially on:

  • Bug fixes
  • New support for other record types

Usage

import 'package:ndef/ndef.dart' as ndef;

// encoding
var uriRecord = new ndef.UriRecord.fromString("https://github.com/nfcim/ndef");
var textRecord = new ndef.TextRecord(text: "Hello");
var encodedUriRecord = uriRecord.encode().toHexString(); /// encode a single record, and use our extension method on [Uint8List]
var encodedAllRecords = ndef.encodeNdefMessage([uriRecord, textRecord]).toHexString(); // encode several records as a message

// decoding
var encodedTextRecord = "d1010f5402656e48656c6c6f20576f726c6421";
var decodedRecords = ndef.decodeRawNdefMessage(encodedTextRecord.toBytes());
assert(decodedRecords.length == 1);
if (decodedRecords[0] is ndef.TextRecord) {
  assert(decodeRecords[0].text == "Hello");
}  else {
  // we will not reach here
}

// data-binding (by implementing payload as dynamic getter / setter)
var origPayload = uriRecord.payload!;
print(origPayload.toHexString());
uriRecord.content = "github.com/nfcim/flutter_nfc_kit";
print(uriRecord.payload!.toHexString()); // changed
uriRecord.payload = origPayload;
print(uriRecord.content); // changed back

// decoding by providing parts of a record
var partiallyDecodedUrlRecord = ndef.decodePartialNdefMessage(ndef.TypeNameFormat.nfcWellKnown, utf8.encode("U"), origPayload, id: Uint8List.fromList([0x1, 0x2]));

See example code for a more complete example.

Refer to the documentation for detailed usage.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.