Reputation: 5365
My goal is to find all bonjour services in WIFI network, find their ip/port, and communicate through HTTP.
Bonjour service, hosted on iMac is discovered incorrectly - service name is found, but port = 0, InetAddress[] = null. Connection to the same service hosted on Windows PC is working properly (same version of soft from one vendor) - name, url and port discovered correctly. Bonjour service browser sees both of services correctly.
JmDNS 3.4.0 lib is used, see code example (some code is omitted):
MulticastLock lock = wifi.createMulticastLock(MULTICAST_LOCK_TAG);
lock.setReferenceCounted(true);
lock.acquire();
final InetAddress inetAddress = getLocalIpAddress();
jmdns = JmDNS.create(inetAddress, JMDNS_NAME);
ServiceInfo[] infos = jmdns.list(CAMERA_SERVERS);
if (infos != null && infos.length > 0) {
for (int i = 0; i < infos.length; i++) {
name = infos[i].getName();
InetAddress[] addresses = infos[i].getInetAddresses();
url = addresses[0].getHostAddress();
port = infos[i].getPort();
}
PS. Tried dns_sd.jar from Apple, but it relies on native code and cant be used in Android.
Any ideas?
Thanks.
Upvotes: 2
Views: 1293
Reputation: 1965
use below code. Tested and implemented and working fine
android.net.wifi.WifiManager.MulticastLock lock;
android.os.Handler handler = new android.os.Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
handler.postDelayed(new Runnable() {
public void run() {
setUp1();
}
}, 1000);
}
private void setUp1()
{
android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) getSystemService(android.content.Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("mylockthereturn");
lock.setReferenceCounted(true);
lock.acquire();
try {
//http://stackoverflow.com/questions/13677199/jmdns-doesnt-work-properly-on-android-4-1
WifiInfo wifiInfo = wifi.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
InetAddress _bindingAddress = InetAddress.getByName(ipAddress);
jmdns = JmDNS.create(_bindingAddress);
ServiceInfo[] infos = jmdns.list("_afpovertcp._tcp.local.");
for (int i=0; i < infos.length; i++) {
notifyUser("\nServic : "+infos[i].getName()+"");
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
private void notifyUser(final String msg) {
handler.postDelayed(new Runnable() {
public void run() {
TextView t = (TextView)findViewById(R.id.text);
t.setText(msg+"\n=== "+t.getText());
}
}, 1);
}
Upvotes: 1
Reputation: 13085
For starters try to use the JmDNS.create
method overload that takes no parameters. The problem is that there might be several network interfaces (mobile and wifi) and you may bind to the wrong IP using the getLocalIpAddress
. Read more about how JmDNS guesses those parameters itself here. The reason it works on the PC because your getLocalIpAddress
returns the WiFi ip. Perhaps because it's the only network interface.
Also make sure that the service you want to access is available via the WiFi. It's very common for wireless routers to separate the WiFi network and the ethernet network for obvious security reasons.
Upvotes: 0