23

I am developing the application in which I want to use VPN network. to create VPN network in device we need to configure it from (settings-wireless and network-VPN settings) menu of device.

I want to do this many settings via code with hard-coded information.

From API level 4.0 android provides API to handle the VPN services.

To know the implementation of this methods I used the Sample project of Android ToyVPN. But in this many methods I didn't find any method to pass username and password. information which I have to connect VPN network is.

  1. VPN Server name
  2. Username
  3. Password

Using this three information I am successfully connecting to VPN network if I am configuring manually from device. But I want to do this programmatically. Here is the class file which is used to connect to VPN network. ToyVpnClient.java and ToyVpnService.java. in addition to this sample application is crashes in device.

Any help will be appreciated.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Juned
  • 6,290
  • 7
  • 45
  • 93

2 Answers2

3

The VpnService you are talking about is not what you think. It's just a framework for you to create your own VPN solution. Once established, you'll get all communication on the network (IP) layer and you can do anything you want with it, e.g., encrypt it and send it to your server - the example for this is the ToyVpn project found in /samples/android-14/ directory. You also need a your own server side code to do the decryption and the necessary routing of the traffic.

Unfortunately you can't set the built-in android VPN programmatically. The best you can do is to show a dialog to the user explaining how to set it and then pop up the system settings with the correct screen showing.

cermak.cz
  • 551
  • 2
  • 8
  • I am trying to do the same. I was able to do it in API 10 modifying the android.jar from the sdk (setting that file exactly the same than the one I got from the device using adb pull), but now I can't do the same since the new device does not contain the files VpnManager.java, PptpProfile.java, etc. (the ones you can find in android.net.vpn), so I created those classes in the android project (by copying them), but although it load into the device, it does not bind. According to what you mentioned, if I save the user name... (will continue) – gus Mar 21 '14 at 20:56
  • (continue) and password in the VPN application (the default from the device), do you know a method to programmatically call that application in order to make it run and connect to the desire network?, without even showing the default application. I am trying to do this in android 4.2.2, API 17. – gus Mar 21 '14 at 20:59
0

It is possible using internal android.jar(com.android.internal) and hidden(@hide) API. Refer the below link:

How to programmatically create a new VPN interface with Android 4.0?

Raghav Sharma
  • 780
  • 10
  • 18