Getting Started
The JavaScript library communicates over a websocket of the NIMMSTA Android / Windows App with the scanner.
Requirements
- NIMMSTA Android App Version 5.0, Windows App 6.0, or Websocket App 6.0, or above
- Browser App on Android that supports ECMAScript 6
The communication with websites with this library works for all NIMMSTA Android / Windows Apps with version 5.0+. You can get the latest version in the NIMMSTA B2B Portal.
The library is compiled with ECMAScript 6 targeted. So ECMAScript 6 support is required.
Warning
The communication with the NIMMSTA Smart Watch only works if you open the website with a browser on the phone where the NIMMSTA App is installed or on a Windows PC that has the websocket application installed.
It is possible to debug the page on your phone. A tutorial on how this is done can be found on the official google page.
Integrate JavaScript Library
To get started with the NIMMSTA JavaScript Library, you have to add the script loader file to your HTML file above your JavaScript code that will use the library. If you want to build an Angular application you can get started with our Angular example project to see how it's done in an easy way.
<script type="text/javascript" src="https://cdn.nimmsta.com/latest/nimmsta.loader.js"></script>
<script type="text/javascript" src="your-index.js"></script>
TypeScript Definition
The TypeScript definition can be downloaded here: https://cdn.nimmsta.com/latest/nimmsta-ts-declarations.zip
Initialize library
The javascript file you added above is only a loader of the whole script which is embedded in the Application that is running on your machine (Android App, Windows JVM Application). If your using a non default websocket port you have to change it. After the initialization is done the callback function Nimmsta.onReady gets called. To initialize the communication between the website and the NIMMSTA Smart Watch you have to create an instance of NimmstaConnectionManager.
NIMMSTA.websocketPort = 64693;
NIMMSTA.requestSendTimeout = 1000; // 1000 ms until a request is considered 'failed'
NIMMSTA.maxCommunicationRetries = 3; // Roughly 3 * 1000 ms = 3000 ms is the time until onError will be invoked if app is missing.
// Invoked once connection to app is ready to be used.
NIMMSTA.onReady(function() {
const connectionManager = new NimmstaConnectionManager();
if (connectionManager.devices.length > 0) {
const device = connectionManager.devices[0];
} else {
connectionManager.displayConnectActivity();
}
});
NIMMSTA.onError(function(error) {
// handle error, e.g. app is not installed, so no connection was established.
// Retry connect by calling NIMMSTA.tryConnect()
});
Keeping Connection Open
The connection to the NIMMSTA Websocket is automatically closed once the focus of current tab gets lost. In certain circumstances, it makes sense ot change this behavior.
You can set keepAlwaysConnected
to true to keep the connection open.
Debug logging
If you want to see debug logging for your development process, you can enable and disable it by calling:
// Enable debug logging
Logger.enableDebugLogging();
// Disable debug logging
Logger.disableDebugLogging();
To learn more about what you can do with the NimmstaConnectionManager or the NimmstaDevice head to the dedicated pages for them.
Keep display on (Android)
As most browsers on Android do not allow to run JavaScript while in the background or display being off, we have a code snippet to fix this issue. It works on all browsers supporting Wakelock API.
/* keep screen active while this window is visible */
let wakeLock = null;
// Function to request a wake lock
async function requestWakeLock() {
try {
// Check if Wake Lock API is supported
if ('wakeLock' in navigator) {
wakeLock = await navigator.wakeLock.request('screen');
console.log('Wake Lock is active');
} else {
console.log('Wake Lock API not supported');
}
} catch (e) {
console.error(`Failed to acquire wake lock: ${e.message}`);
}
}
// Function to release the wake lock
async function releaseWakeLock() {
if (wakeLock !== null) {
await wakeLock.release();
wakeLock = null;
console.log('Wake Lock has been released');
}
}
// Event listener for visibility change
document.addEventListener('visibilitychange', async () => {
if (document.visibilityState === 'visible') {
await requestWakeLock();
} else {
await releaseWakeLock();
}
});
// Request wake lock when the page is loaded
window.onload = requestWakeLock;