I have a distributed system that consists of androids and desktop machines and they need to pass data (a simple serialized object) among themselves. I am interested in a jini-like discovery system for android using which androids/desktop machines can discover each other and then transfer data to each other. The distributed system is very dynamic, in the sense, devices come and go quite abruptly and frequently.
I tried to work with Cling and I could discover my router but could not discover other devices such as android phones. So I was wondering whether android devices are really UPnP compatible or there might be something wrong with my code.
I am using the code discussed in the Cling user manual.
EDIT: Posting the code below-
public class UpnpBrowserActivity extends ListActivity {
private AndroidUpnpService upnpService;
Registry registry;
private ArrayAdapter<DeviceDisplay> listAdapter;
private BrowseRegistryListener registryListener = new BrowseRegistryListener();
private ServiceConnection serviceConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
upnpService = (AndroidUpnpService) service;
registry = upnpService.getRegistry();
// Refresh the list with all known devices
listAdapter.clear();
for (Device device : registry.getDevices()) {
registryListener.deviceAdded(registry,device);
}
// Getting ready for future device advertisements
registry.addListener(registryListener);
// Search asynchronously for all devices
upnpService.getControlPoint().search();
}
public void onServiceDisconnected(ComponentName className) {
upnpService = null;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upnp_browser);
listAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1);
setListAdapter(listAdapter);
getApplicationContext().bindService(new Intent(this, AndroidUpnpServiceImpl.class),serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.upnp_browser, menu);
menu.add(0, 0, 0, "Menu1").setIcon(android.R.drawable.ic_menu_search);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 0 && upnpService != null) {
upnpService.getRegistry().removeAllRemoteDevices();
upnpService.getControlPoint().search();
}
return false;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (upnpService != null) {
upnpService.getRegistry().removeListener(registryListener);
}
getApplicationContext().unbindService(serviceConnection);
}
class BrowseRegistryListener extends DefaultRegistryListener {
@Override
public void remoteDeviceDiscoveryStarted(Registry registry, RemoteDevice device) {
deviceAdded(device);
}
@Override
public void remoteDeviceDiscoveryFailed(Registry registry, final RemoteDevice device, final Exception ex) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(
UpnpBrowserActivity.this,
"Discovery failed of '" + device.getDisplayString() + "': " +
(ex != null ? ex.toString() : "Couldn't retrieve device/service descriptors"),
Toast.LENGTH_LONG
).show();
}
});
deviceRemoved(device);
}
@Override
public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
deviceAdded(device);
}
@Override
public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
deviceRemoved(device);
}
@Override
public void localDeviceAdded(Registry registry, LocalDevice device) {
deviceAdded(device);
}
@Override
public void localDeviceRemoved(Registry registry, LocalDevice device) {
deviceRemoved(device);
}
public void deviceAdded(final Device device) {
runOnUiThread(new Runnable() {
public void run() {
DeviceDisplay d = new DeviceDisplay(device);
int position = listAdapter.getPosition(d);
if (position >= 0) {
// Device already in the list, re-set new value at same position
listAdapter.remove(d);
listAdapter.insert(d, position);
} else {
listAdapter.add(d);
}
}
});
}
public void deviceRemoved(final Device device) {
runOnUiThread(new Runnable() {
public void run() {
listAdapter.remove(new DeviceDisplay(device));
}
});
}
}
}
I also read this question on SO but if android devices cannot be discovered with Cling or another similar library, I would prefer to write a small discovery system using TCP/IP sockets.
Kindly guide me if I am missing or misunderstanding something. Any help is appreciated. Thanks in advance.