1.4 Connect Functions & Reconnect behavior
In this section, we will discuss different ways in which the disconnect and subsequent reconnect can occur.
ConnectionManager
The ConnectionManager needs to be instantiated only once. Even if your device loses the connection your current ConnectionManager will handle all reconnect attempts for you.
Automatic Reconnect
If your device loses the connection, your ConnectionManager will automatically start to scan for your device again. If it finds the device, it will attempt an automatic reconnect. It works exactly the same way as a normal connection attempt, except for the fact that your previously defined preferences and settings will be saved (for example if scanning by pressing on the display is turned on).
If you want to take a different code path if the connection attempt is a reconnect you can check the connectCount attribute of the device like this:
Configure the Reconnect Timeout
You can also configure the reconnect timeout. The default is 30 Minutes, for example:
Connect via QR Code
If your device loses the connection, you can also show the QR code again with connectionManager.displayConnectionActivity(). If you do this any new device will be able to connect, just like in the normal connection case.
Note
If you connect another device, the original device which was connection previously doesn't automatically reconnect anymore!
Connect manually via connectAsync()
If you know the Device address you can also connect directly to a device using a DeviceAddress or ConnectionCode.
The connection code consists of a 5-digit code and the prefix XXConnectHSD50XX:
or
a 6-digit code and the prefix XXConnectHST50XX:
for temporary connection codes, which
the device forgets on shutdown.
// Using device address
BluetoothDeviceMacAddress deviceAddress = new BluetoothDeviceMacAddress("EXAMPLEADDRESS");
connectionManager.connectAsync(deviceAddress);
// Using connection code
HS50ConnectionCode connectionCode = HS50ConnectionCode.Companion.random();
connectionManager.connectAsync(connectionCode);
Search for NIMMSTA Devices continuously.
If you know the Device address you can also connect directly to a device using a DeviceAddress or ConnectionCode.
lateinit var task: Task<Unit>
task = connectionManager.searchForNIMMSTADevices { searchScanResult, deviceListUpdateAction ->
if (searchScanResult.isConnectable && deviceListUpdateAction == DeviceListUpdateAction.ADD) {
Log.d(TAG, "Found connectable device: ${searchScanResult.address}")
// connect to device
task.cancel()
connectionManager.connectAsync(searchScanResult.address).onComplete {
// done
}
}
}
// or cancel anytime
task.cancel()
Task<Unit> task;
task = connectionManager.searchForNIMMSTADevices(new INIMMSTAConnectionManager.DeviceListUpdateDelegate() {
@Override
public void deviceUpdate(@NotNull NIMMSTADeviceScannerListener.SearchScanResult scanResult, @NotNull INIMMSTAConnectionManager.DeviceListUpdateAction action) {
if (scanResult.isConnectable() && action == INIMMSTAConnectionManager.DeviceListUpdateAction.ADD) {
Log.d(TAG, "Found connectable device: ${searchScanResult.address}")
// stop search
task.cancel();
connectionManager.connectAsync(scanResult.getAddress()).onComplete(new TaskCallback<NIMMSTADevice>() {
@Override
public void run(@NotNull Task<NIMMSTADevice> task) {
// done
}
});
}
}
});
// or cancel anytime
task.cancel();