Check if NFC tag is near - android

Is it possible to force android to check if an NFC tag is near? I'm only able to read the tag when android detects it, I would like to force it to check if a tag is near at a specific moment

What you want to do is in general not possible. However if you can live with a dirty hack the following will work (thanks to unspecified behaviour):
First disable the reader-mode of all supported tag types. This brings the NFC subsystem into a clean state, e.g. it makes sure that the NFC controller will have no connection to the tag.
Once done restore the reader-mode again. If a tag is present at that moment you will get the usual discovery action as an intent. It may take a second or two though.
Control of the reader-mode is possible using NfcAdapter.enableReaderMode and NfcAdapter.disableReaderMode

I figured out a bit of a hack for this that works (for me, at least!)
First, when you initially detect the tag via the android.nfc.action.NDEF_DISCOVERED, get the tag as a field in your class and start a timer (this is in C#/Xamarin but the same should apply for Java):
_tag = Intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;
ReaderTimer = new Timer(2000);
ReaderTimer.Elapsed += TimerElapsed;
ReaderTimer.Start();
Now, every 2 seconds this will fire. It will attempt to reconnect to the tag. If it can't the tag is gone.:
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
if (_tag == null)
{
return;
}
Ndef ndef = Ndef.Get(_tag);
if (ndef == null)
{
// NDEF is not supported by this Tag.
return;
}
if (!ndef.IsConnected)
{
try
{
ndef.Close();
ndef.Connect();
}
catch (Exception ex)
{
// could not reconnect
// implies tag is not in proximity
//do whatever needs to be done when NFC is disconnected
ReaderTimer.Stop();
}
}
}
I've tested this with API 14.
Unfortunately when the screen is turned off ndef.Connect() fails, so that is registered as a disconnect.

Try this code and it works ! It will continuously check if the NFC tag is near to the phone or not.
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
readFromIntent(intent);
if(NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())){
myTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
checkNFCStatus();
handler.postDelayed(this,1000);
}
}, 1000);
}
}
public void checkNFCStatus(){
try {
if(myTag != null) {
Ndef ndefTag = Ndef.get(myTag);
ndefTag.connect();
if (ndefTag.isConnected()) {
Log.d("network", "NFC connected");
} else {
Log.d("network", "NFC disconnected");
}
ndefTag.close();
}
} catch (IOException e) {
e.printStackTrace();
Log.d("network", "NFC disconnected");
}
}
//If the connection is not closed, an exception will be thrown

Related

Catch continuous pressing of the NFC card to NFC reader ("LongPress")

This is how usually an NFC tag is detected in my app:
protected void onNewIntent(Intent intent) {
if (intent.getAction().equals(NfcAdapter.ACTION_TAG_DISCOVERED)) {
Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
...
}
}
Now I need to also listen if an NFC tag is held close to the reader for a long time (about 3 seconds). In that case I want to do something else (similar to distinguishing between a normal press and a longpress on a view). Is this possible?
The method
isConnected()
tells you whether the connection to the tag ist still alive. If you check the connection periodically, you can detect a long connection.
The concept behind NFC is to quickly exchange small amounts of data between a tag and an NFC device (or two NFC devices) and not to detect the duration of an interaction. Consequently, there is no dedicated event that lets you distinguish between short and slightly longer interactions.
As corvairjo wrote, you could connect to the tag and check if the tag is still connected after a certain amount of time (e.g. 3 seconds). However, you can only measure the time from the point where your app is notified about the tag (i.e. after onNewIntent() was invoked). You can't measure the time that Android needed to notify your app after the user actually scanned the tag.
Note that isConnected() on its own is not reliable for all device/tag combinations. The most reliable way to test if a tag is still present would be to send a valid command to the tag and check if you get a response:
new AsyncTask<Tag, Void, Boolean>() {
protected Boolean doInBackground(Tag... tags) {
try {
Thread.sleep(3000);
// test if tag is still connected
Ndef ndef = Ndef.get(tags[0]);
if (ndef != null) {
try {
ndef.connect();
ndef.getNdefMessage();
} finally {
ndef.close();
}
return Boolean.TRUE;
}
} catch (Exception e) {
}
return Boolean.FALSE;
}
#Override
protected void onPostExecute(Boolean result) {
if (result) {
// "long press" event
}
}
}.execute(tag);
If your tag supports NDEF (the Ndef tag technology) you could simply query the tag for its NDEF message using Ndef.getNdefMessage() (see above). If your tag does not support NDEF, you would first need to find out what commands your tag supports and then send such a command using the proper tag technology.
E.g. if your tag is an MIFARE Ultralight or NTAG tag (or any NFC Forum Type 2 tag), you could use:
// test if tag is still connected
NfcA nfca = NfcA.get(tags[0]);
if (nfca != null) {
try {
nfca.connect();
byte[] response = nfca.transceive(new byte[] { (byte)0x30, (byte)0x00 });
if ((response != null) && (response.length > 0))
return Boolean.TRUE;
}
} finally {
ndef.close();
}
}

