Connecting and managing devices

This chapter elaborates on how you can discover and manage Jabra devices through setting event listeners.

Subscribing to events that show individually added or removed devices and connections helps you react to new devices and connections without computing a difference between lists of devices or connections.

The sections that follow contain code snippets for each way of managing devices and connections.

Moreover, see a full code sample demonstrating an approach to manage devices, together with an approach to manage connections in Subscribing to newly added or removed devices and connections.

Getting a list of available devices by subscribing to events

The Jabra core module maintains a list of available devices. At any point in time, you can get the current list of all devices, or you can subscribe to receive events whenever the list changes.

The following code snippet shows how to subscribe to changes in the list of devices, which returns a complete list of all devices:

async function subscribeToDeviceListChanges(jabraSdk) {

  jabraSdk.deviceList.subscribe((list) => {
    if (list.length > 0) {
      console.log('---\nAvailable devices:');
      list.forEach((device, index) => {
        console.log(`${index}: ${device.name} - ${device.productId}`)
      });
      console.log('---');
    }
  });
}

Subscribing to events showing individually added or removed devices

On the other hand, and as an alternative to receiving the full list of available devices on each event, you can subscribe to deviceAdded and deviceRemoved events, which only pass the device added or removed.

The individually added or removed devices approach is suited to monitor devices that were added or removed on a singular basis without receiving a full list.

The following code snippet shows how you can subscribe to deviceAdded and deviceRemoved events:

async function printDeviceList(list) {

  if (list.length > 0) {
    console.log('---\nAvailable devices:');
    list.forEach((device, index) => {
      console.log(`${index}: ${device.name} - ${device.productId}`)
    });
    console.log('---');
  }
}

async function printDevicesAndSubscribeToChanges(jabraSdk) {

  jabraSdk.deviceList.subscribe((list) => {
    printDeviceList(list);
  });

  jabraSdk.deviceAdded.subscribe((device) => {
    console.log(`Added: ${device.name} - ${device.productId}`);
  });

  jabraSdk.deviceRemoved.subscribe((device) => {
    console.log(`Removed: ${device.name} - ${device.productId}`);
  });

}

Getting a list of available connections for a device by subscribing to events

When an end-user connects a headset to their computer, some cases can lead to multiple connections, such as simultaneously using a wireless dongle connection for calls, in addition to a wired USB cable connection for charging. A new wired connection does not report the headset as an additional device, but merely as an additional connection for the same device.

With respect to the device, the Jabra library emits a new connection event (e.g., USB cable for charging) without emitting a new device event.

The core module maintains a list of available connections per device. At any point in time, you can get the current list of connections for that device, or you can subscribe to receive events whenever the list changes.

The following code snippet shows how to subscribe to changes in the list of a device's connections, which emits the complete list of available connections for a device.

async function subscribeToConnectionListChanges(device) {
  device.connectionList.subscribe(deviceConnections => {

    console.log(`Current list of connections for device ${device.name}:`);

    deviceConnections.forEach(connection => {
      console.log(`${connection.id} - ${connection.type}`);
    });
  });
}

Subscribing to events showing individually added or removed connections

Instead of receiving the full list of a device's available connections, you can subscribe to newly added or removed connection events for a device.

With these events, you only get the specific connection that changed for a device.

The following code sample shows how to set the event listener using connectionAdded and connectionRemoved events.

async function printDeviceConnections(device) {
  console.log(`Current list of connections for device ${device.name}`);
  if (device.currentConnections.length > 0) {
    device.currentConnections.forEach((connection) => {
      console.log(`${connection.id} = ${connection.type}`);
    });
  } else {
    console.log('None');
  }
}

async function subscribeToChanges(device) {
  printDeviceConnections(device);

  device.connectionAdded.subscribe((newConnection) => {
    console.log(`Added connection to ${device.name} - ${newConnection.type}`);
    printDeviceConnections(device);
  });

  device.connectionRemoved.subscribe((removedConnection) => {
    console.log(`Removed connection from ${device.name} - ${removedConnection.type}`);
    printDeviceConnections(device);
  });
}

4.5 Subscribing to newly added or removed devices and connections

The following full code sample shows how to subscribe to newly added or removed devices and connections:

import { init, RequestedBrowserTransport } from '@gnaudio/jabra-js';
import { takeUntil } from 'rxjs/operators';

async function main(){

  try {
    const config = {
      transport: RequestedBrowserTransport.CHROME_EXTENSION_WITH_WEB_HID_FALLBACK
    };

    // Initialize the Jabra SDK
    const jabraSdk = await init(config);

    // Subscribe to changes in the device list
    jabraSdk.deviceAdded.subscribe((device) => {

      console.log(`New device - ${device.name} - ${device.productId} - ${device.type}`);

      // Subscribe to connections being added and removed for this device,
      // and make those subscriptions only last until the device itself is disconnected.

      device.connectionAdded.pipe(
          takeUntil(device.onDisconnect)
        ).subscribe((connection) => {

          console.log(`${device.name}: new connection - ${connection.type}`);
        });

      device.connectionRemoved.pipe(
          takeUntil(device.onDisconnect)
        ).subscribe((connection) => {

          console.log(`${device.name}: connection removed - ${connection.type}`);
        });
    });

    jabraSdk.deviceRemoved.subscribe((device) => {

      console.log(`Removed device - ${device.name}`);
    });

  } catch (err) {
    console.log(err);
  }
}

main();