MessageBuilder
Mbed OS provides this API to construct NDEF messages, the common data format exchange for NFC messages. The class mbed::nfc::ndef::MessageBuilder
builds an NDEF message into a user-provided buffer. URI
, Text
and Mime
types can be serialized in the builder with the help of the member function append_as_record
.
MessageBuilder class reference
Data Structures | |
struct | PayloadBuilder |
Build a record payload. More... |
Public Member Functions | |
MessageBuilder (const Span< uint8_t > &buffer) | |
Create a new MessageBuilder that can be used to construct valid NDEF messages. More... | |
bool | append_record (const RecordType &type, const RecordPayload &payload=RecordPayload(), bool is_last_record=false) |
Append a new record to the message being built. More... | |
bool | append_record (const RecordType &type, const PayloadBuilder &builder, bool is_last_record=false) |
Append a new record to the message being built. More... | |
bool | append_record (const Record &record, const PayloadBuilder *builder=NULL) |
Append a new record to the message being built. More... | |
void | reset () |
Reset the builder state. More... | |
void | reset (const Span< uint8_t > &buffer) |
Reset the builder state and assign a new buffer to it. More... | |
bool | is_message_complete () const |
Return true if the message stored is complete and false otherwise. More... | |
Span< const uint8_t > | get_message () const |
Return the buffer storing the data if the message is complete or an empty buffer if the message is not complete. More... |
Static Public Member Functions | |
static size_t | compute_record_size (const Record &record, const PayloadBuilder *builder=NULL) |
Compute the size of a record. More... |
MessageBuilder example
/*
* Copyright (c) 2006-2020 Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*/
#include "mbed.h"
#include "nfc/ndef/MessageBuilder.h"
#include "nfc/ndef/common/URI.h"
#include "nfc/ndef/common/Text.h"
#include "nfc/ndef/common/util.h"
using mbed::nfc::ndef::MessageBuilder;
using mbed::nfc::ndef::common::Text;
using mbed::nfc::ndef::common::URI;
using mbed::nfc::ndef::common::span_from_cstr;
int main()
{
uint8_t message_data[128];
const Span<uint8_t, 128> buffer(message_data, sizeof(message_data));
MessageBuilder builder(buffer);
URI uri(URI::HTTPS_WWW, span_from_cstr("mbed.com"));
Text text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("Mbed website"));
uri.append_as_record(builder);
text.append_as_record(builder, /* last record */ true);
printf("Message size: %d bytes\r\n", builder.get_message().size());
}
Related content
- MessageParser API reference.
- SimpleMessageParser API reference.
- NFC architecture.