Implementação do Bonjour no Android
Estou tentando implementar bonjour / zero conf no meu aplicativo Android. Estou usando a biblioteca jmDns para pesquisar todos os dispositivos disponíveis. Aqui está o código que estou usando para pesquisar os dispositivos na mesma rede:
public class ListDevices extends ListActivity {
JmDNS jmdns;
JmDNSImpl impl;
MulticastLock lock;
protected ServiceListener listener;
protected ServiceInfo info;
public ListView lv;
public ArrayList<String> deviceList;
public int cancel = 0;
public final static String TAG = "ListDevices";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
deviceList = new ArrayList<String>();
showAllPrinters();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, deviceList));
lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
this.listener = new ServiceListener() {
public void serviceAdded(ServiceEvent event) {
deviceList.add("Service added : " + event.getName() + "."
+ event.getType());
Log.v(TAG, "Service added : " + event.getName() + "."
+ event.getType());
}
public void serviceRemoved(ServiceEvent event) {
deviceList.add("Service removed : " + event.getName() + "."
+ event.getType());
Log.v(TAG, "Service removed : " + event.getName() + "."
+ event.getType());
}
public void serviceResolved(ServiceEvent event) {
deviceList.add("Service resolved: " + event.getInfo());
Log.v(TAG, "Service resolved: " + event.getInfo());
}
};
}
public void showAllPrinters() {
Log.d("ListDevices", "in showAllPrinters");
try {
WifiManager wifi = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
lock = wifi.createMulticastLock("fliing_lock");
lock.setReferenceCounted(true);
lock.acquire();
InetAddress inetAddress = getLocalIpAddress();
jmdns = JmDNS.create(inetAddress, "TEST");
ServiceInfo[] infos = jmdns.list("_http._tcp.local.");
if (infos != null && infos.length > 0) {
for (int i = 0; i < infos.length; i++) {
deviceList.add(infos[i].getName());
}
} else {
deviceList.add("No device found.");
}
impl = (JmDNSImpl) jmdns;
} catch (IOException e) {
e.printStackTrace();
}
}
public InetAddress getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = (NetworkInterface) en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = (InetAddress) enumIpAddr
.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress;
}
}
}
} catch (SocketException ex) {
Log.e("ListDevices", ex.toString());
}
return null;
}
@Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
lock.release();
}
}
}
Basicamente, estou adicionando-os a uma lista para poder exibir uma lista de todos os dispositivos disponíveis. Agora, quando estou executando esse código, não estou recebendo nenhuma exceção / nada como erro. Mas, por outro lado, nada é adicionado à minha lista [PS: existem pelo menos 5-6 PCs e Macs na rede.
Eu também tentei obter a lista deste código:
jmdns.addServiceListener("_http._tcp.local.", listener);
listener
é definido noonCreate
da atividade. Mas isso também não retornou nenhum dispositivo.
Por favor, ajude, sugira o que estou fazendo de errado aqui. Qualquer ajuda é apreciada!