NFC broadcastreceiver problem

前端 未结 2 613
孤城傲影
孤城傲影 2021-02-11 03:53

I want my app to listen to nfc tags only when is activated. For this I tried to register an nfc listener as following, without any success.

IntentFilter filter          


        
相关标签:
2条回答
  • 2021-02-11 04:13

    If you do not want to use foreground mode, your can always programmatically enable or disable intent filters.

    The NDEF Tools for Android project has working sample using foreground mode, also detects

    1. NFC device support
    2. NFC enabled / disabled on activity launch, or later changed
    3. NFC push enabled / disabled on activity launch, or later changed
    0 讨论(0)
  • 2021-02-11 04:28

    Try to use Foreground Dispatch System.

    To enable it, on the activity's onCreate method, you should prepare some stuffs:

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    

    after that, create the IntentFilters (in my example, all actions are handled using Intent Filters):

        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            ndef.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
    
        IntentFilter tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try {
            tech.addDataType("*/*");
        } catch (MalformedMimeTypeException e) {
            throw new RuntimeException("fail", e);
        }
    
        IntentFilter[] intentFiltersArray = new IntentFilter[] { tag, ndef, tech };
    

    after that, you'll need a String array to contain supported technologies:

        String[][] techList = new String[][] { new String[] { NfcA.class.getName(),
                NfcB.class.getName(), NfcF.class.getName(),
                NfcV.class.getName(), IsoDep.class.getName(),
                MifareClassic.class.getName(),
                MifareUltralight.class.getName(), Ndef.class.getName() } };
    

    in the onResume method, you should enable the foreground dispatch method:

    NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
    nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);
    

    and disable in onPause:

    @Override
    protected void onPause() {
        super.onPause();
        nfcAdapter.disableForegroundDispatch(this);
    }
    

    By this way, you have successfully initialized the needed mechanism. To handle a received Intent you should override the onNewIntent(Intent intent) method.

    @Override
    public void onNewIntent(Intent intent) {
        String action = intent.getAction();
    
        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
            // reag TagTechnology object...
        } else if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
            // read NDEF message...
        } else if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
    
        }
    }
    

    Note: if you want to handle the intents by only foreground dispatching, do not enable Intent Dispatch System in your manifest file, just give the right permissions to your application there.

    I hope that helps.

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