启动或恢复应用程序时读取Nfc标记



在我的Xamarin Forms android应用程序中,我收到并读取了一个nfc标签。当应用程序打开并聚焦时,以下代码可以正常工作。

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
NfcAdapter.IOnNdefPushCompleteCallback, NfcAdapter.ICreateNdefMessageCallback
{
private NfcAdapter _nfcAdapter;
const string _appModePrefix = "@AppMode";
const string _package = "com.companyname.nfc";
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
_nfcAdapter = NfcAdapter.GetDefaultAdapter(this);            
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnResume()
{
base.OnResume();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
{
var intent = new Intent(this, GetType()).AddFlags(ActivityFlags.SingleTop);
_nfcAdapter.EnableForegroundDispatch
(
this,
PendingIntent.GetActivity(this, 0, intent, 0),
new[] { new IntentFilter(NfcAdapter.ActionNdefDiscovered) },
new String[][] {
new string[] {
NFCTechs.Ndef
},
new string[] {
NFCTechs.MifareClassic,
},
}
);
_nfcAdapter.SetOnNdefPushCompleteCallback(this, this);
_nfcAdapter.SetNdefPushMessageCallback(this, this);
}
}
public void OnNdefPushComplete(NfcEvent e)
{
}
public NdefMessage CreateNdefMessage(NfcEvent e)
{            
byte[] mimeBytes = System.Text.Encoding.ASCII.GetBytes(string.Format("application/{0}",_package));
byte[] id = new byte[] { 1, 3, 3, 7 };
string appMode = "1";
string appId = "826F3361-2E93-4378-A6B9-33D2B6087246";
byte[] payload = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}|{2}", _appModePrefix, appMode, appId));
return new NdefMessage(new NdefRecord[] {
NdefRecord.CreateApplicationRecord(_package),                    
new NdefRecord(NdefRecord.TnfMimeMedia, mimeBytes, id, payload),                                        
});
}
protected override void OnPause()
{
base.OnPause();
if (_nfcAdapter != null && _nfcAdapter.IsEnabled && _nfcAdapter.IsNdefPushEnabled)
_nfcAdapter.DisableForegroundDispatch(this);
}
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
if (intent.Action == NfcAdapter.ActionTechDiscovered || intent.Action == NfcAdapter.ActionTagDiscovered)
{
var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);
if (rawMessages == null)
return;
//...
}         
}        

}

当应用程序未运行(或在后台工作(时,应用程序将按预期打开。我也得到onNewIntent事件,但在这种情况下是

intent.Action == "android.nfc.action.MAIN"

intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

返回null。在这一点上,是否有可能获得最初的意图(通过技术或标签发现的行动(?

我确信这是否是适合您的完美解决方案。

我的工作:

1.(在TECH_DISCOVERED 上使用意向过滤器定义活动

<activity
android:name=".activities.general.NFCActivity"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
</activity>

2.(定义技术的资源

<resources>
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcB</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcBarcode</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcF</tech>
</tech-list>
<tech-list>
<tech>android.nfc.tech.NfcV</tech>
</tech-list>
</resources>

3.(使用标签信息

if (getIntent() != null) {
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(NfcAdapter.EXTRA_ID)) {
byte[] id_array = getIntent().getByteArrayExtra(NfcAdapter.EXTRA_ID);
}
}
}

希望有帮助:(

最新更新