This sounds weird, I know.
I am developing an Android app using NFC, the app reads a tag from a samrtcard using IsoDep APDU commands, everything usually works but sometimes it fails, and keep on failing in the followed executions, when it fails it occurs when connecting the tag, the tag has its "mConnectedValue = -1", which means no technology connected but the followed code is executed
card = IsoDep.get(tag);
card.setTimeout(20000);
card.connect();
connect() throws ans excepcion, the exception is catch by this code:
} catch (IOException e) {
...
}
If "e" is inspected with eclipse shows e=null, but the code inside the catch is executed, anyone can explain why e=null? The code above is inside a loop, and followed executions keep on returning that "exception" e=null.
Thank you very much!
Fixed, well, actually the "e=null" still the same, sometimes it throws "IOException e = null" and sometimes just "e= null", in both cases the error is caught by the same code, it might be some kind of bug.
I also solved the connection problem, it was device's fault, the chip was not powerful enough to supply energy to the smartcard, for reading and writing was ok, but when the card make some operations it isn't enough, just moving the phone a little bit upwards solved the problem.
Thanks!
Related
I have written an Android application in which I allow the user to select an image from the gallery and post it to an Internet server as a Base64 encoded String using HTTP REST-ful web-services upon click of a button.
When the server is up/ running and the phone has a good 3G connection, it works fine. However, if the phone has a bad connection or if the server is down for some reason, then the Android application crashes altogether (screen goes black and application quits).
Although I’ve given a catch block to catch any IO/ other exceptions, the catch block code doesn’t execute if there is an error while executing connection.connect () ; I have tried catching Throwable as well to no avail – the code in the catch block never executes and the application itself stops working.
Can somebody please help me in figuring out how to catch these exceptions which are occurring when the server is not reachable or if there is an error while transmitting the image?
if you check (ex as Exception) instead of IOException, maybe it helps
I try to create BluetoothServerSocket on Samsung Galaxy Gio.
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothServerSocket tmp = null;
try
{
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(NAME_INSECURE, MY_UUID_INSECURE);
}
catch (IOException e)
{
}
mmServerSocket = tmp;
When I no longer need to use a socket I just close it.
public void cancel()
{
try
{
mmServerSocket.close();
}
catch (IOException e)
{
}
}
In both cases no exceptions are throws. So my problem is. When i try to use 1st pease of code again( without exit from app) Log cat show me exception :
07-07 18:27:44.239: D/BluetoothSocket(13672): create BluetoothSocket: type = 1, fd =-1,
uuid = [null], port = 25
07-07 18:27:44.339: E/BLZ20_WRAPPER(13672): ##### ERROR : __listen_prot_rfcomm: failed
with reason 1#####
That happens till then i don't reboot my phone or TurnOFF\wait\TurnOn my bluetooth. So i think the problem is that BluetoothServerSocket create sockets but he don't close it. Maybe my preconceptions is not right so i want to help.
After a bit of searching, I found this thread whose poster appears to get the same exception (Although it's in Italian, so I don't really understand...). If you scroll down a little, you'll see a java exception thrown for this native exception (__listen_prot_rfcomm: failed with reason 1). The java exception is java.io.IOException: Bad file number.
You can find this problem in many threads online (Android Bluetooth IOException bad file number, Bluetooth failed to get port number). From these 2 we can learn that this problem is device specific. Some devices seem to keep the file descriptor of the socket alive, even after you close it with BluetoothServerSocket.close(), so you can't recreate any sockets using the same settings.
The solution will depend on your application audience:
If you are not planning to publish it in the Play Store/Any other market, you could call BluetoothAdapter.disable(), and then re-enable it. This is very bad user experience, as an application shouldn't disable the bluetooth without asking the users first. But it will solve your problem, because all bluetooth file descriptors will be disposed automatically. So if the only user is you, it's a possible solution.
If you do plan on publishing this, you should find a real solution... It might not be easy, but this is indeed a major problem and it also seems that many devices are affected by it, so you can't publish your app as long as this problem persists.
I have a project that connects to a device over Bluetooth. It used to work fairly reliably, but now it fails the BluetoothSocket.connect() call every time. (Well, I got it to connect once during the thousands of attempts over a 4 hour period.) Most of the code has been taken from the standard sample chat code in the API, with the exception of the common modification in getting the BluetoothSocket device itself:
Method m = device.getClass().getMethod(
"createRfcommSocket", new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
Here's the method of interest, which gets run once a BluetoothSocket is obtained:
public void run() {
setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
mmSocket.connect();
} catch (Exception e) {
Log.e(TAG, "Connection to " + mmDevice.getName() + " at "
+ mmDevice.getAddress() + " failed:" + e.getMessage());
// Close the socket
try {
mmSocket.close();
} catch (Exception e2) {
Log.e(TAG, "unable to close() " + mSocketType
+ " socket during connection failure", e2);
}
connectionFailed(e.getMessage());
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice, mSocketType);
}
The relevant log entry (printed when the exception is caught while calling connect()) is this:
11-30 10:23:51.685: E/BluetoothChatService(2870): Connection to
ZYNO-700091 at 00:06:66:42:8E:01 failed:read failed, socket might
closed, read ret: -1
This error used to come up once in a while. I have an aggressive reconnect system - it basically hammers the connection over and over until it connects, and if it were ever to disconnect, it starts hammering it again. So, it kills the connection thread and starts from scratch constantly. I had considered that there might be an issue there - maybe a multithreading one, or maybe in handling the socket cleanup/initialization. However, if that were the case, I'd still expect the first connection attempt to succeed, since that system doesn't kick in until there's a failed connection attempt.
I looked into the source code throwing the exception. The issue seems to be that the underlying InputStream has no data. Of course, that's not really an answer, just a step towards it. Why would the stream have no data?
I'm trying to keep an open mind about the potential issue. Am I getting the BluetoothSocket properly? The fact that it was once an intermittent issue and is now nearly constant makes me suspect multithreading, but that's a relatively simple topic in Java compared to C++ - hard to screw up if you know what you're doing. Plus, the majority of this code (in particular, the parts dealing with synchronizing the threads) is straight out of the sample code.
The device on the other end is an embedded Bluetooth device, so there's not much hope of debugging the problem from that end.
UPDATE ===========================
It occurred to me that it might be due to an OS upgrade (I'm running on Galaxy Nexus phones - I have several to test with). So I unpacked a new phone with 4.0.4 and it worked! So then went back and tested on the two original test phones, both running 4.2, expecting the failure I've been seeing all this time. Strangely, now it works on those phones too. I'd like to say I did something to make this work again, but I didn't. I'm still mystified, and now also suspicious that this thing is going to work when I really need it to.
I wonder if there's a possibility that somehow connecting using 4.0.4 could have properly set the state of the server module, making it receptive to the 4.2 devices? Just a shot in the dark, I suppose...
UPDATE 2 ===========================
I've found that unpairing and re-pairing will allow the devices to connect. It's a workaround, but it's better than nothing.
Jellybean has a completely different Bluetooth stack, so version differences could certainly be triggering something, but that in itself wouldn't explain why it stays working or not-working after connecting with an older device. Could it be to do with pairing? If it happens again, try unpairing from the device and pairing again.
I know this is kind of an old question. But as I was not able to find any solution on the web, here is a workaround I recently have created: IOException: read failed, socket might closed - Bluetooth on Android 4.3
In my case it was due to bad UUID in createRfcommSocketToServiceRecord() function.
I want to connect to SPP serial profile in raspberry pi 3 and I used this UUID:
private static final UUID MY_UUID_SECURE =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
I found somewhere in android documents' page for SPP.
I had the same problem while connecting to the arduino via bluetooth module.The problem only arised while connecting with arduino as it connected smoothly with another android phone bluetooth.
What worked for me was changing the UUID string..
I have a project that connects to a device over Bluetooth. It used to work fairly reliably, but now it fails the BluetoothSocket.connect() call every time. (Well, I got it to connect once during the thousands of attempts over a 4 hour period.) Most of the code has been taken from the standard sample chat code in the API, with the exception of the common modification in getting the BluetoothSocket device itself:
Method m = device.getClass().getMethod(
"createRfcommSocket", new Class[] { int.class });
tmp = (BluetoothSocket) m.invoke(device, Integer.valueOf(1));
Here's the method of interest, which gets run once a BluetoothSocket is obtained:
public void run() {
setName("ConnectThread" + mSocketType);
// Always cancel discovery because it will slow down a connection
mAdapter.cancelDiscovery();
// Make a connection to the BluetoothSocket
try {
mmSocket.connect();
} catch (Exception e) {
Log.e(TAG, "Connection to " + mmDevice.getName() + " at "
+ mmDevice.getAddress() + " failed:" + e.getMessage());
// Close the socket
try {
mmSocket.close();
} catch (Exception e2) {
Log.e(TAG, "unable to close() " + mSocketType
+ " socket during connection failure", e2);
}
connectionFailed(e.getMessage());
return;
}
// Reset the ConnectThread because we're done
synchronized (BluetoothChatService.this) {
mConnectThread = null;
}
// Start the connected thread
connected(mmSocket, mmDevice, mSocketType);
}
The relevant log entry (printed when the exception is caught while calling connect()) is this:
11-30 10:23:51.685: E/BluetoothChatService(2870): Connection to
ZYNO-700091 at 00:06:66:42:8E:01 failed:read failed, socket might
closed, read ret: -1
This error used to come up once in a while. I have an aggressive reconnect system - it basically hammers the connection over and over until it connects, and if it were ever to disconnect, it starts hammering it again. So, it kills the connection thread and starts from scratch constantly. I had considered that there might be an issue there - maybe a multithreading one, or maybe in handling the socket cleanup/initialization. However, if that were the case, I'd still expect the first connection attempt to succeed, since that system doesn't kick in until there's a failed connection attempt.
I looked into the source code throwing the exception. The issue seems to be that the underlying InputStream has no data. Of course, that's not really an answer, just a step towards it. Why would the stream have no data?
I'm trying to keep an open mind about the potential issue. Am I getting the BluetoothSocket properly? The fact that it was once an intermittent issue and is now nearly constant makes me suspect multithreading, but that's a relatively simple topic in Java compared to C++ - hard to screw up if you know what you're doing. Plus, the majority of this code (in particular, the parts dealing with synchronizing the threads) is straight out of the sample code.
The device on the other end is an embedded Bluetooth device, so there's not much hope of debugging the problem from that end.
UPDATE ===========================
It occurred to me that it might be due to an OS upgrade (I'm running on Galaxy Nexus phones - I have several to test with). So I unpacked a new phone with 4.0.4 and it worked! So then went back and tested on the two original test phones, both running 4.2, expecting the failure I've been seeing all this time. Strangely, now it works on those phones too. I'd like to say I did something to make this work again, but I didn't. I'm still mystified, and now also suspicious that this thing is going to work when I really need it to.
I wonder if there's a possibility that somehow connecting using 4.0.4 could have properly set the state of the server module, making it receptive to the 4.2 devices? Just a shot in the dark, I suppose...
UPDATE 2 ===========================
I've found that unpairing and re-pairing will allow the devices to connect. It's a workaround, but it's better than nothing.
Jellybean has a completely different Bluetooth stack, so version differences could certainly be triggering something, but that in itself wouldn't explain why it stays working or not-working after connecting with an older device. Could it be to do with pairing? If it happens again, try unpairing from the device and pairing again.
I know this is kind of an old question. But as I was not able to find any solution on the web, here is a workaround I recently have created: IOException: read failed, socket might closed - Bluetooth on Android 4.3
In my case it was due to bad UUID in createRfcommSocketToServiceRecord() function.
I want to connect to SPP serial profile in raspberry pi 3 and I used this UUID:
private static final UUID MY_UUID_SECURE =
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
I found somewhere in android documents' page for SPP.
I had the same problem while connecting to the arduino via bluetooth module.The problem only arised while connecting with arduino as it connected smoothly with another android phone bluetooth.
What worked for me was changing the UUID string..
got a strange problem here.
I have a function which reads the NetworkInterfaces and gets me the WiFi-Interface name.
This worked properly 2 weeks ago with the only permission "android.permission.ACCESS_NETWORK_STATE".
This week i mainly tested with 3G and did some minor changes to the overall code and today i noticed 'NetworkInterface.getNetworkInterfaces()' doesnt work anymore, giving me a "permission denied" exception. Few secs later my dear friend google tells me i need INTERNET permission...
-> I never ever used INTERNET permission in this project ???
So what could be wrong? Any way to get the NetworkInterfaces without using the internet permissions? It works again if i add it but idont really want to use it just for that.
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}