5.3 Multi Device
Since Version 6.0, we support connecting multiple devices at once.
To allow this, you first need to enable multi device support. By default, connecting more than one device will throw an error.
fun main() {
val connectionManager = ConnectionManager()
// Set this before connecting the first device
connectionManager.setConnectionStrategy(IConnectionStrategy.MULTI_DEVICE_SLOTS).await()
val eventHandler = ExampleEventHandler()
connectionManager.startReceivingEvents(eventHandler)
connectionManager.displayConnectionActivity()
// Prevent app from exiting
while (true) {
Thread.sleep(10)
}
}
class Main {
public static void main(String[] args) throws InterruptedException {
ConnectionManager connectionManager = new ConnectionManager();
// Set this before connecting the first device
connectionManager.setConnectionStrategy(IConnectionStrategy.COMPANION.MULTI_DEVICE_SLOTS);
ExampleDelegate delegate = new ExampleDelegate();
connectionManager.startReceivingEvents(delegate);
connectionManager.displayConnectionActivity(false);
// Prevent app from exiting
while (true) {
Thread.sleep(10);
}
}
}
Every callback method like didScanBarcode or didTouch already provides a reference to the device that initiated the respective action.
The advanced API can be accessed via the ConnectionManager.
// Retrieve a set of all devices
// This includes all devices that were connected at some point during the runtime of the program
// The order is preserved even if a device is reconnected
connectionManager.devices.all
// Retrieve a set of all connected devices
// Filters out all devices that are not currently connected
connectionManager.devices.connectedDevices
// Retrieve a set of all devices that are currently charging
// Check out the other filters as well
connectionManager.devices.filter(DeviceFilter.CHARGING)
// Retrieve a device by Address or ConnectionCode
connectionManager.devices[HS50ConnectionCode("ABCDE")]
connectionManager.devices[BluetoothDeviceMacAddress("FF:FF:FF:FF:FF:FF")]
// Retrieve a set of all devices
// This includes all devices that were connected at some point during the runtime of the program
// The order is preserved even if a device is reconnected
connectionManager.getDevices().getAll();
// Retrieve a set of all connected devices
// Filters out all devices that are not currently connected
connectionManager.getDevices().getConnectedDevices();
// Retrieve a set of all devices that are currently charging
// Check out the other filters as well
connectionManager.getDevices().filter(DeviceFilter.CHARGING);
// Retrieve a device by Address or ConnectionCode
connectionManager.getDevices().get(new HS50ConnectionCode("ABCDE"));
connectionManager.getDevices().get(new BluetoothDeviceMacAddress("FF:FF:FF:FF:FF:FF"));