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.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
serviceConnection = NIMMSTAServiceConnection.bindServiceToActivity(this).onComplete {
try {
// this is the point in time when the (background) task completes and the result throws if an error occurred.
it.result
it.result.enableBackgroundAndNotifications()
// Allow multi device before displaying the connection activity
it.result.setConnectionStrategy(IConnectionStrategy.MULTI_DEVICE_SLOTS).onError {
// handle error
}
// Check if connected, if not show connection Activity
if (!serviceConnection.isConnected) {
serviceConnection.displayConnectionActivity()
}
} catch (throwable: Throwable) {
throwable.printStackTrace()
Toast.makeText(
this@MainActivity,
throwable.message,
Toast.LENGTH_LONG
).show()
}
}
}
class MainActivity: Activity() {
/* ... */
NIMMSTAServiceConnection serviceConnection;
/* ... */
serviceConnection = NIMMSTAServiceConnection.bindServiceToActivity(this)
.onComplete(new NIMMSTADoneCallback<Task<NIMMSTAServiceConnection>>() {
@Override
public void onDone(Task<NIMMSTAServiceConnection> result) {
try {
// this is the point in time when the (background) task completes and the result throws if an error occurred.
result.getResult();
// Allow multi device before displaying the connection activity
result.getResult().setConnectionStrategy(IConnectionStrategy.COMPANION.MULTI_DEVICE_SLOTS);
} catch (Throwable throwable) {
throwable.printStackTrace();
Toast.makeText(
MainActivity.this,
throwable.getMessage(),
Toast.LENGTH_LONG
).show();
}
}
});
@Override
protected void onDestroy() {
super.onDestroy();
serviceConnection.close();
}
}
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
serviceConnection.devices?.all
// Retrieve a set of all connected devices
// Filters out all devices that are not currently connected
serviceConnection.devices?.connectedDevices
// Retrieve a set of all devices that are currently charging
// Check out the other filters as well
serviceConnection.devices?.filter(DeviceFilter.CHARGING)
// Retrieve a device by Address or ConnectionCode
serviceConnection.devices?.get(HS50ConnectionCode("ABCDE"))
serviceConnection.devices?.get(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
serviceConnection.getDevices().getAll();
// Retrieve a set of all connected devices
// Filters out all devices that are not currently connected
serviceConnection.getDevices().getConnectedDevices();
// Retrieve a set of all devices that are currently charging
// Check out the other filters as well
serviceConnection.getDevices().filter(DeviceFilter.CHARGING);
// Retrieve a device by Address or ConnectionCode
serviceConnection.getDevices().get(new HS50ConnectionCode("ABCDE"));
serviceConnection.getDevices().get(new BluetoothDeviceMacAddress("FF:FF:FF:FF:FF:FF"));