30
I am trying to implement bonjour/zero conf on my android app. I am using jmDns library for searching the all the available devices. Here is the code that I am using for searching the devices in the same network:
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());