0

I am trying to write an NFC tag with coordinates (latitude and longitude), in the following manner:

This is inside onCreate():

btnWriteMap.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        String latitude = lat.getText().toString();
        String longitude = lon.getText().toString();            
        urlAddress = "geo:"+latitude+","+longitude;
        TextView messageText = (TextView)findViewById(R.id.txtMessage);
        messageText.setText("Touch NFC Tag to share GEO location\n"+
            "Latitude: "+latitude+"\nLongitude: "+longitude);                   
    }
});

mNfcAdapter = NfcAdapter.getDefaultAdapter(this); 
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);

mFilters = new IntentFilter[] {
    ndef,
};

mTechLists = new String[][] { new String[] { Ndef.class.getName() },
    new String[] { NdefFormatable.class.getName() }};

The onNewIntent() method:

@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);  
    String externalType = "nfclab.com:geoService";
    NdefRecord extRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], urlAddress.getBytes());
    NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord});
    writeNdefMessageToTag(newMessage, tag);    
}

The code is a sample from a book. I have tested and the tag is indeed written with geo:lat,lon, of course with the coordinates I have in my EditTexts.

The problem occurs when I read the tag. It will just display (on the default Android Tags application) the following message:

vnd.android.nfc//ext/nfclab.com:geoService

What I want is for the Tags application to recognize that these are Google Maps coordinates and launch Maps with the coordinates. What should the externalType String contain? Do I need to use an Intent filter in my manifest?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
eddiePopescu
  • 65
  • 1
  • 9

1 Answers1

1

The Tags app displays the unrecognized NFC Forum external type nfclab.com:geoService (note that only lower-case letters should be used for external type names, see my answer here), because you stored that record type on your tag. That type is a custom type created by nfclab.com and by no means standardized. Consequently, the Tags app does not know what it should do with that record.

The standard way to store geo-coordinates on NFC tags is the geo: URI scheme. Hence, you would typically create a URI record containing your geo: URI:

String geoUri = "geo:" + latitude + "," + longitude;
NdefRecord geoUriRecord = NdefRecord.createUri(geoUri);

The Tags app will handle this type of URI and will permit you to open the geo: URI in any app that is registered for the geo: scheme (e.g. Google Maps).

Community
  • 1
  • 1
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Thank you, it works now. Do you happen to know what can I do that, after I read the tag, it doesn't only take me to the location, but also places a marker on that location (for example in Google Maps)? Because otherwise it is hard to know the precise location that was written on the tag. Later edit: I guess this can be also done with another activity, which reads the tag and launches google maps, placed in a fragment, placing also a marker with the coordinates. – eddiePopescu Apr 28 '15 at 10:49
  • 1
    See [How to show marker in Maps launched by geo uri Intent?](http://stackoverflow.com/q/3990110/2425802) – Michael Roland Apr 28 '15 at 14:54