Java Code Examples for android.nfc.NfcAdapter#ACTION_TAG_DISCOVERED

The following examples show how to use android.nfc.NfcAdapter#ACTION_TAG_DISCOVERED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WifiNetworkActivity.java    From WiFiKeyShare with GNU General Public License v3.0 6 votes vote down vote up
private void setupForegroundDispatch() {
    /* initialize the PendingIntent to start for the dispatch */
    nfcPendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    /* initialize the IntentFilters to override dispatching for */
    nfcIntentFilters = new IntentFilter[3];
    nfcIntentFilters[0] = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    nfcIntentFilters[0].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[1] = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    nfcIntentFilters[1].addCategory(Intent.CATEGORY_DEFAULT);
    nfcIntentFilters[2] = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    nfcIntentFilters[2].addCategory(Intent.CATEGORY_DEFAULT);
    try {
        nfcIntentFilters[0].addDataType("*/*"); // Handle all MIME based dispatches.
    } catch (IntentFilter.MalformedMimeTypeException e) {
        Log.e(TAG, "setupForegroundDispatch: " + e.getMessage());
    }

    /* Initialize the tech lists used to perform matching for dispatching of the
     * ACTION_TECH_DISCOVERED intent */
    nfcTechLists = new String[][] {};
}
 
Example 2
Source File: ZannenNfcWriter.java    From effective_android_sample with Apache License 2.0 6 votes vote down vote up
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    setContentView(R.layout.main);
    findViewById(R.id.write_tag).setOnClickListener(mTagWriter);

    // Handle all of our received NFC intents in this activity.
    mNfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

    // Intent filters for reading a note from a tag or exchanging over p2p.
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndefDetected.addDataType("text/plain");
    } catch (MalformedMimeTypeException e) { }
    mNdefExchangeFilters = new IntentFilter[] { ndefDetected };

    // Intent filters for writing to a tag
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    mWriteTagFilters = new IntentFilter[] { tagDetected };
}
 
Example 3
Source File: NfcDetectorActivity.java    From effective_android_sample with Apache License 2.0 6 votes vote down vote up
/**
    * 
    * Initialize Nfc fields
    * 
    */
   
protected void initializeNfc() {
	nfcAdapter = NfcAdapter.getDefaultAdapter(this);
	nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
	
       IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
       IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
       IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
       writeTagFilters = new IntentFilter[] {ndefDetected, tagDetected, techDetected};
       
       nfcStateChangeBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final int state = intent.getIntExtra(EXTRA_ADAPTER_STATE, -1);
			if(state == STATE_OFF || state == STATE_ON) {
				
				runOnUiThread(new Runnable() {
				    public void run() {
						if(state == STATE_ON) {
							if(detecting) {
								enableForeground();
							}
						} 

				    	detectNfcStateChanges();
				    }
				});
			}
		}
	};
}
 
Example 4
Source File: NfcIdReaderActivity.java    From geopaparazzi with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();

    bluetoothDevice = BluetoothManager.INSTANCE.getBluetoothDevice();
    if (bluetoothDevice != null) {
        bluetoothDevice.addListener(this);
    }
    checkScanners();

    if (!inReadMode) {
        if (nfcAdapter != null && nfcAdapter.isEnabled()) {

            // Handle all of our received NFC intents in this activity.
            Intent intent = new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent nfcPendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

            // Intent filters for reading a note from a tag or exchanging over p2p.
            IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
            IntentFilter[] ndefExchangeFilters = new IntentFilter[]{tagDetected};
            nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, ndefExchangeFilters, null);
        }
    }
}
 
Example 5
Source File: MainActivity.java    From android-nfc-tag-read-write with MIT License 5 votes vote down vote up
@Override
protected void onResume() {
    super.onResume();
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};

    PendingIntent pendingIntent = PendingIntent.getActivity(
            this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    if(mNfcAdapter!= null)
        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);

}
 
Example 6
Source File: SignUpActivity.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onResumeWithService() {
    if (mNfcAdapter != null) {
        final IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
        final IntentFilter[] filters = new IntentFilter[]{filter};
        mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, filters, null);
    }
    UI.showIf(mNfcAdapter != null && mNfcAdapter.isEnabled(), mNfcSignupIcon);
}
 
