2

This is the code that is used to connect to a screen with wifi on address 192.168.22.1. It is working fine when connecting with mobile data turned off, but doesn't work if mobile data is on:

public void onConnectClicked(View view){
        Y2Screen screen = new Y2Screen("http://192.168.22.1");
        final TextView message=findViewById(R.id.mess);
        try {
            message.setText("Connecting....");
            if (!screen.login("guest", "guest")) {
                message.setText("Connection Failed");
            } else {
                message.setText("Done Loging.");
                //VideoArea va = new VideoArea(0, 0, screen.getWidth(), screen.getHeight());
                PicArea pa=new PicArea(0, 0, screen.getWidth(), screen.getHeight());


                File dir = Environment.getExternalStorageDirectory();
                String path = dir.getAbsolutePath();


                //va.addVideo(path+"/glomo/image.jpeg", 100);
                pa.addPage(path+"/glomo/test3.png","PNG");
                ProgramPlayFile prog = new ProgramPlayFile(1);
                prog.getAreas().add(pa);
                String listId = screen.writePlaylist(new ProgramPlayFile[]{prog});
                screen.play(listId);

            }
        } catch (Y2Exception e) {
            e.printStackTrace();
        }
    }
Vivek Sharma
  • 531
  • 6
  • 20

2 Answers2

2

Probably is because 192.168.22.1 is a local address, so it is only accessible from local network (wifi, ...). If you are using mobile connection data you are on the public internet so you will need to NAT that local address to a public address port.

You can detect the type of connection using:

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();

More info at Detect network connection type on Android and about NAT issue: Get the NAT translated IP and port of some local port and How to expose my localhost to the WWW? (port forwarding?)

If you have both connections active loop all the networks from connectivityManager.getAllNetworks() and choose one with connectivityManager.bindProcessToNetwork(network);

for (Network network : connectivityManager.getAllNetworks()) {
  NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
  if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET || networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
    connectivityManager.bindProcessToNetwork(network);
    break;
  }
}

See Use multiple network interfaces in an app

user1039663
  • 1,230
  • 1
  • 9
  • 15
  • Sorry for my inability to explain the question, but that was my question that I already know that the **IP** runs only on wifi locally so how can I route the network through wifi when both **wifi** and **mobile internet** is turned on? – Vivek Sharma Jan 08 '20 at 11:52
  • 1
    I added that info to the answer: if you have both connections active loop all the networks from connectivityManager.getAllNetworks() and choose one with connectivityManager.bindProcessToNetwork(network); – user1039663 Jan 08 '20 at 11:57
1

I found out a solution for that and its working, we just need to change the network channel to transport through wifi instead of mobile internet.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            final ConnectivityManager manager = (ConnectivityManager) MainActivity.this
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkRequest.Builder builder;
            builder = new NetworkRequest.Builder();
            //set the transport type do WIFI
            builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
            manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        manager.bindProcessToNetwork(network);
                    } else {
                        //This method was deprecated in API level 23

                        ConnectivityManager.setProcessDefaultNetwork(network);
                    }
                    try {

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    manager.unregisterNetworkCallback(this);
                }
            });
        }
Vivek Sharma
  • 531
  • 6
  • 20