flutter-nfc-manager
flutter-nfc-manager copied to clipboard
Migration vom 3.x to 4.x
How can i create an NdefMessage with the lastest package?
This is my old implementation for creating an ndef message in package version 3.x:
final NdefMessage message = NdefMessage(records: [
NdefRecord.createExternal(
Constants.nfcNdefExternalRecordAndroidDomain,
Constants.nfcNdefExternalRecordAndroidTyp,
Uint8List.fromList(Constants.nfcNdefExternalRecordPayload.codeUnits)),
NdefRecord.createMime(Constants.nfcNdefMimeType, Uint8List.fromList(Constants.nfcCardPayloadText.codeUnits)),
NdefRecord.createUri(Uri.parse(Constants.nfcUri)),
]);
The factory methods .createExternal, .createUri, .createMime are not supported in version 4.x anymore - this breaks my production app.
@okadan can you provide an example how to create a NdefMessage?
Have you found a solution? I couldn't find the documentation anywhere. The example.md also shows hello world app which is not useful at all
@ilhamwd You can create it using this function that used in the package instead:
const uriPrefixList = [
'',
'http://www.',
'https://www.',
'http://',
'https://',
'tel:',
'mailto:',
'ftp://anonymous:anonymous@',
'ftp://ftp.',
'ftps://',
'sftp://',
'smb://',
'nfs://',
'ftp://',
'dav://',
'news:',
'telnet://',
'imap:',
'rtsp://',
'urn:',
'pop:',
'sip:',
'sips:',
'tftp:',
'btspp://',
'btl2cap://',
'btgoep://',
'tcpobex://',
'irdaobex://',
'file://',
'urn:epc:id:',
'urn:epc:tag:',
'urn:epc:pat:',
'urn:epc:raw:',
'urn:epc:',
'urn:nfc:',
];
NdefRecord _createNdefRecord(Uri uri) {
final uriString = uri.normalizePath().toString();
if (uriString.isEmpty) throw ('uri is empty');
int prefixIndex = NFCService.uriPrefixList.indexWhere((e) => uriString.startsWith(e), 1);
if (prefixIndex < 0) prefixIndex = 0;
return NdefRecord(
typeNameFormat: TypeNameFormat.wellKnown,
type: Uint8List.fromList([0x55]),
identifier: Uint8List.fromList([]),
payload: Uint8List.fromList(
[prefixIndex] + utf8.encode(uriString.substring(NFCService.uriPrefixList[prefixIndex].length)),
),
);
}
x
`