Use Seeeduino Arch(or Arch Pro) + NFC Shield to exchange NDEF messages with Android 4.0+

Dependencies:   PN532 mbed

main.cpp

Committer:
yihui
Date:
2013-11-21
Revision:
1:f228fdc41598
Parent:
0:21ac4fe1ccf8

File content as of revision 1:f228fdc41598:

#include "mbed.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"

#if 1                           // Using Seeeduino Arch
#define LOG(args...)
SPI spi(P1_22, P1_21, P1_20);  // SPI(mosi, miso, clk)
PN532_SPI pn532spi(spi, P0_2);

#else                           // Using Arch Pro
#define LOG(args...)        pc.printf(args)
Serial pc(USBTX, USBRX);
SPI spi(P0_18, P0_17, P0_15);
PN532_SPI pn532spi(spi, P0_6);
#endif

DigitalOut led(LED1);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

int main()
{
    while (1) {
#if 1
        LOG("Send a message to Android");
        NdefMessage message = NdefMessage();
        message.addUriRecord("http://www.seeedstudio.com");
        int messageSize = message.getEncodedSize();
        if (messageSize > sizeof(ndefBuf)) {
            LOG("ndefBuf is too small");
            while (1) {
            }
        } 
        message.encode(ndefBuf);
        if (0 >= nfc.write(ndefBuf, messageSize)) {
            LOG("Failed\n");
        } else {
            LOG("Success\n");
        }
#else
        LOG("Get a message from Android");
        int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
        if (msgSize > 0) {
            NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
            msg.print();
            LOG("\nSuccess");
        } else {
            LOG("failed");
        }
#endif
        wait(5);
    }
}