应用启动或恢复时读取Nfc标签

时间:2018-07-19 13:17:45

标签: android nfc

在我的Xamarin Forms安卓应用中,我接收并读取了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 个答案:

答案 0 :(得分:0)

我注意到这是否是您的理想选择。

我做什么:

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);
        }
    }
}

希望对您有所帮助:)