ElectronicCats / ElectronicCats-PN7150

Arduino library for I2C access to the PN7150 RFID/Near Field Communication chip
MIT License
33 stars 15 forks source link

Refactor NDEF examples with new methods #59

Closed DeimosHall closed 8 months ago

DeimosHall commented 8 months ago

This pull request renames NDEFRead and NDEFSend to NDEFReadMessage and NDEFSendRawMessage respectively. Furthermore, it add a new example called NDEFSendMessage that uses the new methods to create messages.

Advanced users can create their own custom messages using NDEFSendRawMessage like this:

// Create a message with 1 record
const char ndefMessage[] = {0xD1,  // MB/ME/CF/1/IL/TNF
                            0x01,  // Type length (1 byte)
                            0x08,  // Payload length (status, language, content)
                            'T',  // Type -> 'T' for text, 'U' for URI
                            0x02,  // Status
                            'e', 'n',  // Language
                            'H', 'e', 'l', 'l', 'o',};  // Content

In the NDEFSendMessage example, the message is created using methods. Users can create as many records as they want, with a maximum of 255 bytes per message. However, the library only supports the creation of one message at a time.

message.addTextRecord("Hello");                          // English by default
message.addTextRecord("world", "en");                    // English explicitly, the library only supports two letter language codes (ISO 639-1) by now
message.addTextRecord("Hola mundo!", "es");              // Spanish explicitly, check a language code table at https://www.science.co.il/language/Locale-codes.php
message.addTextRecord("Bonjour le monde!", "fr");        // French explicitly
message.addUriRecord("google.com");                      // No prefix explicitly
message.addUriRecord("https://www.electroniccats.com");  // https://www. prefix explicitly
message.addUriRecord("tel:1234567890");                  // The library can handle all the prefixes listed at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#uri-prefixes
String ssid = "Bomber Cat";                              // SSID of the WiFi network
String authenticationType = "WPA2 PERSONAL";             // Supported authentication types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-authentication-types
String encryptionType = "AES";                           // Supported encryption types at https://github.com/ElectronicCats/ElectronicCats-PN7150/blob/master/API.md#wifi-encryption-types
String password = "Password";                            // Password of the WiFi network
message.addWiFiRecord(ssid, authenticationType, encryptionType, password);
message.addUriRecord("mailto:deimoshall@gmail.com");
message.addMimeMediaRecord("text/plain", "Hello world!", 12);  // Media-type as defined in RFC 2046

The first approach gives the user more control over the message, but it requires more knowledge about the NDEF format. The second one is easier, but it has the following limitations:

In future versions this limitations could be removed.

The status of the completed and pending tasks is as follows: