diff --git a/README.md b/README.md index 7912cd3..1e0eea2 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ * Digital signature * Smart poster * Connection handover + * Android application record * Media (MIME data) * Bluetooth easy pairing / Bluetooth low energy * other MIME data diff --git a/lib/ndef.dart b/lib/ndef.dart index e2e1a44..c458dbb 100644 --- a/lib/ndef.dart +++ b/lib/ndef.dart @@ -10,14 +10,14 @@ export 'record/signature.dart'; export 'record/smartposter.dart'; export 'record/text.dart'; export 'record/uri.dart'; +export 'record/androidApplication.dart'; import 'dart:typed_data'; import 'record.dart'; import 'utilities.dart'; /// Decode raw NDEF messages (containing at least one [NDEFRecord]) from byte array -List decodeRawNdefMessage(Uint8List data, - {var typeFactory = NDEFRecord.defaultTypeFactory}) { +List decodeRawNdefMessage(Uint8List data, {var typeFactory = NDEFRecord.defaultTypeFactory}) { var records = []; var stream = new ByteStream(data); while (!stream.isEnd()) { @@ -36,17 +36,14 @@ List decodeRawNdefMessage(Uint8List data, /// Decode a NDEF record, providing its parts separately. /// This is most useful in mobile environment because the APIs will give you these information in a separate manner. -NDEFRecord decodePartialNdefMessage( - TypeNameFormat tnf, Uint8List type, Uint8List payload, - {required Uint8List id}) { +NDEFRecord decodePartialNdefMessage(TypeNameFormat tnf, Uint8List type, Uint8List payload, {required Uint8List? id}) { var decoded = NDEFRecord.doDecode(tnf, type, payload, id: id); return decoded; } /// Encode an NDEF message (containing several [NDEFRecord]s) to byte array. /// Set [canonicalize] to set the MB and ME fields automatically in the first / last record. -Uint8List encodeNdefMessage(List records, - {bool canonicalize = true}) { +Uint8List encodeNdefMessage(List records, {bool canonicalize = true}) { if (records.length == 0) { return new Uint8List(0); } diff --git a/lib/record/androidApplication.dart b/lib/record/androidApplication.dart new file mode 100644 index 0000000..d3b73db --- /dev/null +++ b/lib/record/androidApplication.dart @@ -0,0 +1,34 @@ +import 'dart:convert'; +import 'dart:typed_data'; + +import 'package:ndef/record/externalRecord.dart'; + +class AARRecord extends ExternalRecord { + static const String classType = "android.com:pkg"; + + @override + String get decodedType { + return AARRecord.classType; + } + + String packageName; + + AARRecord(this.packageName); + + @override + Uint8List? get payload { + return Uint8List.fromList(utf8.encode(packageName)); + } + + set payload(Uint8List? payload) { + packageName = utf8.decode(payload!); + } + + @override + String toString() { + var str = "AARRecord: "; + str += basicInfoString; + str += "package=$packageName"; + return str; + } +} diff --git a/lib/record/externalRecord.dart b/lib/record/externalRecord.dart new file mode 100644 index 0000000..30753d8 --- /dev/null +++ b/lib/record/externalRecord.dart @@ -0,0 +1,26 @@ +import 'dart:typed_data'; + +import '../ndef.dart'; + +class ExternalRecord extends NDEFRecord { + static const TypeNameFormat classTnf = TypeNameFormat.nfcExternal; + + TypeNameFormat get tnf { + return classTnf; + } + + ExternalRecord({String? decodedType, Uint8List? payload, Uint8List? id}) : super(id: id, payload: payload) { + if (decodedType != null) { + this.decodedType = decodedType; + } + } + + @override + String toString() { + var str = "ExternalRecord: "; + str += basicInfoString; + str += "type=$decodedType "; + str += "payload=${(payload?.toHexString()) ?? '(null)'}"; + return str; + } +}