After getting an NdefFormatable object, if tag is being pulled out of range, then NdefFormatable.format is being called, the function failure to return, causing the calling thread to "freeze".
To solve this, the unique solution found is restart the NFC resource of device. It's horrible for me, because need root privilegies.
This problem seems to happen only on some devices:
Occurs on LG L90 (LG-D410hn) with Android 5.0.2 (tested in 3 devices different)
Tested on Motorola Moto X 2013, and not happens.
My code:
ndefFormatable = NdefFormatable.get(tag);
if (ndefFormatable == null) {
return;
}
formatted = false;
final Thread thread2format = new Thread() {
#Override
public void run() {
try {
ndefFormatable.connect();
ndefFormatable.format(ndefMessage); // if communication interrupts, freeze this thread
formatted = true;
ndefFormatable.close();
} catch (IOException e) {
e.printStackTrace();
} catch (FormatException e) {
e.printStackTrace();
}
}
};
thread2format.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!formatted) {
ndefFormatable.close(); // freeze too
}
Anyone have some ideas to fix this?
Related
Recently I modified my Android code for Android-12.
I was not having android-12 related bluetooth connect permissions for my API. So I handled as below :
public List<BluetoothDevice> getHfpDevices() {
List<BluetoothDevice> hfpConnectedDevs = new ArrayList<>();
try {
if (mHeadsetService != null) {
hfpConnectedDevs = mHeadsetService.getConnectedDevices();
}
} catch (SecurityException ex) {
Log.e(TAG, "Security Exception in Android-12", ex);
}
return hfpConnectedDevs;
}
Does this catch the securityException thrown when this API is accessed ? or do I need to handle the Exception itself ?
catch (Exception ex) {
PS : I don't want to add the requestPermission flow as of now. I just need to solve the exception raised when this API is called
I'm creating an app on Android which uses a part of speech tagger from Apache (OpenNLP). The app works okay, but for some reason there is large delay when it creates the part of speech tagger from a database file (en-pos-maxent.bin). I'm using Android Studio, and the database is in app/res/raw/en-pos-maxent.bin. Here's the code that initializes the POSModel:
public POSModel setupPOSTagger() {
InputStream modelIn = null;
POSModel model = null;
try {
modelIn = getResources().openRawResource(R.raw.en_pos_maxent);
//the huge delay is mainly on the line below
model = new POSModel(modelIn);
} catch (IOException e) {
// Model loading failed, handle the error
e.printStackTrace();
} finally {
if (modelIn != null) {
try {
modelIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return model;
}
Any ideas?
This is what we see from Mopub and other ad networks:
java.io.IOException: Connection failure
com.google.android.gms.ads.identifier.AdvertisingIdClient.g(Unknown
Source)
com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown
Source)
They all seem to have the same problem.
The weird thing is that we have no problem getting the advertising id from our app whatsoever with the following source. We get the right advertising id and we have no error logs.
All the SDKs are hitting the same issue (Connection failure).
Any help appreciated.
private void getAdvertisingId(AdvertisingIdHolder receiver) {
AdvertisingIdClient.Info adInfo = null;
String id = null;
boolean isLAT = false;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(App.getCtx());
id = adInfo.getId();
isLAT = adInfo.isLimitAdTrackingEnabled();
} catch (IOException e) {
SLog.e("error", e);
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
} catch (GooglePlayServicesNotAvailableException e) {
SLog.e("error", e);
// Google Play services is not available entirely.
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
receiver.receive(id, isLAT);
}
I went through trials and errors these days on getting advertising id. Finally I got it!
The connection error can be solved if we pass in getApplicationContext() instead of the context of current activity. Below is my working code:
private void getGaid() {
new Thread(new Runnable() {
#Override
public void run() {
try {
String gaid = AdvertisingIdClient.getAdvertisingIdInfo(
getApplicationContext()).getId();
if (gaid != null) {
Log.d("DEBUG", gaid);
// gaid get!
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
}).start();
}
getGaid() can be put in onCreate(), onResume(), or onClick() of a view, as long as the thread is called by the main ui thread.
Another thing you may need is to update google play services library to latest version. As the official document here mentioned, IOException is probably caused because the old version of the service doesn't support getting AdvertisingId.
Feel free to comment if there is any other questions.
I made android application that connects to remote server and send some data.
Remote server is Windows application.
Connection method:
private void ConnectToMonitor() {
try {
s = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
This works perfectly if server is online. Application is sending data and server is receiving. But if server is offline android app. is blocked. My question is how to handle this? How to continue with application and avoid error even the server is down?
Remember to call this outside the UIThread.
Follow this tutorial. In android all connections need to be managed outside the UIThread, in the tutorial I linked you will find easy ways to post your results back to the UI (handlers, asynctasks...)
Of course we don't know if the problem is about the thread with just the given code, but it is the most usual error.
First remember to set the socket timeout :
mSocket.setSoTimeout(timeout); //in milliseconds
You can however specify different timeout for connection and for all other I/O operations through the socket:
private void connectToMonitor() {
try {
socket = new Socket();
InetAddress[] iNetAddress = InetAddress.getAllByName(SERVER_ADDRESS);
SocketAddress address = new InetSocketAddress(iNetAddress[0], TCP_SERVER_PORT);
socket.setSoTimeout(10000); //timeout for all other I/O operations, 10s for example
socket.connect(address, 20000); //timeout for attempting connection, 20 s
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Second, in Android, you should perform any network I/O in separate threads!
As an example, using regular Java Threads :
String threadName = getClass().getName() + "::connect";
new Thread(new Runnable() {
#Override
public void run() {
connectToMonitor();
}
}, threadName).start();
You can set A timeout for the socket. Use Socket.setSoTimeout method
socket.setSoTimeout(timesinmilis);
by using this your socket will throw a socket timout exception. You can catch that and do what you want
I've been working with Eclipse ADT for about 2 months. In that time, I have a small utility that allows me to select an IP Address and Port, and then send a file to that combo. The utility works as intended, but when I type in the wrong file name, the application hangs.
#Override
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}
}
I've even tried to do the following in hopes that one or the other would throw, but I am unsuccessful in both.
public void run() {
if (data != null) {
this.send(data);
} else if (this.file != null) {
if (file.exists()) {
this.send(file);
} else {
transferError = new FileNotFoundException("The specified file could not be found");
}
}try {
throw new Exception("blah blah blah");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I've jockeyed around the exception, I've added the one above, I've tried placing it in different places, and all unsuccessful. Again, I'm exceptionally new to this, and got here from basically mincing various tcp client codes. Aside of creating a way to throw the exception correctly, please help me understand why the first one isn't working and why the one you suggest is.
in your else block you aren't throwin the transferError you create.
throw transferError;
However you probably won't be able to do that because FileNotFoundException is a checked exception and the run() method doesn't declare any thrown exceptions. You probably need to find a different way to present the error to the user, like with a Toast or something.
Your second block doesn't work because you are catching the exception you throw.