Code example for IntentFilter

Methods: addDataType

0
 
    private void registerMimeType(JSONArray data, CallbackContext callbackContext) throws JSONException {
        String mimeType = "";
        try { 
            mimeType = data.getString(0);
            intentFilters.add(createIntentFilter(mimeType));
            callbackContext.success();
        } catch (MalformedMimeTypeException e) {
            callbackContext.error("Invalid MIME Type " + mimeType);
        } 
    } 
 
    // Cheating and writing an empty record. We may actually be able to erase some tag types. 
    private void eraseTag(CallbackContext callbackContext) throws JSONException {
        Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NdefRecord[] records = {
            new NdefRecord(NdefRecord.TNF_EMPTY, new byte[0], new byte[0], new byte[0])
        }; 
        writeNdefMessage(new NdefMessage(records), tag, callbackContext);
    } 
     
    private void writeTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
        if (getIntent() == null) {  // TODO remove this and handle LostTag 
            callbackContext.error("Failed to write tag, received null intent");
        } 
 
        Tag tag = savedIntent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
        writeNdefMessage(new NdefMessage(records), tag, callbackContext);
    } 
 
    private void writeNdefMessage(final NdefMessage message, final Tag tag, final CallbackContext callbackContext) {
        cordova.getThreadPool().execute(new Runnable() {
            @Override 
            public void run() { 
                try { 
                    Ndef ndef = Ndef.get(tag);
                    if (ndef != null) {
                        ndef.connect();
 
                        if (ndef.isWritable()) {
                            int size = message.toByteArray().length;
                            if (ndef.getMaxSize() < size) {
                                callbackContext.error("Tag capacity is " + ndef.getMaxSize() +
                                        " bytes, message is " + size + " bytes.");
                            } else { 
                                ndef.writeNdefMessage(message);
                                callbackContext.success();
                            } 
                        } else { 
                            callbackContext.error("Tag is read only");
                        } 
                        ndef.close();
                    } else { 
                        NdefFormatable formatable = NdefFormatable.get(tag);
                        if (formatable != null) {
                            formatable.connect();
                            formatable.format(message);
                            callbackContext.success();
                            formatable.close();
                        } else { 
                            callbackContext.error("Tag doesn't support NDEF");
                        } 
                    } 
                } catch (FormatException e) {
                    callbackContext.error(e.getMessage());
                } catch (TagLostException e) {
                    callbackContext.error(e.getMessage());
                } catch (IOException e) {
                    callbackContext.error(e.getMessage());
                } 
            } 
        }); 
    } 
 
    private void shareTag(JSONArray data, CallbackContext callbackContext) throws JSONException {
        NdefRecord[] records = Util.jsonToNdefRecords(data.getString(0));
        this.p2pMessage = new NdefMessage(records);
 
        startNdefPush(callbackContext);
    } 
 
    private void createPendingIntent() { 
        if (pendingIntent == null) {
            Activity activity = getActivity();
            Intent intent = new Intent(activity, activity.getClass());
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
        } 
    } 
 
    private void addTechList(String[] list) {
        this.addTechFilter();
        this.addToTechList(list);
    } 
 
    private void addTechFilter() { 
        intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED));
    } 
 
    private void addTagFilter() { 
        intentFilters.add(new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED));
    } 
 
    private void startNfc() { 
        createPendingIntent(); // onResume can call startNfc before execute 
 
        getActivity().runOnUiThread(new Runnable() {
            public void run() { 
                NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
 
                if (nfcAdapter != null && getActivity().isFinishing() == false) {
                    nfcAdapter.enableForegroundDispatch(getActivity(), getPendingIntent(), getIntentFilters(), getTechLists());
 
                    if (p2pMessage != null) {
                        nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
                    } 
 
                } 
            } 
        }); 
    } 
 
    private void stopNfc() { 
        Log.d(TAG, "stopNfc");
        getActivity().runOnUiThread(new Runnable() {
            public void run() { 
 
                NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
 
                if (nfcAdapter != null) {
                    nfcAdapter.disableForegroundDispatch(getActivity());
                } 
            } 
        }); 
    } 
 
    private void startNdefPush(final CallbackContext callbackContext) {
        getActivity().runOnUiThread(new Runnable() {
            public void run() { 
 
                NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
 
                if (nfcAdapter == null) {
                    callbackContext.error(STATUS_NO_NFC);
                // isNdefPushEnabled would be nice, but requires android-17 
                //} else if (!nfcAdapter.isNdefPushEnabled()) { 
                //    callbackContext.error(STATUS_NDEF_PUSH_DISABLED); 
                } else { 
                    nfcAdapter.setNdefPushMessage(p2pMessage, getActivity());
                    callbackContext.success();
                } 
            } 
        }); 
    } 
 
    private void stopNdefPush() { 
        getActivity().runOnUiThread(new Runnable() {
            public void run() { 
 
                NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
 
                if (nfcAdapter != null) {
                    nfcAdapter.setNdefPushMessage(null, getActivity());
                } 
 
            } 
        }); 
    } 
 
    private void addToTechList(String[] techs) {
        techLists.add(techs);
    } 
 
    private IntentFilter createIntentFilter(String mimeType) throws MalformedMimeTypeException {
        IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        intentFilter.addDataType(mimeType);
        return intentFilter;
    } 
 
    private PendingIntent getPendingIntent() {
        return pendingIntent;