Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android port forwarding

How can I make a port forwarding from a android device to router using the IP from another device?

I want to connect from android device externally to a routers public ip:port so that I can access the hardware device that is connected to the router.(Android -> external IP:Port) -> Router -> hardware Device(hardware device has its own IP and mac).

enter image description here

Code:

    PortMapping mapping = new PortMapping();
    UnsignedIntegerTwoBytes externalPort = new UnsignedIntegerTwoBytes(22555L);
    UnsignedIntegerTwoBytes internalPort = new UnsignedIntegerTwoBytes(80L);

    mapping.setDescription("HardwareDescription");
    mapping.setEnabled(true);
    mapping.setExternalPort(externalPort);
    mapping.setInternalClient("192.168.2.68");
    mapping.setInternalPort(internalPort);
    mapping.setProtocol(PortMapping.Protocol.TCP);
    mapping.setRemoteHost("192.168.2.254");


    mUpnpService = new MyUpnpServiceImpl(new PortMappingListener(mapping));
    mUpnpService.getRouter();
    mUpnpService.getControlPoint().search(SCAN_TIMEOUT);

UpnpServiceImpl:

private class MyUpnpServiceImpl extends UpnpServiceImpl {

    public MyUpnpServiceImpl(RegistryListener... listeners) {
        super(new AndroidUpnpServiceConfiguration(getWifiManager()),
                listeners);
    }

    @Override
    public Router getRouter() {
        return super.getRouter();
    }
    @Override
    protected Router createRouter(ProtocolFactory protocolFactory,
            Registry registry) {
        return new AndroidWifiSwitchableRouter(configuration,
                protocolFactory, getWifiManager(), getConnectivityManager());
    }
}

The code above doesn't crash, but also it doesn't create any port!

Is this possible? If the answer is yes, can you point me in the right direction.

like image 200
MSA Avatar asked Mar 14 '14 08:03

MSA


People also ask

Is it possible to forward port on Android mobile hotspot?

On the Mobile Hotspot Homepage, click the Advanced Settings icon. Go to Router > Port Forward. Select Enable option beside "Port Forwarding".

Can I port forward on mobile?

You won't have any use port forwarding a mobile Hotspot connection. The IP address assigned to a mobile device is generally a CG-NAT private address, and forwarding that won't help you achieve a publicly reachable host.


1 Answers

Found the answer for this question. The first step is to enable the UPNP option on router, after this step import the library net.sbbi.upnp search for the router (IGD) device and use the method addPortMapping.

Here is an example for anyone that want to open o port on router using any IP, not just from the current device that the app runs.

https://github.com/ManolescuSebastian/Port_Forward_Android

like image 155
MSA Avatar answered Oct 03 '22 07:10

MSA