BluetoothServerSocket hangs on accept

I've seen many other posts regarding this specific issue, all with results that did not help me or were relevant to my case.
Here is my problem, I'm trying to setup a bluetooth piconet, with one node as a server and 7 as clients, each given a number as a location representative starting from 0 ( the server) and going to 7 (the clients). Currently I'm trying to get this to work for just two devices, the server and one client. And I assume that they're already paired. In the following code uuid is
private UUID uuid=UUID.fromString("0000111f-0000-1000-8000-00805f9b34fb");
Here is my thread which accepts incoming connections, ad is just the bluetooth adapter
private class BTServerThread implements Runnable{
#Override
public void run() {
try {
if (ad != null) {
if (ad.isEnabled()) {
BluetoothServerSocket btss=ad.listenUsingInsecureRfcommWithServiceRecord("MyApp",uuid);
Location=0;
Integer loc=1;
Log.wtf("Server","Searching");
while(loc<=7){
Thread t=new Thread(new BTServerHandler(btss.accept(),loc));
t.start();
Log.wtf("BTS","Found");
loc++;
}
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
And here is my client thread which attempts to connect
private class BTClientThread implements Runnable{
#Override
public void run() {
try {
if (ad != null) {
if (ad.isEnabled()) {
Set<BluetoothDevice> bondedDevices = ad.getBondedDevices();
if (bondedDevices.size() > 0) {
Iterator<BluetoothDevice> iter = bondedDevices.iterator();
BluetoothDevice device = iter.next();
Log.wtf("dev", device.getName());
BluetoothSocket clientsocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
clientsocket.connect();
Log.wtf("Connected",device.getName());
}
Log.e("error", "No appropriate paired devices.");
} else {
Log.e("error", "Bluetooth is disabled.");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Hold your breath, here comes the weird part, the call btss.accept() hangs forever (Doesn't even return once), while at the same time, the client device connects somehow. When I call
BluetoothSocket clientsocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
clientsocket.connect()
This pops the toast on the phone which is hanging on accept saying "DeviceName connected" then after a while it pops another toast saying "DeviceName disconnected" on its own, without me doing ANYTHING, and at the same time the serverphone is still hanging on accept.
Here are my questions, why is it hanging on accept when the toast popped saying connected? And how could it possibly connect when the other phone is still listening for a connection?
Thanks for the help.
As it turns out, using that specific UUID caused a problem from some reason I still don't understand? After trying many random things, I finally decided to try another random UUID and that magically caused it to work.
Here is my new UUID
private UUID uuid = UUID.fromString("56e8a14a-80b3-11e5-8bcf-feff819cdc9f");

Check programmatically if device has NFC reader

Is there a way to check at run time whether a device has an NFC reader? My app uses NFC to perform a task, but if no reader is present, it can perform the same task by using a button.
Hope This works for you
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
//Yes NFC available
}else if(adapter != null && !adapter.isEnabled()){
//NFC is not enabled.Need to enable by the user.
}else{
//NFC is not supported
}
The simplest way to check if an Android device has NFC functionality is to check for the system feature PackageManager.FEATURE_NFC ("android.hardware.nfc"):
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
// device has NFC functionality
}
However, there exist devices (at least one of Sony's first Android NFC smartphones has this issue) that do not properly report the FEATURE_NFC. (That's those devices that do not allow you to install apps that require NFC functionality through Play Store does such a check for apps that require NFC.)
Therefore, the more reliable solution is the one described by Sainath Patwary karnate. To check if a device has NFC functionality (or rather if a device has a running NFC service), you can use:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (nfcAdapter != null) {
// device has NFC functionality
}
If you also want to check if the user enabled NFC on their device, you may use the NfcAdapter's isEnabled() method. But be warned that it's not always as easy as described by Sainath Patwary karnate. Particularly on Android 4.0.*, the isEnabled() method sometimes throws undocumented exceptions when the NFC service had crashed before, so you might want to catch those exceptions. Moreover, on Android >= 2.3.4 and < 4.1 (I could not reproduce the problem on later versions but that does not mean it is not there!), the first call to isEnabled() after the NFC service had been stopped or crashed always returned false, so it is advisable to always ignore the result of the first call of isEnabled().
if (nfcAdapter != null) {
try {
nfcAdapter.isEnabled();
} catch (Exception e) {}
bool isEnabled = false;
try {
isEnabled = nfcAdapter.isEnabled();
} catch (Exception e) {}
if (isEnabled) {
// NFC functionality is available and enabled
}
}
Here's my function that I use for detecting NFC presence.
public static boolean deviceHasNfc() {
// Is NFC adapter present (whether enabled or not)
NfcManager nfcMgr = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
if (manager != null) {
NfcAdapter adapter = manager.getDefaultAdapter();
return adapter != null;
}
return false;
}
As stated in #Sainath's answer you can also detect if the NFC is enabled using adapter.isEnabled()
For those of you doing Kotlin here is a quick enabled check extension following the rules posted above
fun Context.isNfcEnabled(): Boolean {
val nfcAdapter = NfcAdapter.getDefaultAdapter(this)
if (nfcAdapter != null) {
return try {
nfcAdapter.isEnabled
} catch (exp: Exception) {
// Double try this as there are times it will fail first time
try {
nfcAdapter.isEnabled
} catch (exp: Exception) {
false
}
}
}
return false
}

Android to detect possibilities of no data connection

I have an application which needs to communicate with the server at some random interval through GPRS or EDGE.. But there are few possibilities at which internet cannot be accessed by the application when the user is in call or deactivated etc. At these time i have two scenario's recoverable and non recoverable.
Recoverable scenarios
On phone call ( User will hang up and data connection will be active
again)
No Signal (Sometimes signal may drop and the phone will get
signal again)
Non Recoverable Scenarios
Flight mode
Deactivating Data Connection
When its recoverable i can try again for the connection after some defined interval. And during non recoverable i have to alert user. For instance if the user deactivates data connection or enables flight mode i have to alert the user.
EDIT:I can able to detect flight mode through one of the intents. I couldn't able to find for others.
The below code return if valid connections are available
public boolean isConnectionsAvailable() {
boolean lRet = false;
try{
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info= conMgr.getActiveNetworkInfo();
if(info != null && info.isConnected()) {
lRet = true ;
}else{
lRet = false ;
}
}catch (Exception e) {
Log.d("Connection Error", e.toString());
lRet = false ;
}
return lRet;
}
After this, if you have low signal strength then you make a HTTP request by setting relevant time out to it. If timeout happened give relevant alert msg to user as below
public void serverCall(String pURL){
if (isConnectionsAvailable()){
// Call server by setting proper timeout
}
}
Edit:
To check the Airplane mode status:
private static boolean isAirplaneModeOn(Context context) {
return Settings.System.getInt(context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
}
You could try surrounding your method with try and catch. If the method fails because it cannot connect to your server for whatever reason you could call postDelayed from a Handler and re-run your method again in a pre-determined length of time.
Handler mHandler = new Handler();
Runnable yourMethodRunnable = new Runnable(){
#Override
public void run(){
yourMethod();
}
};
private void yourMethod(){
try{
// talk to server
} catch (InCallException e) {
mHandler.postDelayed(yourMethodRunnable, delay)
} catch (NoSignalException e) {
// etc...
} catch (OtherException e) {
// etc...
}
}
The exceptions are just examples, and likely don't exist, get the exceptions that you want to catch either from the Android Developer Docs, or by looking at the output from LogCat when you re-enact each time that the connection to the server would fail.

How can I enable NFC reader via API?

There is any way I can enable Android NFC reader using API?
So apparently there is no way to enable the NFC from the API, even though Google does so within their source code (see below).
If you look at a line from the API for NfcAdapter.isEnabled():
Return true if this NFC Adapter has
any features enabled.
Application may use this as a helper
to suggest that the user should turn
on NFC in Settings.
If this method returns false, the NFC
hardware is guaranteed not to generate
or respond to any NFC transactions.
It looks like there is no way to do it within the API. Bummer. Your best bet is a dialog to inform the user they need to enable it in the settings, and perhaps launch a settings intent.
EDIT: The following is from the source, but it looks like they didn't allow the user to implement the methods in the API (I'm confused about this).
I found this from the android source code to help enable and disable the adapter.
Relevant source:
public boolean onPreferenceChange(Preference preference,
Object value) {
// Turn NFC on/off
final boolean desiredState = (Boolean) value;
mCheckbox.setEnabled(false);
// Start async update of the NFC adapter state, as the API is
// unfortunately blocking...
new Thread("toggleNFC") {
public void run() {
Log.d(TAG, "Setting NFC enabled state to: "
+ desiredState);
boolean success = false;
if (desiredState) {
success = mNfcAdapter.enable();
} else {
success = mNfcAdapter.disable();
}
if (success) {
Log.d(TAG,
"Successfully changed NFC enabled state to "
+ desiredState);
mHandler.post(new Runnable() {
public void run() {
handleNfcStateChanged(desiredState);
}
});
} else {
Log.w(TAG, "Error setting NFC enabled state to "
+ desiredState);
mHandler.post(new Runnable() {
public void run() {
mCheckbox.setEnabled(true);
mCheckbox
.setSummary(R.string.nfc_toggle_error);
}
});
}
}
}.start();
return false;
}
I got it working through reflection
This code works on API 15, haven't checked it against other verions yet
public boolean changeNfcEnabled(Context context, boolean enabled) {
// Turn NFC on/off
final boolean desiredState = enabled;
mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
if (mNfcAdapter == null) {
// NFC is not supported
return false;
}
new Thread("toggleNFC") {
public void run() {
Log.d(TAG, "Setting NFC enabled state to: " + desiredState);
boolean success = false;
Class<?> NfcManagerClass;
Method setNfcEnabled, setNfcDisabled;
boolean Nfc;
if (desiredState) {
try {
NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
setNfcEnabled = NfcManagerClass.getDeclaredMethod("enable");
setNfcEnabled.setAccessible(true);
Nfc = (Boolean) setNfcEnabled.invoke(mNfcAdapter);
success = Nfc;
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
} else {
try {
NfcManagerClass = Class.forName(mNfcAdapter.getClass().getName());
setNfcDisabled = NfcManagerClass.getDeclaredMethod("disable");
setNfcDisabled.setAccessible(true);
Nfc = (Boolean) setNfcDisabled.invoke(mNfcAdapter);
success = Nfc;
} catch (ClassNotFoundException e) {
} catch (NoSuchMethodException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
if (success) {
Log.d(TAG, "Successfully changed NFC enabled state to "+ desiredState);
} else {
Log.w(TAG, "Error setting NFC enabled state to "+ desiredState);
}
}
}.start();
return false;
}//end method
This requires 2 permissions though, put them in the manifest:
<!-- change NFC status toggle -->
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
The NFC button's state switches accordingly when the code is used, so there are no issues when doing it manually in the seetings menu.
If you can see the NfcService Application Source Code, there is a Interface file INfcAdapter.aidl. In the file two API's are there namely "boolean enable()" and "boolean disable()". You can directly use this API's to enable and disable NfcService through an android application. But the trick over here is that you can not compile the code using SDK provided by the Android. You have to compile the application using the a makefile. I have successfully build a application.
I hope this forum would be help you to resolve this issue as well to get the clear understanding on the NFC power on/off API barries.
http://ranjithdroid.blogspot.com/2015/11/turn-onoff-android-nfc-by.html

Categories

Resources