NimmstaConnectionManager
The NimmstaConnectionManager handles the communication with the app and distributes responses to the correct NimmstaDevice. You can also connect to new Devices and check what devices are already connected to the app. It has an array of all currently connected devices.
const device = connectionManager.devices[0];
const devicesWithKnownAddress = connectionManager.getDeviceWithAddress(address);
Properties
The following properties can be read and set to change the behavior of the host application.
- Get/Set allowMultiDevice = false, true
- Get/Set interpretButtonAsMotionTrigger = false, true (if true, the preferredTriggerMode button will be interpreted as a motion trigger)
Example
// Get AllowMultiDevice
const allowMultiDevice = connectionManager.allowMultiDevice;
// Change AllowMultiDevice
device.allowMultiDevice = true;
Actions
To communicate with the app to manage connections you can call several functions, that will be explained in this section.
The following actions are currently supported:
Get Devices
To get information about devices you can call the getDevices action which returns a list of devices that matches the given filter. There are 5 different filters available: ALL, CONNECTED, CONNECTING, CHARGING and SEARCHING_TO_RECONNECT
connectionManager.getDevices(DeviceFilter.ALL).then((response) => {
console.log('Devices', response.devices);
});
// Default filter is CONNECTED
connectionManager.getDevices().then((response) => {
response.devices.forEach((device) => {
console.log('Address', device.address);
console.log('ConnectionCode', device.connectionCode);
console.log('BatteryLevel', device.batteryLevel);
console.log('Status', device.status);
console.log('AllowHid', device.allowHid);
console.log('IsCharging', device.isCharging);
console.log('isSearchingToReconnect', device.isSearchingToReconnect);
console.log('PrefersReconnect', device.prefersReconnect);
console.log('PreferredPickingMode', device.preferredPickingMode);
console.log('PreferredTriggerMode', device.preferredTriggerMode);
console.log('PrefersShutdownOnCharge', device.prefersShutdownOnCharge);
console.log('Rules', device.rules);
console.log('Reconnect', device.reconnect);
console.log('wasReconnected', device.wasReconnected);
});
}).catch((error) => {
console.log('Error', error);
});
Display Connect Activity
This is the easiest way to connect a new HS-50. This action opens a new window that displays the scannable connection QR-Code. After the connection is established successfully the window disappears again.
Search For NIMMSTA Devices
You can also start a search for NIMMSTA Devices, to detect all HS-50s that are in reach of your device. To get information about discovered/lost devices you have to subscribe the deviceFoundEvent and the deviceRemovedEvent that are covered in the event section.
connectionManager.searchForNimmstaDevices().then(() => {
console.log('Success');
}).catch((error) => {
console.log('Error', error);
});
Cancel Search
The search you started above runs infinitely until you cancel it with this action again.
connectionManager.cancelSearch().then(() => {
console.log('Successfully cancelled');
}).catch((error) => {
console.log('Error', error);
});
Connect to Address
If the search for devices found a device you know the address of it. With this address you can connect to the device.
connectionManager.connectToAddress(address).then((device) => {
console.log('Connected to device', device);
}).catch((error) => {
console.log('Error', error);
});
Get Connection Code
If you want to implement the connection progress with the QR-Code scanning yourself you can do so with the getConnectionCode Action. This Action returns a connectionCode and a base64 encoded png image of a QR-Code that you can display on your webpage.
connectionManager.getConnectionCode().then((response) => {
console.log('Got response', response);
const connectionCode = response.connectionCode;
const qrBase64 = response.qrCodeImage;
document.getElementById('PlaceForQRCode').innerHTML = `<img width="400" height="400" src="data:image/png;base64,${qrBase64}">`;
}).catch((error) => {
console.log('Error', error);
});
Connect to Connection Code
To connect to a device that has scanned the QR-Code you have to start a connection attempt.
connectionManager.connectToConnectionCode(connectionCode).then((device) => {
console.log('Connected to device', device);
}).catch((error) => {
console.log('Error', error);
});
Cancel Connect
To cancel an ongoing connect attempt you can call the cancelConnectAction, which accepts an address or an connectionCode.
connectionManager.cancelConnect(addressOrConnectionCode).then(() => {
console.log('Successfully cancelled');
}).catch((error) => {
console.log('Error', error);
});
Cancel All Connect
You can also cancel all ongoing connection attempts at once.
connectionManager.cancelAllConnect().then(() => {
console.log('Successfully cancelled all');
}).catch((error) => {
console.log('Error', error);
});
Update Keyboard Settings
Updates global keyboard settings. Need to be set again if the application is restarted.
Warning
This settings is only available on Windows!
connectionManager.updateKeyboardSettings(keyboardSettings).then(() => {
console.log('Successfully updated keyboard settings');
}).catch((error) => {
console.log('Error', error);
});
Events
To get notified about new connections or other events you can subscribe to them. Keep in mind that you can subscribe multiple times and every subscription will be called afterwards. So make sure you cancel the subscription of events that are no longer needed to prevent accidental double calls.
Unsubscribe from Events
To cancel a subscription you need to save the return value of the subscribe call and call cancel on it afterwards.
const subscription = connectionManager.connectEvent.subscribe((device) => {
// DO STUFF
});
// Call cancel if no longer needed
subscription.cancel();
General Error Event
A general error Event occurs when an error occurs that hinders the processing of a sent message
connectionManager.generalErrorEvent.subscribe((error) => {
console.error("General error occurred", error);
});
Connect Event
A connect Event occurs when a new HS-50 is connected to the app. This event returns a new instance of NimmstaDevice that corresponds to the connected scanner.
connectionManager.connectEvent.subscribe((device) => {
console.log("New Scanner connected", device);
});
Device Found Event
The deviceFoundEvent occurs if a device is discovered by the NimmstaDevices search.
connectionManager.deviceFoundEvent.subscribe((event) => {
console.log("Address of device", event.device);
console.log("DeviceName", event.deviceName);
console.log("IsConnectable", event.isConnectable);
console.log("RSSI", event.rssi);
console.log("ConnectionCode", event.connectionCode);
console.log("TimeFound", event.timeFound);
});
Device Removed Event
The deviceRemovedEvent occurs if a previously discovered device was lost again, e.g. out of range or shutdown.
connectionManager.deviceRemovedEvent.subscribe((event) => {
console.log("Address of device", event.device);
console.log("DeviceName", event.deviceName);
console.log("IsConnectable", event.isConnectable);
console.log("RSSI", event.rssi);
console.log("ConnectionCode", event.connectionCode);
console.log("TimeFound", event.timeFound);
});
Socket Connect Event
The socketConnectEvent occurs if any application opens a new connection with the socket. It let's you know how many applications are currently communicating on the socket.
connectionManager.socketConnectEvent.subscribe((event) => {
console.log("CurrentConnectionCount", event.currentConnectionCount);
});
Socket Disconnect Event
The socketDisconnectEvent occurs if any application closes a connection with the socket. It let's you know how many applications are currently communicating on the socket.