I working with IoT (Arduino and ESP32). I am able to assign a hostname to the board using mDNS. I want from an Android App to write the url "arduino.local" and get the IP address (for example 198.162.1.25". I have tried with jmDNS and I don't know how to list the services found.
Library: implementation 'org.jmdns:jmdns:3.5.7'
This is my code:
package com.anaconet.jmdns;
import androidx.appcompat.app.AppCompatActivity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.MulticastLock;
import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
// jmDNS .........................................
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.util.Log;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceListener;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "jmDNS (PAUL)... ";
private static final String SERVICE_TYPE = "_http._tcp.local.";
//private static final String SERVICE_TYPE = "_workstation._tcp.local.";
private String type = "_dynamix._tcp.local.";
private static JmDNS jmdns = null;
private ServiceListener listener = null;
private ServiceInfo serviceInfo;
private static MulticastLock multicastLock;
boolean isDiscovering;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isDiscovering = false;
WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(android.content.Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock("jmdns-multicast-lock");
multicastLock.setReferenceCounted(false);
}
private InetAddress getDeviceIpAddress(WifiManager wifi) {
InetAddress result = null;
try {
// default to Android localhost
result = InetAddress.getByName("192.168.1.1");
// figure out our wifi address, otherwise bail
WifiInfo wifiinfo = wifi.getConnectionInfo();
int intaddr = wifiinfo.getIpAddress();
byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff),
(byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) };
result = InetAddress.getByAddress(byteaddr);
} catch (UnknownHostException ex) {
Log.w(TAG, String.format("getDeviceIpAddress Error: %s", ex.getMessage()));
}
return result;
}
private class ServiceListenerImpl implements ServiceListener {
@Override
public void serviceAdded(ServiceEvent event) {
event.getDNS().requestServiceInfo(event.getType(), event.getName());
System.out.println("Service added: " + event.getInfo());
}
@Override
public void serviceRemoved(ServiceEvent event) {
//Handle service dropping out.
System.out.println("Service removed: " + event.getInfo());
}
@Override
public void serviceResolved(ServiceEvent event) {
System.out.println("Service resolved: " + event.getInfo());
System.out.println("Service resolved: "
+ event.getInfo().getQualifiedName()
+ " port:" + event.getInfo().getPort());
}
}
@Override
protected void onStart() {
Log.i(TAG, "Starting ServiceActivity...");
super.onStart();
try {
Log.i(TAG, "Starting Mutlicast Lock...");
WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
// get the device ip address
final InetAddress deviceIpAddress = getDeviceIpAddress(wifi);
multicastLock = wifi.createMulticastLock(getClass().getName());
multicastLock.setReferenceCounted(true);
multicastLock.acquire();
Log.i(TAG, "Starting ZeroConf probe....");
// Create a JmDNS instance
// JmDNS jmdns = JmDNS.create(address, "JmDNS-IP-" + (jmdnsInstances.size() + 1));
jmdns = JmDNS.create(deviceIpAddress, "HOSTNAME");
if (jmdns != null) {
// Add a service listener
jmdns.addServiceListener(SERVICE_TYPE, new ServiceListenerImpl());
// jmdns.list("_exp._tcp.local.", 10 * 1000);
isDiscovering = true;
Log.d(TAG, "discovering services of type: " + SERVICE_TYPE);
/*
serviceInfo = ServiceInfo.create("_test._tcp.local.",
"AndroidTest", 0,
"test from android");
jmdns.registerService(serviceInfo);
*/
}
} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
System.out.println(e.getMessage());
}
Log.i(TAG, "Started ZeroConf probe....");
}
@Override
protected void onStop() {
Log.i(TAG, "Stopping ServiceActivity...");
super.onStop();
stopScan();
}
private static void stopScan() {
try {
if (jmdns != null) {
Log.i(TAG, "Stopping ZeroConf probe....");
jmdns.unregisterAllServices();
jmdns.close();
jmdns = null;
}
if (multicastLock != null) {
Log.i(TAG, "Releasing Mutlicast Lock...");
multicastLock.release();
multicastLock = null;
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage(), ex);
}
}
}
List all IP and Hostname connected to WiFi.