Example 7
Source File: WriteNfcTagDialog.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
private void enableTagWriteMode() {
    IntentFilter intentFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter[] mWriteTagFilters = new IntentFilter[]{intentFilter};
    PendingIntent nfcPendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    nfcAdapter.enableForegroundDispatch(this, nfcPendingIntent, mWriteTagFilters, null);
}
 
Example 8
Source File: NfcDetectorActivity.java    From external-nfc-api with Apache License 2.0 5 votes vote down vote up
/**
    * 
    * Initialize Nfc fields
    * 
    */
   
protected void initializeNfc() {
	nfcAdapter = NfcAdapter.getDefaultAdapter(this);
	nfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, this.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
	
       IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
       IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
       IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
       IntentFilter tagLost = new IntentFilter(ACTION_TAG_LEFT_FIELD);
       
       writeTagFilters = new IntentFilter[] {ndefDetected, tagDetected, techDetected, tagLost};
       
       nfcStateChangeBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			final int state = intent.getIntExtra(EXTRA_ADAPTER_STATE, -1);
			if(state == STATE_OFF || state == STATE_ON) {
				
				runOnUiThread(new Runnable() {
				    public void run() {
						if(state == STATE_ON) {
							if(detecting) {
								enableForeground();
							}
						} 

				    	detectNfcStateChanges();
				    }
				});
			}
		}
	};
}
 
Example 9
Source File: ZannenNfcWriter.java    From effective_android_sample with Apache License 2.0 5 votes vote down vote up
private void enableTagWriteMode() {
    mWriteMode = true;
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    mWriteTagFilters = new IntentFilter[] {
        tagDetected
    };
    mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mWriteTagFilters, null);
}
 
Example 10
Source File: NfcUtil.java    From zap-android with MIT License 4 votes vote down vote up
public static IntentFilter[] IntentFilters() {
    IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
    return new IntentFilter[]{techDetected, tagDetected, ndefDetected};
}
 
Example 11
Source File: ACRBroadcast.java    From external-nfc-api with Apache License 2.0 4 votes vote down vote up
public void onTagNdefDiscovered(int slotNumber, int[] techList, Bundle[] bundles, Boolean writable, byte[] id, NdefMessage messages, Object instance) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    String action = NfcAdapter.ACTION_TAG_DISCOVERED;

    final Intent intent = new Intent(action);
    intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, new NdefMessage[]{messages});

    Constructor<?>[] constructors = Tag.class.getConstructors();

    // public Tag(byte[] id, int[] techList, Bundle[] techListExtras, int serviceHandle, INfcTag tagService)
    Parcelable tag = (Parcelable) constructors[0].newInstance(id, techList, bundles, slotNumber, instance);

    intent.putExtra(NfcAdapter.EXTRA_TAG, tag);
    intent.putExtra(NfcAdapter.EXTRA_ID, id);

    Log.d(TAG, "Constructed " + tag);

    sendBroadcast(intent);
}
 
Example 12
Source File: NfcActivity.java    From commcare-android with Apache License 2.0 3 votes vote down vote up
/**
 * Makes it so that this activity will be the default to handle a new tag when it is discovered.
 *
 * The intent filters being passed to enableForegroundDispatch() here are intentionally very
 * broad, on the assumption that if CommCare's NfcActivity is in the foreground while a user
 * tries to scan an NFC tag, they were intending to scan something that CommCare would
 * recognize. So if the user scans a tag of a type that we aren't expecting, instead of
 * filtering it out (which would result in the device's default NFC handler consuming the tag,
 * resulting in confusing on-screen behavior for the user), we'll consume it ourselves and
 * then show a useful error message.
 */
private void setReadyToHandleTag() {
    IntentFilter ndefDiscoveredFilter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    IntentFilter tagDiscoveredFilter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
    IntentFilter[] intentFilters = new IntentFilter[]{ndefDiscoveredFilter, tagDiscoveredFilter};
    this.nfcManager.enableForegroundDispatch(this, this.pendingNfcIntent, intentFilters, null);
}