How can I send a string through NFC while Screen-Pinning?

前端 未结 1 2027

I am trying to send a String through NFC while my app is using screen pinning. It does not work: The transfer does not happen; but if I disable

1条回答
  •  [愿得一人]
    2020-12-09 19:38

    I'm not sure if this actually answers your question, but I'd like to summarize my findings:

    When trying your example on Android 5.0.1 (LRX22C on Nexus 4), the receiving side automatically unpins the screen upon receiving the NDEF message and (re-)starts the activity. So it seems that the intent filter that is registered in the manifest gets priority over (manual?) screen pinning.

    I'm aware that this does not quite match the experiences described in the question. I'm wondering if this is due to the different Android version (5.0 vs. 5.0.1) or due to the use of manual screen pinning instead of programatic screen pinning...

    In my test setup, I was able to solve the problem (i.e. prevent the activity from getting automatically unpinned) by using the foreground dispatch system to register the activity to receive its NDEF message:

    In your onResume() method create a pending intent like this and enable foreground dispatch:

    PendingIntent pi = this.createPendingResult(0x00A, new Intent(), 0);
    nfcAdapter.enableForegroundDispatch(this, pi, null, null);
    

    You will then receive intents notifying you about discovered tags through the activity's onActivityResult() method:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 0x00A:
                onNewIntent(data);
            break;
        }
    }
    

    Moreover, you have to disable the foreground dispatch in your onPause() method:

    nfcAdapter.disableForegroundDispatch(this);
    

    0 讨论(0)
提交回复
热议问题