This program demonstrates how to use a MicroNFCBoard as a peripheral to receive a message from a phone/tablet using P2P/SNEP/Android Beam.
Dependencies: MicroNFCBoardAPI mbed
Fork of MicroNFCBoardAPI_P2P_Server by
main.cpp@2:37d78773f875, 2015-05-14 (annotated)
- Committer:
- AppNearMe
- Date:
- Thu May 14 17:24:49 2015 +0000
- Revision:
- 2:37d78773f875
- Parent:
- 1:5b804c0c8aa8
P2P Server fork
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:37d78773f875 | 26 | |
AppNearMe | 2:37d78773f875 | 27 | //Start polling |
AppNearMe | 2:37d78773f875 | 28 | while(true) |
AppNearMe | 0:e6bbb5a92267 | 29 | { |
AppNearMe | 2:37d78773f875 | 30 | printf("Poll\r\n"); |
AppNearMe | 2:37d78773f875 | 31 | nfc.startPolling(false, false, true); |
AppNearMe | 2:37d78773f875 | 32 | |
AppNearMe | 2:37d78773f875 | 33 | while( nfc.polling() ); |
AppNearMe | 2:37d78773f875 | 34 | |
AppNearMe | 2:37d78773f875 | 35 | if( nfc.p2p() ) |
AppNearMe | 2:37d78773f875 | 36 | { |
AppNearMe | 2:37d78773f875 | 37 | printf("Connected in P2P mode\r\n"); |
AppNearMe | 2:37d78773f875 | 38 | } |
AppNearMe | 2:37d78773f875 | 39 | else |
AppNearMe | 2:37d78773f875 | 40 | { |
AppNearMe | 2:37d78773f875 | 41 | continue; |
AppNearMe | 2:37d78773f875 | 42 | } |
AppNearMe | 2:37d78773f875 | 43 | |
AppNearMe | 2:37d78773f875 | 44 | bool ndefRead = false; |
AppNearMe | 2:37d78773f875 | 45 | while( nfc.connected() ) |
AppNearMe | 2:37d78773f875 | 46 | { |
AppNearMe | 2:37d78773f875 | 47 | if( !ndefRead && nfc.ndefPresent() ) |
AppNearMe | 2:37d78773f875 | 48 | { |
AppNearMe | 2:37d78773f875 | 49 | printf("Received message\r\n"); |
AppNearMe | 2:37d78773f875 | 50 | char buf[512]; |
AppNearMe | 2:37d78773f875 | 51 | if( nfc.readNdefUri(buf, sizeof(buf)) ) |
AppNearMe | 2:37d78773f875 | 52 | { |
AppNearMe | 2:37d78773f875 | 53 | printf("Got URI: %s\r\n", buf); |
AppNearMe | 2:37d78773f875 | 54 | } |
AppNearMe | 2:37d78773f875 | 55 | if( nfc.readNdefText(buf, sizeof(buf)) ) |
AppNearMe | 2:37d78773f875 | 56 | { |
AppNearMe | 2:37d78773f875 | 57 | printf("Got Text: %s\r\n", buf); |
AppNearMe | 2:37d78773f875 | 58 | } |
AppNearMe | 2:37d78773f875 | 59 | ndefRead = true; |
AppNearMe | 2:37d78773f875 | 60 | } |
AppNearMe | 2:37d78773f875 | 61 | } |
AppNearMe | 2:37d78773f875 | 62 | |
AppNearMe | 2:37d78773f875 | 63 | printf("Disconnected\r\n"); |
AppNearMe | 0:e6bbb5a92267 | 64 | } |
AppNearMe | 0:e6bbb5a92267 | 65 | } |