Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android VPN Service Builder.establish() returns null

Tags:

java

android

vpn

I am trying to make a VPN Service work on android.
To do this, I think I have to use the Builder class provided in the VpnService class, which is provided by the android SDK. Now when I try to do this, the method

builder.establish()

returns null. Here is the full method I wrote;

// If theInterface still exists from last time, reuse it, it has the same parameters.
if (theInterface != null) {
    return;
}
// configure the VPN settings.
Builder builder = new Builder();
builder.setMtu(1500);
builder.addAddress("10.0.2.0", 32);
builder.addRoute("0.0.0.0", 0);
try {
    theInterface.close();
} catch (Exception e) {
    // Ignore
}
theInterface = builder.establish();
Log.i(TAG + "IMPORTANT", builder.toString());

The code crashes at:

Log.i(TAG + "IMPORTANT", builder.toString());

When debugging, the debugger says 'theInterface = null' throughout the entire execution of the program.

EDIT:
I have looked trough the Builder class which is located in android.net.VpnService
In this class, it seems that there is a hidden class (IConnectivityManager), maybe my problem has something to do with this...
Here is the piece of code I'm talking about.

/**
 * Use IConnectivityManager since those methods are hidden and not
 * available in ConnectivityManager.
 */
private static IConnectivityManager getService() {
    return IConnectivityManager.Stub.asInterface(
            ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
}
like image 812
Targa Avatar asked Mar 23 '15 22:03

Targa


1 Answers

as VpnService.Builder.establish documents:

Returns ParcelFileDescriptor of the VPN interface, or null if the application is not prepared.

need call VpnService.prepare: http://developer.android.com/reference/android/net/VpnService.html#prepare(android.content.Context) first

like image 76
xjdrew Avatar answered Oct 16 '22 22:10

xjdrew