Issue
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();
}
}
Solution
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
Answered By - user1039663
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.