Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending URL via NFC to be opened by browser

Tags:

android

url

nfc

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?

like image 234
user1681001 Avatar asked Jan 14 '13 21:01

user1681001


People also ask

Can you open a website with NFC?

Instead of asking someone to open their phones, pull up a browser, and type in a long url – NFC eliminates the hassle and allow users to simply tap and visit. The possibilities are endless for connecting real life items to the digital world.

Can we share data through NFC?

NFC or Near Field Communication is a very easy and fast way of transferring data between two devices. NFC can be used to send Photos, Videos, and other files, it can also be used to make payments or perform triggers.


2 Answers

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.

like image 128
thunsaker Avatar answered Nov 15 '22 10:11

thunsaker


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.

like image 41
Nils Pipenbrinck Avatar answered Nov 15 '22 10:11

Nils Pipenbrinck