4 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.

For background information and further details, see Connecting and managing devices in the Jabra Integrator's Guide.

4.1 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:

private static void SubscribeToDeviceListChanges(IApi jabraSdk)
{
    jabraSdk.DeviceList.Subscribe((devices) =>
    {
        Console.WriteLine("Current list of available devices:");
        devices.ForEach((device) =>
        {
            Console.WriteLine(String.Format("{0} - {1}", device.Name,
                              device.ProductId));
        });
    });
}

4.2 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:

private static void PrintDevicesAndSubscribeToChanges(IApi jabraSdk)
{
    Console.WriteLine("Current list of available devices:");
    jabraSdk.DeviceList
        .Subscribe(
        devices =>
        {
          Console.Write("Current device list: ");
          if (devices.Count == 0)
          {
            Console.Write("Empty\n");
          }
          else
          {
            devices.ForEach(device => Console.Write(String.Format("{0} - {1}; ", device.Name, device.ProductId)));

            // We can get the same information from the CurrentDevices member
            Console.Write("\nDevices in CurrentDevices: ");
            jabraSdk.CurrentDevices.ForEach(device => Console.Write(String.Format("{0} - {1}; ", device.Name, device.ProductId)));
            Console.WriteLine("");
          }
        });

    jabraSdk.DeviceAdded.Subscribe(
        device =>
        {
            Console.WriteLine(String.Format("Added: {0} - {1}", device.Name, device.ProductId));
        });

    jabraSdk.DeviceRemoved.Subscribe(
        device =>
        {
            Console.WriteLine(String.Format("Removed: {0} - {1}", device.Name, device.ProductId));
        });
}

4.3 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:

private static void SubscribeToConnectionListChanges(IDevice device)
{
    device.ConnectionList.Subscribe(deviceConnections =>
    {
        Console.WriteLine(String.Format("Current list of connections for device'{0}':", device.Name));
        deviceConnections.ForEach(connection =\>
        {
            Console.WriteLine(String.Format("{0} - {1}", connection.Id,  connection.Type));
        });
    });
}

4.4 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.

private static void PrintDeviceConnections(IDevice device)
{
    Console.WriteLine(String.Format("Current list of connections for device {0}", device.Name));
    device.CurrentConnections.ForEach((connection) =>
    {
        Console.WriteLine(String.Format("{0} - {1}", connection.Id, connection.Type));
    });
}

private static void SubscribeToChanges(IDevice device)
{
    PrintDeviceConnections(device);
    device.ConnectionAdded.Subscribe((newConnection) =>
    {
        Console.WriteLine(String.Format("Added connection to {0} - {1}", device.Name, newConnection.Type));
        PrintDeviceConnections(device);
    });

    device.ConnectionRemoved.Subscribe((removedConnection) =>
    {
        Console.WriteLine(String.Format("Removed connection from {0} - {1}", 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:

using System.Reactive.Linq;
using Jabra.NET.Sdk.Core;
using Jabra.NET.Sdk.ToolsAndHelpers;

namespace FullDeviceAndConnectionSample
{
    class FullDeviceAndConnectionSample
    {
        static async Task Main()
        {
            // Initialize the Jabra SDK
            try
            {
                var jabraSdk = Init.InitSdk(new Config());

                // Subscribe to changes in the device list
                jabraSdk.DeviceAdded.Subscribe((device) =>
                {
                    Console.WriteLine(String.Format("New device - {0} - {1} - {2}", 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
                        .TakeUntil(device.OnDisconnect)
                        .Subscribe((connection) =>
                        {
                            Console.WriteLine(String.Format("{0}: new connection - {1}", device.Name, connection.Type));
                        });

                    device.ConnectionRemoved
                       .TakeUntil(device.OnDisconnect)
                       .Subscribe((connection) =>
                       {
                           Console.WriteLine(String.Format("{0}: removed connection - {1}", device.Name, connection.Type));
                       });
                });

                jabraSdk.DeviceRemoved.Subscribe((device) =>
                {
                    Console.WriteLine(String.Format("Removed device = {0}", device.Name));
                });
            }
            catch (JabraException ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Prevent the app from terminating, to allow for attaching and detaching.
            await Task.Delay(System.Threading.Timeout.Infinite);
        }
    }
}