androidnfcintentfilternfc-p2pandroid-beam

Intent Filter not working for Android Beam NFC


I am sure this is simple but I cannot figure it out. All I am trying to do is send a message via NFC (android Beam) and open my App on the receiver device. I did test my code on a new Project and it worked perfectly but if I try it on my real Project it just start "New Tag collected" and shows application/eu.freemoser.mydebts2go (see my screenshot). I don't know what's wrong maybe something with the manifest? The Google results doesn't match with my problem (or I am just to stupid) but I believe I found some related answer unfortunately I still was not able to solve my problem

My Manifest:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="eu.freemoser.myDebts2go"
        android:versionCode="16"

        android:versionName="1.1.0">

        <uses-permission android:name="android.permission.GET_ACCOUNTS" />
        <uses-permission android:name="android.permission.READ_CONTACTS" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.NFC" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

        <uses-feature android:name="android.hardware.nfc" android:required="false" />
        <uses-feature android:name="android.hardware.camera" />



        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="19" />
        <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true" />

        <application
            android:name="eu.freemoser.myDebts2go.MyApplication"
            android:allowBackup="true"
            android:hardwareAccelerated="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/Theme.Mydebts2go">
            >


            <service android:name="eu.freemoser.myDebts2goService.NotificationService"></service>
            <service android:name="eu.freemoser.myDebts2goService.SynchronizationService"></service>
            <service android:name="eu.freemoser.myDebts2goService.SynchronizService"></service>
            <service android:name="eu.freemoser.myDebts2goService.AwesomeSynchronizService"></service>


            <activity
                android:name="eu.freemoser.myDebts2go.MainActivity"
                android:label="@string/app_name"
                android:screenOrientation="portrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>


            <activity
           android:name="eu.freemoser.myDebts2GoActivities.AwesomeDetailActivity"
                android:screenOrientation="portrait" android:theme="@style/Theme.MyDebts2GO.Detail">
            </activity>

            <activity android:name=".AndroidBeamActivity" android:screenOrientation="portrait"
                android:theme="@android:style/Theme.DeviceDefault.Wallpaper.NoTitleBar">
                <intent-filter>
                    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="application/eu.freemoser.myDebts2go" />
                </intent-filter>
            </activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.DriveRestoreActivity"></activity>
            <activity android:name="eu.freemoser.myDebts2GoActivities.SynchronizActivity"></activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.SettingActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.DatePickerActivity"></activity>
            <activity android:name="eu.freemoser.myDebts2GoActivities.LocationPickerActivity"></activity>

            <activity android:name="eu.freemoser.myDebts2GoActivities.SearchActivity">
                <intent-filter>
                    <action android:name="android.intent.action.SEARCH" />
                </intent-filter>
            </activity>

            <activity
                android:name="eu.freemoser.myDebts2GoActivities.DriveAuthorzingActivity"
                android:theme="@android:style/Theme.Translucent.NoTitleBar">
            </activity>

            <activity
                android:name="eu.freemoser.myDebts2GoActivities.FastModusShortcutActivity"
                android:exported="true"
                android:theme="@android:style/Theme.Holo.Light.NoActionBar">
            </activity>


            <!-- android:value="API_KEY" /> DEBUG-->

            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="API_KEY" />
            <meta-data
                android:name="com.google.android.gms.version"
                android:value="VERSION" />


        </application>


    </manifest>

The Fragment (sender)

public class AwesomeDetailFragment extends Fragment implements ObservableScrollView.Callbacks, NfcAdapter.CreateNdefMessageCallback, NfcAdapter.OnNdefPushCompleteCallback {
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       ...
        setUpAndroidBeam();
        return mRootView;
    }

    private void setUpAndroidBeam() {
        PackageManager pm = getActivity().getPackageManager();
        // Check whether NFC is available on device
        if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
            // NFC is not available on the device.

        }
        // Check whether device is running Android 4.1 or higher
        else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            // Android Beam feature is not supported.

        } else {
            myNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
            manageNfc();

        }
    }

    private void manageNfc() {
        if (myNfcAdapter != null) {
            myNfcAdapter.setNdefPushMessageCallback(this, getActivity());
            myNfcAdapter.setOnNdefPushCompleteCallback(this, getActivity());
        }
    }

    ...

    @Override
    public NdefMessage createNdefMessage(NfcEvent event) {
        Time time = new Time();
        time.setToNow();
        String text = myBetrag + "//" + myTitle + "//" + myContactName + "//" + myStatus + "//" + myDebtDate + "//" + myCreateDate + "//" + myRemamberDate + "//" + myNote;
        NdefMessage msg = new NdefMessage(
                new NdefRecord[]{createMimeRecord(
                        "application/eu.freemoser.myDebts2go", text.getBytes())});
        return msg;
    }

    public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord mimeRecord = new NdefRecord(
                NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
        return mimeRecord;
    }

    @Override
    public void onNdefPushComplete(NfcEvent event) {

    }
}

The Activity (receiver)

public class Beam extends Activity {
    private Long userID = null;
    private Long adressID = null;

    private DBAdapter myDb;
    //NFC
    private String myBetrag;
    private String myTitle;
    private String myContactName;
    private String myDebtDate;
    private String myCreateDate;
    private String myStatus;
    private String myRemamberDate;
    private String myNote;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    void processIntent(Intent intent) {
        myDb = new DBAdapter(this);
        myDb.open();
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        // record 0 contains the MIME type, record 1 is the AAR, if present
        String temp = new String(msg.getRecords()[0].getPayload());
        String[] arrrayTemp = temp.split("//");
        try {
            myBetrag = arrrayTemp[0];
            myTitle = arrrayTemp[1];
            myContactName = arrrayTemp[2];
            myStatus = arrrayTemp[3];
            myDebtDate = arrrayTemp[4];
            myCreateDate = arrrayTemp[5];
            // can be "NOT"
            myRemamberDate = arrrayTemp[6];
            myNote = arrrayTemp[7];
            // checkValues
            channgeStatus();
            checkIfNot();
            checkUser();
            //write to database
            write();

        } catch (Exception ex) {
            this.finish();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myDb.close();
    }

    private void checkUser() {
        //do some stuff
       ...
    }

    private void write() {

    //do some stuff
       ...

    }

    private void checkIfNot() {
    //do some stuff
       ...
    }

    private void channgeStatus() {
    //do some stuff
       ...
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

}

Solution

  • Android's MIME type matching for intent filters is case-sensitive (eventhough the MIME types themselves are not). Therefore, with Android (and also pretty much everywhere you use them) you should stick to the convention to use MIME types with lowercase letters only.

    Specifically with MIME type records received over NFC, Android will automatically convert them to all-lowercase letters to overcome the problem of case-sensitivity in intent filters. So in your example, changing the type name in the intent filter to "application/eu.freemoser.mydebts2go" should work:

    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="application/eu.freemoser.mydebts2go"/>
    </intent-filter>
    

    In addition, you should also make sure you send the MIME type in lowercase letters only:

    NdefMessage msg = new NdefMessage(
                new NdefRecord[]{createMimeRecord(
                        "application/eu.freemoser.mydebts2go", text.getBytes())});