This program demonstrates how to use a MicroNFCBoard as a peripheral to send a message to a phone/tablet using P2P/SNEP/Android Beam.

Dependencies:   MicroNFCBoardAPI mbed

Fork of MicroNFCBoardAPI_P2P_Client by AppNearMe Official

Committer:
AppNearMe
Date:
Fri Apr 24 13:01:12 2015 +0000
Revision:
0:e6bbb5a92267
Child:
1:5b804c0c8aa8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AppNearMe 0:e6bbb5a92267 1 /*
AppNearMe 0:e6bbb5a92267 2 MicroNFCBoard mbed API
AppNearMe 0:e6bbb5a92267 3
AppNearMe 0:e6bbb5a92267 4 Copyright (c) 2014-2015 AppNearMe Ltd
AppNearMe 0:e6bbb5a92267 5
AppNearMe 0:e6bbb5a92267 6 Licensed under the Apache License, Version 2.0 (the "License");
AppNearMe 0:e6bbb5a92267 7 you may not use this file except in compliance with the License.
AppNearMe 0:e6bbb5a92267 8 You may obtain a copy of the License at
AppNearMe 0:e6bbb5a92267 9
AppNearMe 0:e6bbb5a92267 10 http://www.apache.org/licenses/LICENSE-2.0
AppNearMe 0:e6bbb5a92267 11
AppNearMe 0:e6bbb5a92267 12 Unless required by applicable law or agreed to in writing, software
AppNearMe 0:e6bbb5a92267 13 distributed under the License is distributed on an "AS IS" BASIS,
AppNearMe 0:e6bbb5a92267 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AppNearMe 0:e6bbb5a92267 15 See the License for the specific language governing permissions and
AppNearMe 0:e6bbb5a92267 16 limitations under the License.
AppNearMe 0:e6bbb5a92267 17 */
AppNearMe 0:e6bbb5a92267 18
AppNearMe 0:e6bbb5a92267 19 #include "mbed.h"
AppNearMe 0:e6bbb5a92267 20 #include "micronfcboard.h"
AppNearMe 0:e6bbb5a92267 21
AppNearMe 0:e6bbb5a92267 22 MicroNFCBoard nfc(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS, D9);
AppNearMe 0:e6bbb5a92267 23
AppNearMe 0:e6bbb5a92267 24 int main() {
AppNearMe 0:e6bbb5a92267 25 nfc.init();
AppNearMe 0:e6bbb5a92267 26 bool b = true;
AppNearMe 0:e6bbb5a92267 27 for(int i = 0; i < 10; i++)
AppNearMe 0:e6bbb5a92267 28 {
AppNearMe 0:e6bbb5a92267 29 nfc.setLeds(b, !b);
AppNearMe 0:e6bbb5a92267 30 wait_ms(200);
AppNearMe 0:e6bbb5a92267 31 b = !b;
AppNearMe 0:e6bbb5a92267 32 }
AppNearMe 0:e6bbb5a92267 33
AppNearMe 0:e6bbb5a92267 34 //Start polling
AppNearMe 0:e6bbb5a92267 35 while(true)
AppNearMe 0:e6bbb5a92267 36 {
AppNearMe 0:e6bbb5a92267 37 printf("Poll\r\n");
AppNearMe 0:e6bbb5a92267 38 nfc.startPolling();
AppNearMe 0:e6bbb5a92267 39
AppNearMe 0:e6bbb5a92267 40 while( nfc.polling() );
AppNearMe 0:e6bbb5a92267 41
AppNearMe 0:e6bbb5a92267 42 if( nfc.type2() )
AppNearMe 0:e6bbb5a92267 43 {
AppNearMe 0:e6bbb5a92267 44 printf("Connected to type 2 tag\r\n");
AppNearMe 0:e6bbb5a92267 45 }
AppNearMe 0:e6bbb5a92267 46 else if( nfc.p2p() )
AppNearMe 0:e6bbb5a92267 47 {
AppNearMe 0:e6bbb5a92267 48 printf("Connected in P2P mode\r\n");
AppNearMe 0:e6bbb5a92267 49 }
AppNearMe 0:e6bbb5a92267 50
AppNearMe 0:e6bbb5a92267 51 bool ndefRead = false;
AppNearMe 0:e6bbb5a92267 52 bool ndefReadingStarted = false;
AppNearMe 0:e6bbb5a92267 53 while( nfc.connected() )
AppNearMe 0:e6bbb5a92267 54 {
AppNearMe 0:e6bbb5a92267 55 if( !ndefReadingStarted && nfc.ndefReadable() )
AppNearMe 0:e6bbb5a92267 56 {
AppNearMe 0:e6bbb5a92267 57 printf("Reading tag\r\n");
AppNearMe 0:e6bbb5a92267 58 ndefReadingStarted = true;
AppNearMe 0:e6bbb5a92267 59 nfc.ndefRead();
AppNearMe 0:e6bbb5a92267 60 }
AppNearMe 0:e6bbb5a92267 61 if( !ndefRead && nfc.ndefPresent() )
AppNearMe 0:e6bbb5a92267 62 {
AppNearMe 0:e6bbb5a92267 63 printf("Got message\r\n");
AppNearMe 0:e6bbb5a92267 64 char buf[512];
AppNearMe 0:e6bbb5a92267 65 if( nfc.readNdefUri(buf, sizeof(buf)) )
AppNearMe 0:e6bbb5a92267 66 {
AppNearMe 0:e6bbb5a92267 67 printf("Got URI: %s\r\n", buf);
AppNearMe 0:e6bbb5a92267 68 }
AppNearMe 0:e6bbb5a92267 69 if( nfc.readNdefText(buf, sizeof(buf)) )
AppNearMe 0:e6bbb5a92267 70 {
AppNearMe 0:e6bbb5a92267 71 printf("Got Text: %s\r\n", buf);
AppNearMe 0:e6bbb5a92267 72 }
AppNearMe 0:e6bbb5a92267 73 ndefRead = true;
AppNearMe 0:e6bbb5a92267 74 }
AppNearMe 0:e6bbb5a92267 75 }
AppNearMe 0:e6bbb5a92267 76
AppNearMe 0:e6bbb5a92267 77 printf("Disconnected\r\n");
AppNearMe 0:e6bbb5a92267 78 }
AppNearMe 0:e6bbb5a92267 79 }
AppNearMe 0:e6bbb5a92267 80