title: Set a VPN connection programmatically on android 4.0
body: <p>I'm working on programmatically setting a VPN connection on android devices. I was successfully able to do so for devices using OS 2.3.5 and before (I used reflection to get to the hidden classes). But with android 4.0 they got rid of the old classes and use the VPNService class instead. </p>
  
  <p>I figured the best place to start would be to use the ToyVPN example android provided, but I'm facing a lot of challenges with it. In the example code they only needed to send the server address: </p>
  
  <pre><code>InetSocketAddress server = new InetSocketAddress(mServerAddress, Integer.parseInt(mServerPort));
  </code></pre>
  
  <p>And then created the VPN tunnel by opening the channel:</p>
  
  <pre><code>tunnel = DatagramChannel.open();
  </code></pre>
  
  <p>But in my case I need to send the server address, username and password. So far I haven't figured out how to do so. My best guess was to do something like this:</p>
  
  <pre><code>Authenticator.setDefault(new Authenticator(){
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication("user","pass".toCharArray());
          }});
  
      try {
          // Create a DatagramChannel as the VPN tunnel.
  
          tunnel = DatagramChannel.open();
  </code></pre>
  
  <p>But this did not work.
  so what I'm asking is:</p>
  
  <ol>
  <li>Is there a way, other than what's used in ToyVpn, to create a VPN connection programmatically?</li>
  <li>If not, how do I send the credentials when I want to establish a connection to the server?</li>
  </ol>
  
  <p><strong>Edit</strong></p>
  
  <p>I forgot to mention that I need to specify the VPN type (PPTP, L2TP, L2TP/IPSec PSK or L2TP/IPSec CRT).</p>