5

I want to be able to send a URL via NFC so that it can be opened by the receiving phone's browser on android phones. Is this a possible action?

user1681001
  • 632
  • 2
  • 7
  • 7
  • Only if the application ("browser") is configured/registered to do this. If you write your own browser you can support it, dont know about the standard android browser or chrome – Tobrun Jan 14 '13 at 21:39
  • Can't you already send links over NFC on default android broswers? – user1681001 Jan 14 '13 at 21:44
  • Yes, but I believe that this is the case when the browser is active. In your situation you want a registered application to open when the tag is read. I dont believe that the default browser supports this. I will have a look – Tobrun Jan 14 '13 at 21:50

4 Answers4

4

After scratching my head for a few hours and browsing numerous posts. I found that all I needed to do was implement the NdefCallbacks and set the uri in the "createNdefMessage" method.

First implement the NdefCallbacks

public class MyActivity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {

Add the unimplemented methods

@Override
public NdefMessage createNdefMessage(NfcEvent event) {  
    ...
}
...
@Override
public void onNdefPushComplete(NfcEvent arg0) {
    ...
}

Create an NFC Adapter at the top of your activity

NfcAdapter mNfcAdapter;

Then setup the NFC Adapter in the onCreate method

mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(mNfcAdapter != null) {
    // Register callback to set NDEF message
    mNfcAdapter.setNdefPushMessageCallback(this, this);

    // Register callback to listen for message-sent success
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
} else {
    Log.i("LinkDetails", "NFC is not available on this device");
}

Then put the following in your createNdefMessage method:

return new NdefMessage(new NdefRecord[] {
    NdefRecord.createUri(YOUR_URL_HERE)
});

Tap away, Android will handle the rest.

Thanks to the fine folks at TAPPED for their guide, which got me about 3/4ths of the way to a successful NFC implementation.

thunsaker
  • 854
  • 5
  • 11
  • 2
    Don't forget to add the NFC permission to your application manifest: ```` - https://developer.android.com/reference/android/Manifest.permission.html#NFC – Mike Jarema Jun 20 '13 at 14:54
3

Heck yes.

Put an NDEF formatted record containing the URL of the website onto the tag and you're done.

The Android SDK is FULL of examples.

Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
  • 1
    Hi, would it be possible for you provide an example of this? I want to send an arbitrary url from 1 phone, wwwm.google.com for example – user1681001 Jan 14 '13 at 22:13
1

Code blocks are from Android reference:

This is what you want to do, but you cant change the intent filters of the default Android browser.

Reading NDEF data from an NFC tag is handled with the tag dispatch system, which analyzes discovered NFC tags, appropriately categorizes the data, and starts an application that is interested in the categorized data. An application that wants to handle the scanned NFC tag can declare an intent filter and request to handle the data.

These features are supported if apps are active supporting beam functionality:

The Android Beam™ feature allows a device to push an NDEF message onto another device by physically tapping the devices together. This interaction provides an easier way to send data than other wireless technologies like Bluetooth, because with NFC, no manual device discovery or pairing is required. The connection is automatically started when two devices come into range. Android Beam is available through a set of NFC APIs, so any application can transmit information between devices. For example, the Contacts, Browser, and YouTube applications use Android Beam to share contacts, web pages, and videos with other devices.

Do you see the difference between the two?

Hope this helps

Tobrun
  • 18,291
  • 10
  • 66
  • 81
1

Use these code, active android NFC Beam.

And most importantly is the "http://" prefix. I believe most of users tracked to this question stuck in this .

@Override
public NdefMessage createNdefMessage(NfcEvent event) {
    NdefMessage msg = new NdefMessage(new NdefRecord[] { 
        NdefRecord.createUri("http://www.google.com")
    });
    return msg;
}
Shu Zhang
  • 521
  • 12
  • 20