Flutter NFC管理插件nfc_manager_new的使用
Flutter NFC管理插件nfc_manager_new的使用
nfc_manager_new
插件用于管理和操作NFC标签,并支持设置密码、移除密码及通过密码进行认证。
开始使用
注意: 此插件依赖于 NFCTagReaderSession
(需要iOS 13.0或更高版本)和 NfcAdapter#enableReaderMode
(需要Android API级别19或更高版本)。
设置
Android设置
- 在
AndroidManifest.xml
中添加<uses-permission android:name="android.permission.NFC" />
。
iOS设置
- 添加
Near Field Communication Tag Reader Session Formats Entitlements
到你的 entitlements 文件。 - 在
Info.plist
中添加NFCReaderUsageDescription
。 - 根据需要在
Info.plist
中添加com.apple.developer.nfc.readersession.felica.systemcodes
和com.apple.developer.nfc.readersession.iso7816.select-identifiers
。
使用方法
处理会话
// 检查可用性
bool isAvailable = await NfcManager.instance.isAvailable();
// 启动会话
NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async {
// 在这里处理发现的NfcTag实例
},
);
// 停止会话
NfcManager.instance.stopSession();
处理平台标签
以下平台标签类是可用的:
Ndef
FeliCa
(仅限iOS)Iso7816
(仅限iOS)Iso15693
(仅限iOS)MiFare
(仅限iOS)NfcA
(仅限Android)NfcB
(仅限Android)NfcF
(仅限Android)NfcV
(仅限Android)IsoDep
(仅限Android)MifareClassic
(仅限Android)MifareUltralight
(仅限Android)NdefFormatable
(仅限Android)
可以通过调用类上的工厂构造函数 from
获取实例。例如:
Ndef? ndef = Ndef.from(tag);
if (ndef == null) {
print('Tag is not compatible with NDEF');
return;
}
// 在这里处理Ndef实例
请参阅 API 文档 以获取更多详细信息。
实际应用
查看 此仓库,它展示了如何使用此插件的实际应用。
完整示例
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:nfc_manager_new/nfc_manager_new.dart';
import 'package:nfc_manager_new/nfc_manager_utlity.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _nfcManagerNewPlugin = NfcManagerNew();
[@override](/user/override)
void initState() {
super.initState();
initPlatformState();
}
// 平台消息是异步的,因此我们在异步方法中初始化
Future<void> initPlatformState() async {
String platformVersion;
// 平台消息可能失败,因此我们使用try/catch处理PlatformException
// 我们还处理消息可能返回null的情况
try {
platformVersion = await _nfcManagerNewPlugin.getPlatformVersion() ??
'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
// 如果小部件从树中被移除而异步平台消息正在飞行,则我们希望丢弃回复而不是调用setState来更新我们的非存在的外观。
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
var size = MediaQuery.of(context).size;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
InkWell(
onTap: () async {
final isRemovePassword = await NfcManager.instance.startSession(
alertMessage:
'Hold the NFC tag on the upper back of your iPhone',
onDiscovered: (NfcTag tag) async {
final isRemovePassword =
await NfcManagerUtility.removePasswordWithTag(
tag: tag);
print("isRemovePassword: $isRemovePassword");
if (isRemovePassword) {
await NfcManager.instance.stopSession(
alertMessage: "Password remove successfully ");
}
});
},
child: button(size,
color: Colors.orangeAccent,
text: 'Password remove ',
textColor: Colors.white),
),
InkWell(
onTap: () async {
final isRemovePassword = await NfcManager.instance.startSession(
alertMessage:
'Hold the NFC tag on the upper back of your iPhone',
onDiscovered: (NfcTag tag) async {
final isSetPassword =
await NfcManagerUtility.setPasswordWithTag(
tag: tag, password: "1111");
print("isSetPassword: $isSetPassword");
if (isSetPassword) {
await NfcManager.instance.stopSession(
alertMessage: "Password set successfully ");
}
});
},
child: button(size,
color: Colors.orangeAccent,
text: 'Password set',
textColor: Colors.white),
),
InkWell(
onTap: () async {
final isRemovePassword = await NfcManager.instance.startSession(
alertMessage:
'Hold the NFC tag on the upper back of your iPhone',
onDiscovered: (NfcTag tag) async {
final isAuthTag = await NfcManagerUtility.authTag(
tag: tag, password: "1111");
print("isAuthTag: $isAuthTag");
if (isAuthTag) {
await NfcManager.instance.stopSession(
alertMessage: "Password Auth successfully ");
} else {
await NfcManager.instance
.stopSession(errorMessage: "Password can not auth");
}
});
},
child: button(size,
color: Colors.orangeAccent,
text: 'Password Auth',
textColor: Colors.white),
),
],
),
),
);
}
Container button(Size size,
{required Color color, required Color textColor, required String text}) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 10),
alignment: Alignment.center,
width: size.width * 0.8,
padding: EdgeInsets.all(20),
decoration: BoxDecoration(
color: color, borderRadius: BorderRadius.circular(20)),
child: Text(
"$text".toUpperCase(),
style: TextStyle(color: textColor),
));
}
}
更多关于Flutter NFC管理插件nfc_manager_new的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复