Reputation: 632
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?
Upvotes: 5
Views: 13606
Reputation: 561
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;
}
Upvotes: 1
Reputation: 874
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.
Upvotes: 5
Reputation: 86363
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.
Upvotes: 3
Reputation: 18391
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
Upvotes: 1