This program demonstrates how to use a MicroNFCBoard as a peripheral to read and decode NFC tags.

Dependencies:   MicroNFCBoardAPI mbed

Fork of MicroNFCBoardAPI_Blink by AppNearMe Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 MicroNFCBoard mbed API
00003 
00004 Copyright (c) 2014-2015 AppNearMe Ltd
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010 http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017  */
00018 
00019 #include "mbed.h"
00020 #include "micronfcboard.h"
00021 
00022 MicroNFCBoard nfc(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, D9);
00023 
00024 int main() {
00025     nfc.init();
00026 
00027     //Start polling
00028     while(true)
00029     {
00030         printf("Poll\r\n");
00031         nfc.startPolling(true, false, false);
00032 
00033         while( nfc.polling() );
00034 
00035         if( nfc.type2Tag() )
00036         {
00037             printf("Connected to type 2 tag\r\n");
00038         }
00039         else
00040         {
00041           continue;
00042         }
00043 
00044         bool ndefRead = false;
00045         bool ndefReadingStarted = false;
00046         while( nfc.connected() )
00047         {
00048             if( !ndefReadingStarted && nfc.ndefReadable() )
00049             {
00050               printf("Reading tag\r\n");
00051               ndefReadingStarted = true;
00052               nfc.ndefRead();
00053             }
00054             if( !ndefRead && nfc.ndefPresent() )
00055             {
00056                 printf("Got message\r\n");
00057                 char buf[512];
00058                 if( nfc.readNdefUri(buf, sizeof(buf)) )
00059                 {
00060                   printf("Got URI: %s\r\n", buf);
00061                 }
00062                 if( nfc.readNdefText(buf, sizeof(buf)) )
00063                 {
00064                   printf("Got Text: %s\r\n", buf);
00065                 }
00066                 ndefRead = true;
00067             }
00068         }
00069 
00070         printf("Disconnected\r\n");
00071     }
00072 }