6

I'm surprised I cannot find any info on the internet on this common situation: How can I start an internet connection? I looked at ConnectivityManager but it seems it is just for monitoring network connectivity.

PS: The phone will be rooted, so it is not a problem.

Caner
  • 49,709
  • 33
  • 153
  • 169
  • 1
    "How can I start an internet connetion?", means what you want to do? – user370305 Oct 28 '11 at 10:26
  • Let's say I want to open a wifi connection on a private network programatically. Then I want to connect to server uplaod/download data... – Caner Oct 28 '11 at 10:27
  • 3
    then you have to monitoring is wifi enable or not? if not then just open a wifi connectivity dialog and it land to wifi settings. – user370305 Oct 28 '11 at 10:30
  • I don't want user interaction, it should do everything itself. – Caner Oct 28 '11 at 10:32
  • 3
    I don't think so, without user interaction you can't enable wifi. may be for security reason. – user370305 Oct 28 '11 at 10:36
  • That's not correct, there is a method `WifiManager.setWifiEnabled()` – Caner Oct 28 '11 at 11:20
  • I am not quite sure what it is you want to do, if you want a connection to a server, you can try classes such as URL: http://docs.oracle.com/javase/1.4.2/docs/api/java/net/URL.html and its openConnection() method, or for more advanced use, the HttpClient might be of use to you: http://developer.android.com/reference/org/apache/http/client/HttpClient.html – Jave Feb 21 '12 at 10:50

4 Answers4

2

If you want simply enable WiFi you can use this method:

private boolean changeWifiState(boolean state) {
    final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifi.setWifiEnabled(state);
    return wifi.isWifiEnabled();
}

Check the permissions required for this. I think you should add these permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Yury
  • 19,498
  • 7
  • 52
  • 79
  • It's not so easy. And this functionality is version dependent. For instance, you can look here how to do this: http://stackoverflow.com/questions/3644144/how-to-disable-mobile-data-on-android – Yury Feb 21 '12 at 14:41
  • And you should know that Wifi and data connection are usually cannot be enables simultaneously. – Yury Feb 21 '12 at 14:42
  • 1
    +1 thank you for useful answer. However this was not exactly what I needed(See my answer) – Caner Feb 22 '12 at 12:16
1

You can indeed turn wifi on or off (see also this article) but there is no guarantee that if wifi is turned on there will be an internet connection.

The ConnectivityManager only allows you to inspect the current connectivity state. You cannot use it to enable a connection. Also the ConnectivityManager has no knowledge if an active network connection is an internet connection, but it is easy to check this yourself (see this post for example).

Community
  • 1
  • 1
THelper
  • 14,136
  • 6
  • 59
  • 95
1

I found the following solution:

1) Add preferred wifi/APN(Access Point Name) configuration to phone settings
2) Enable Wifi/APN
3) Connection will be established automatically

Wifi configuration is straightforward and the following page shows how it could be done:

How to programmatically create and read WEP/EAP WiFi configurations in Android?

APN configuration is tricky. Android has some hidden API's that allow you to access/modify these settings, and you need to use Reflection to do this. This might of course break with future versions of Android. The following page explains how to access/modify those settings:

http://blogs.msdn.com/b/zhengpei/archive/2009/10/13/managing-apn-data-in-google-android.aspx

Community
  • 1
  • 1
Caner
  • 49,709
  • 33
  • 153
  • 169
0

Sockets are what you need. To get your basic info on them, read it here: http://en.wikipedia.org/wiki/Internet_socket You don't have to read it all, but I think its important you read atleast all of the "implementation issues" to get familiar with the socket methods. Those methods are, of course, implemented differently for each platform (Windows, Android...) but they usually do the same everywhere. So once you understand what each one does, you can work with sockets in any platform with ease.

This diagram (From wiki) helps to demonstrate the sockets usage: enter image description here

You are in the client side. So just create a socket and call the connect method for the IP address of your server.

Personally, I have never used sockets in Android development. But I think you should use this class: http://developer.android.com/reference/java/net/Socket.html

Jong
  • 8,813
  • 3
  • 30
  • 64
  • Sockets may be a solution, but they are rather low level. I might be easier to setup a [ftp](http://stackoverflow.com/questions/1567601/android-ftp-library) or [http connection](http://developer.android.com/reference/java/net/HttpURLConnection.html) if the server the OP is trying to connect allows it. – THelper Oct 28 '11 at 12:36
  • Sockets are the basic; It's better to understand them, then use a higher level class which wraps the socket in itself. Besides, these classes are wrapping sockets to be used in very specific protocols. Since we don't know what protocol the original poster needs, we can't tell him to use any but the basic socket. – Jong Oct 28 '11 at 12:38
  • But this doesn't answer the question. How do I establish the internet connection socket will use in the first place? – Caner Oct 29 '11 at 02:04
  • What do you mean by establishing internet connection, if not creating a socket and connecting it to a server or something? – Jong Oct 29 '11 at 15:51
  • A phone can connect to internet in many ways. It can use Roaming, Wifi, can be connected to a PC with cable etc.. I want to be able to control this. – Caner Nov 01 '11 at 12:08
  • You mean, control the method the device has internet access? – Jong Nov 01 '11 at 12:36