Skip to content

Searching for remote devices

pichler edited this page Jun 14, 2015 · 4 revisions

After a local device has been created (see this page), remote device handles can be obtained. If the IP address and device instance number of the remote device is already known, the findRemoteDevice() method of the local device instance can be used to get the handle:

String remoteDeviceIpAddress = "10.0.0.1";	// example IP address
int port = IpNetwork.DEFAULT_PORT;			// port 0xBAC0
int remoteDeviceIdentifier = 2138113;		// example device instance number
        
RemoteDevice remoteDevice = localDevice.findRemoteDevice(new Address(remoteDeviceIpAddress, port), null, remoteDeviceIdentifier);     

It's also possible to search for remote devices in the current IP network by sending a broadcast WhoIs message. If a remote device receives this message, it answers with an iAm response message. The response messages are collected from the local device instance and can be retrieved by using the getRemoteDevices() method. The following snippet shows how to send the broadcast and how to retrive the response results:

localDevice.sendGlobalBroadcast(new WhoIsRequest());
Thread.sleep(1*1000);	// wait a bit to collect the response messages
List<RemoteDevice> devices = localDevice.getRemoteDevices();

To retrieve more information from the remote device (like a description), bacnet4J offers a static utility method. The infomation will be stored in the remote device instance:

RequestUtils.getExtendedDeviceInformation(localDevice, remoteDevice);
System.out.println(remoteDevice.getName());

It is also possible to add a listener to the local device instance that will be informed on new iAm messages received. Therefore, bacnet4J provides the DeviceEventListener interface and a DeviceEventAdapter implementation to simplify the handling. The following example shows the relevant method of the interface and how to add the listener to the local device instance:

localDevice.getEventHandler().addListener(new DeviceEventListener() {		
	@Override
	public void iAmReceived(RemoteDevice device) {
		// TODO Auto-generated method stub			
	}
});

Back to bacnet4J page