Android - getNetworkInterface and needed permissions - android

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();
}

Related

Android app can not connect to internet, but other apps are fine

I have an app for android created with kotlin.
In general, it works fine. But one user reports he can not login and error is like no internet connection.
However, he says connection is fine. He can open api endpoint with the browser on same device .
What can be the reason for this?
The app has manifest record for network permissions. For other users it works fine
As you don't have given much information, I can give you some points to check:
Network permission in manifest file (that is already done).
Run time permission for Android 6.0 and above.
check if you done something like this code.
try{
//network code
}
catch(Exception e){
DialogBOXSHOW("Network error");
}
because Exception here can catch any type of error even NullPointerException

IndexOutOfRangeExeption Xamarin Live Player

I'm studying Xamarin for two weeks now and I'm following the Xamarin.University course (Consuming REST-based Web Services (XAM150)). In an exercise we have to simply check the connection of the phone. My emulator (Android 7.0) in Visual Studio 2017 does it very well. But when I try on my actual phone (Android 7.1) using Xamarin Live Player, the app will not start and the following Error appears on my phone:
"Visualization Error. Index was outside the bounds of the array.
(IndexOutOfRangeExeption)"
I've searched Google and the forum of Xamarin.University but no luck, it seems that no one else has this problem. When I debug, it stops at when it checks the connectivity. I use the Xam.Plugin.Connectivity for asking the connection and yes, it is referenced in all the parts of the project.
I've come from using this code from the site:
using Plugin.Connectivity;
public App() {
// The root page of your application
MainPage = CrossConnectivity.Current.IsConnected
? (Page) new NetworkViewPage()
: new NoNetworkPage();
}
To this to debug:
using Plugin.Connectivity;
public App() {
InitializeComponent();
try {
var isConnected = CrossConnectivity.Current.IsConnected;
MainPage = isConnected
? (Page)new NetworkViewPage()
: (Page)new NoNetworkPage();
} catch (Exception e) {
throw e;
}
//MainPage = new NetworkViewPage();
}
All this code works for my emulator, but not for my phone. I can display the different pages on my phone but not when I check for the connectivity so the error is really from the plugin and not from my pages.
Has someone any idea what I can try to make this work?
Frauke Nonnenmacher pointed out that it could be a problem with Xamarin Live Player and apparantly that is the issue. If I implement it on my phone with USB debugging, it works just fine. It didn't occur to me that Live Player could be the issue since Xamarin.University wants you to use it and I didn't find anybody on the forum there that had the same problem.
My guess is that checking the connection gives a problem with Xamarin Live Player because it works over the network, you can't use the Player if you don't have a wifi connection. So maybe something goes wrong there.
It is still a mystery why it doesn't work, but a workaround is just USB debugging.

Bluetooth creating socket error

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.

IsoDep .connect throws "e=null"

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!

android httprequest java.net.UnknownHostException

I want to make a http request with android.
is use this:
void testHTTP()
{
HttpClient httpClient = new DefaultHttpClient();
HttpUriRequest request = new HttpPost("http://www.google.com");
try {
HttpResponse response = httpClient.execute(request);
//System.out.println("response protocol version: " + response.getProtocolVersion());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I have the internet permission set in my manifest file.
I keep getting java.net.UnknownHostException: www.google.com
My final purpose ios to be able to invoke a web service. So, I initially tried the ksoap2 library and the proper url and got the same result.
I suspect I have some bad setting either in my app or in my emulator.
can you give me some pointers on where to look?
it would also bee useful a working example of something similar. then i could check and if the problem persists i would know that is my development setup at fault.
btw, i am using mac snow leopard with eclipse and android 2.2 as a setup
thanks
regards
Check the emulator log messages. There's a good chance you just aren't asking for the INTERNET permission in your app manifest.
I've had problems with the emulator where the network icon shows that I have no service, and I can't connect to the Internet. Usually, restarting the emulator fixed it. Once, I had to create a new emulator device.
Do check for clues in the emulator/device logs. You can view them in Eclipse using the LogCat view in the DDMS perspective.
BenTobin's answer pointed me to something I was missing. I had restarted the emulator several times to no avail, but noticed the emulator's Wi-fi indicator was showing 0 bars. I went into settings, turned Wi-fi mode off and then on and also Airplane Mode On then off. This resolved it for me. My bars came back.
Delete you AVD recreate new AVD and restart the eclipse
If you have permission to access the Internet is at the manifest file and the WiFi is connected so can have problems sharing the internet from your computer via WiFi, Ethernet. Try restarting internet sharing on computer. Can then will be fixed.
Restart your wifi connection if you are testing on device, its work for me.

Categories

Resources