Java SDK - v1.2
// GNU/Linux, in folder /dev/.
"(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}"
// Solaris, in folder /dev/term/.
"[0-9]*|[a-z]*"
// Mac OS X, folder /dev/.
"tty.(serial|usbserial|usbmodem).*"
// Windows: use Windows API to find available serial ports...
public class FindLocalDevices
{
public static void main(String[] args)
{
localDevices
is used to store the instances of
Device found by
the SDK, using the static method getPluggedDevices.
If no device is available, pluggedDevices
references an empty Map.
List<Device> localDevices = new ArrayList<>();
// get all local devices available
Map<String, PluggedDevice> pluggedDevices = Device.getPluggedDevices();
if(pluggedDevices.isEmpty())
{
out.println("No device available. End.");
return;
}
for(PluggedDevice pd : pluggedDevices.values())
{
// display: "DeviceType (SerialNumber) on port PortName"
out.println(
String.format("%s (%s) on port %s ",
pd.getDeviceType(),
pd.getSerialNumber(),
pd.getSerialPort()
)
);
SampleEventHandler
below).
try {
Device newDevice = new Device(pd.getSerialNumber(), pd.getSerialPort());
localDevices.add(newDevice);
newDevice.addListener(new SampleEventHandler(newDevice));
} catch (DeviceCreationException dce) {
// catch DeviceCreationException to know if the device could not be initialized
err.println(
String.format(
"(Unable to connect with this device : %s)",
dce.getMessage()
)
);
}
}
// release all connected devices
for(Device device : localDevices)
{
device.release();
}
}
// The event listener is an inner static class for this example
private static class SampleEventHandler implements BasicEventHandler
{
private Device _device;
SampleEventHandler(Device device)
{
_device = device;
}
@Override
public void deviceDisconnected()
{
out.println(
String.format("Device %s has been disconnected.", _device.getSerialNumber())
);
}
@Override
public void deviceStatusChanged(DeviceStatus deviceStatus)
{
out.println(
String.format("Device %s -> [%s].",
_device.getSerialNumber(),
deviceStatus
)
);
}
}
}
public class ScanLightTags
{
private static Device _smartboard;
public static void main(String[] args)
{
/dev/ttyUSB0
).new TcpDevice(ip);
instead.
try {
_smartboard = new Device(null, "/dev/ttyUSB0");
} catch (DeviceCreationException dce) {
err.println("Unable to connect the SmartBoard.");
return;
}
_smartboard.addListener(new SampleEventHandler(_smartboard));
_smartboard.requestScan();
out.println("Scan requested, waiting for events.");
// wait for an input before releasing the device and exiting
out.println("Press Enter to exit...");
new Scanner(System.in).nextLine();
_smartboard.stopLightingTagsLed();
_smartboard.release();
}
private static void onScanCompleted()
{
// Warning: getLastInventory() may return null if using a TcpDevice
List<String> tags = _smartboard.getLastInventory().getTagsAll();
out.println(String.format("Scan Completed: %d tags scanned.", tags.size()));
_smartboard.startLightingTagsLed(tags);
}
private static void onLightingStarted(List<String> tagsNotLighted)
{
out.println("Lighting Started!");
for(String uid : tagsNotLighted)
{
err.println(String.format("%s not lighted", uid));
}
}
private static class SampleEventHandler implements ScanEventHandler, LedEventHandler
{
onScanCompleted
for a better readability.
@Override
public void scanCompleted()
{
onScanCompleted();
}
onLightingStarted
for a better readability.
@Override
public void lightingStarted(List<String> tagsNotLighted)
{
onLightingStarted(tagsNotLighted);
}
// Note: Irrelevant implementations are hidden for clarity reasons
}
}
public class ContinuousScanning
{
private static Device _smartboard;
public static void main(String[] args)
{
/dev/ttyUSB0
).new TcpDevice(ip);
instead.
try {
_smartboard = new Device(null, "/dev/ttyUSB0");
} catch (DeviceCreationException dce) {
err.println("Unable to connect the SmartBoard.");
return;
}
_smartboard.addListener(new SampleEventHandler(_smartboard));
_smartboard.requestScan();
// wait for an input before releasing the device and exiting
new Scanner(System.in).nextLine();
_smartboard.release();
}
private static void onScanCompleted()
{
// Warning: With Remote Devices, "getLastInventory" result can be null
Inventory inventory = _smartboard.getLastInventory();
out.println(String.format("Scan Completed at %s", inventory.getCreationDate()));
out.println(String.format("%d tags added", inventory.getNumberAdded()));
out.println(String.format("%d tags already present", inventory.getNumberPresent()));
out.println(String.format("%d tags removed", inventory.getNumberRemoved()));
_smartboard.requestScan();
}
private static void onScanCancelledOrFailed()
{
err.println("Scan cancelled or failed.");
_smartboard.requestScan();
}
SampleEventHandler
additionally implements ScanEventHandler.
private static class SampleEventHandler implements ScanEventHandler
{
onScanCompleted
for a better readability.
@Override
public void scanCompleted()
{
onScanCompleted();
}
onScanCancelledOrFailed
for a better readability.
@Override
public void scanFailed()
{
onScanCancelledOrFailed();
}
@Override
public void scanCancelledByHost()
{
onScanCancelledOrFailed();
}
// Note: Irrelevant implementations are hidden for clarity reasons
}
}