Connecting and managing devices

This chapter teaches you to discover and manage Jabra devices through 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, and in some cases full code samples.

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 jabraCore)
{
    jabraCore.DeviceList.Subscribe((devices) =>
    {
        Debug.WriteLine("Current list of available devices:");
        devices.ForEach((device) =>
        {
            Debug.WriteLine(String.Format("{0} - {1}", device.Name,
                              device.ProductId));
        });
    });
}

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.

There is no immediate event following individually added or removed devices. If the device is added before you have set up an event listener, then no event is triggered. If you have already subscribed, then adding a new device results in an event.

The following code snippet shows how you can subscribe to DeviceAdded and DeviceRemoved events:

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

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

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

    jabraCore.DeviceRemoved.Subscribe(
        device =>
        {
            Debug.WriteLine(String.Format("Removed: {0} - {1}", 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:

// Note that the second half of this code sample is from the above section
// on getting a list of available devices
private static void SubscribeToConnectionListChanges(IDevice device)
{
    device.ConnectionList.Subscribe(deviceConnections =>
    {
        Debug.WriteLine(String.Format("Current list of connections for device'{0}':", device.Name));
        deviceConnections.ForEach(connection =>
        {
            Debug.WriteLine(String.Format("{0} - {1}", connection.Id,  connection.Type));
        });
    });
}

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

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.

There is no immediate event following individually added or removed connections. If the connection is added before you have set up an event listener, then no event is triggered. If you have already subscribed, then adding a new connection results in an event.

The following code sample shows how to set the event listener using ConnectionAdded and ConnectionRemoved events.

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

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

    device.ConnectionRemoved.Subscribe((removedConnection) =>
    {
        Debug.WriteLine(String.Format("Removed connection from {0} - {1}", device.Name, removedConnection.Type));
        PrintDeviceConnections(device);
    });
}

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

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
            {
                IApi jabraCore = Init.InitSdk(new Config());

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

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

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

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