Git Product home page Git Product logo

ndef-js's Introduction

Library to create and parse NDEF messages.

ndef = require('ndef');

message = [
    ndef.textRecord("hello, world")
];

bytes = ndef.encodeMessage(message);

// do something useful with bytes: write to a tag or send to a peer
  
records = ndef.decodeMessage(bytes);

ndef.text.decodePayload(records[0].payload);

// prints 'hello, world'

See the examples directory and the mifare classic examples for more information on creating and decoding messages.

See the phonegap-nfc documentation for additional info.

ndef-js's People

Contributors

don avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ndef-js's Issues

.slice, .unshift is not a function when using with mifare-classic examples

Set-Up Info:
Beaglebone Black Rev C running debian jessie
PN532 Elechouse V3 Module
iojs v2.2.1
ndef-js
mifare-classic-js
libnfc v1.7.1
libfreefare 0.4.0

Code I use to write the tag:

#!/usr/bin/env node

var ndef = require('ndef'),
    mifare = require('..'),
    message,
    bytes;

message = [
    ndef.textRecord("Hello from nodejs")
];

bytes = ndef.encodeMessage(message);

mifare.write(bytes, function(err) {
    if (err) {
        console.log("Write failed ");
        console.log(err);
    } else {
        console.log("OK");
    }
});

Code I use to read the tag:

var ndef = require('ndef'),
    mifare = require('..');

mifare.read(function(err, buffer) {
    if (err) {
        console.log("Read failed ");
        console.log(err);
    } else {
        // TODO handle empty buffer!
        console.log(buffer.toJSON());
        var message = ndef.decodeMessage(buffer.toJSON());
        console.log("Found NDEF message with " + message.length +
            (message.length === 1 ? " record" : " records" ));
        console.log(ndef.stringify(message));
    }
})

I am getting this error when trying to use this library with mifare-classic which you wrote also.

I read up about what .slice and .unshift do and it seems to work on arrays but instead the value returned is an object so i modified your code to

ndef.js:276

var bytes = bytes.data.slice(0),

ndef-uri.js:41

encoded.data.unshift(protolCode);

ndef-text.js:8

languageCode = data.data.slice(1, 1 + languageCodeLength),

ndef-text.js:14

return util.bytesToString(data.data.slice(languageCodeLength + 1));

ndef-text.js:25

encoded.data.unshift(lang.length);

no errors appear but nothing happens and the mifare classic card doesn't appear to get written at all.

I just get this:

Found Mifare Classic 1k with UID 07cf36f2. 
NFC Forum application contains a "NDEF Message TLV".

 Record

Even when i tried to log the raw data to understand what was going on, it showed me this:

Found Mifare Classic 1k with UID 07cf36f2. 
NFC Forum application contains a "NDEF Message TLV".
{ type: 'Buffer', data: [ 193, 0, 0, 0, 0, 0, 0, 0 ] }
Found NDEF message with 1 record
 Record

Which is weird because it seems to be missing some data as I think it's supposed to look like

This is the encoded Data 
[ 193,
  undefined,
  0,
  0,
  0,
  0,
  { type: 'Buffer', data: [ 84 ] },
  { type: 'Buffer',
    data: 
     [ 2,
       101,
       110,
       72,
       101,
       108,
       108,
       111,
       32,
       102,
       114,
       111,
       109,
       32,
       110,
       111,
       100,
       101,
       106,
       115 ] } ]
NDEF file is 8 bytes long.
Found Mifare Classic 1k with UID 07cf36f2. 
OK

instead.

The card also reads as empty on android.

I tried to follow and log all the return values and it seems like the return values are definitely correct. But it's just not writing to the card which is what I don't understand.

I messaged you on twitter about this also.

.slice is not a function

I have got problem similiar to issue#4. I have now newest version of Node.JS (v.8.2.1) and I am running everything on Linux.
When I was trying to run of examples (readTag.js) from this library (and also mifare-classic examples) I got error:
`WARNING: End of message does not look correct. Expecting 0xFE but got 110
<Buffer d1 01 08 54 02 65 6e 48 65 6c 6c 6f>
/home/pi/magisterka/ndef-js/lib/ndef.js:276
var bytes = bytes.slice(0), // clone since parsing is destructive
^

TypeError: bytes.slice is not a function
at Object.decodeMessage (/home/pi/magisterka/ndef-js/lib/ndef.js:276:27)
at printNdefInfo (/home/pi/magisterka/ndef-js/examples/readTag.js:16:20)
at ChildProcess. (/home/pi/magisterka/ndef-js/examples/readTag.js:53:9)
at emitTwo (events.js:125:13)
at ChildProcess.emit (events.js:213:7)
at maybeClose (internal/child_process.js:921:16)
at Socket.stream.socket.on (internal/child_process.js:348:11)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at Pipe._handle.close [as _onclose] (net.js:549:12)
`

I dont know what should I change to support this version of Node?

Prefix or not?

Is it given that there will always be a prefix, considering this code always slices off the first character in the byte array when parsing, or is it known whether that will be the case beforehand?

I did this as a fail-safe. This is not the exact same context, but you see what I do anyway. Sorry for no use of let etc.

    var payload = "";
    var first = record.payload[0];
    if (first < 32) {
        payload = nfcProtocols[first] + nfc.bytesToString(record.payload.slice(1));
    } else {
        payload = nfc.bytesToString(record.payload);
    }

i also noted there are more than 32 elements in the array.

Writing to tags

Excellent work - I like this project. I have only looked at it for around ten minutes so my questions might sound daft..

What I'm wondering though - is if I were to write to a card - could I just write the buffer object somewhere or are there other objects that are abstracted away? It's not clear to me what the different objects are - could you point me in the right direction:

Say that I use the readTags-example and want to use the code from uri/text to write to a tag - which structure should I use to do so? I will look more at this and see if I figure it out - but the question is - is there a method to write that does what read does? What would be really cool is to be able to "clone" - reading a tag and then writing a modified version the contents of it to a new tag. Not sure how to implement that in this though. But just the prospect of not having to bit bang but instead use js is kinda awesome. Keep up the good work! :)

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.