Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The act of connecting and disconnecting to a device, as well as maintaining a record of connected devices, is handled by the device manager. Before any work can be done on reading, writing, and subscribing to services, users must first create and work with the device manager.

Code Block
languagec#
VictorBleDeviceManager m_deviceManager = new VictorBleDeviceManager();

Scanning for Devices

Once a VictorBleDeviceManager has been instantiated, a scan must be performed to identify any active VictoR devices accepting connections over bluetooth. By default, scanning for devices is set to take 10 seconds, with a query for any devices with the term “victor” in its name, with case sensitivity not taken into account. Once the scan has completed, a Set of VictorDeviceInfo objects is returned, which each contain the name and address of a VictoR device.

Code Block
languagec#
var scannedDevices = await m_deviceManager.ScanForDevices();

Setting Scan Parameters

Users have a means to also specify VictorDeviceScanParameters when scanning for VictoR devices. Options include specifying the scan time, the string query for victor device names, and whether the case of the device name is important.

Code Block
languagec#
VictorDeviceScanParameters scanParameters = new VictorDeviceScanParameters();
scanParameters.ScanTime = scan;
scanParameters.DeviceName = "victor";
scanParameters.CaseSensitive = false;

var scannedDevices = await m_deviceManager.ScanForDevices(scanParameters);

Device Callbacks

Additionally, users can choose to receive a callback during the act of scanning for devices, rather than having to wait for the scan to complete. Callbacks can be set by passing a VictorDeviceCallback object that defines the onDeviceFound methodAction<VictorDeviceInfo>.

Code Block
languagec#
List<string> m_discoveredDevices = new List<string>();

public async Task Connect()
{
    var result = await m_deviceManager.ScanForDevices(OnDeviceAdded);
}

public async void OnDeviceAdded(VictorDeviceInfo deviceInfo)
{
    m_discoveredDevices.Add(deviceInfo.Address);
}

Connecting To A Device

Once a powered on VictoR device has been found by scanning for devices with the VictorBleDeviceManager, the resulting VictorDeviceInfo can be used to connect to the device along with the VictorBleDeviceManager that found the device.

Code Block
languagec#
List<string> m_discoveredDevices = new List<string>();

public async Task Connect()
{
    var result = await m_deviceManager.ScanForDevices(OnDeviceAdded);
}

public async void OnDeviceAdded(VictorDeviceInfo deviceInfo)
{
    m_discoveredDevices.Add(deviceInfo.Address);
    bool result = await m_deviceManager.Connect(deviceInfo.Address);
}

Subscribing to Connection State Changes

It is also possible to subscribe to the connection state of a VictoR device. This results in callbacks being triggered whenever the device is connected to or disconnected from.

Code Block
languagec#
public bool SubscribeConnectionState()
{
  bool subscriptionStatus = m_deviceManager.SubscribeConnectionStateChange(m_deviceName, ConnectionStateUpdated);

  return subscriptionStatus;
}

public void ConnectionStateUpdated(object sender, VictorConnectionStateChangedEventArgs args)
{
  onConnectionStateChanged.Invoke(args.Info.Address, args.Connected);
}

Disconnecting From A Device

A VictoR device can be disconnected by using the device manager that originally connected to the device, along with the device’s address.

Code Block
languagec#
await m_deviceManager.Disconnect(deviceID);

Reading From A VictoR Device

In order to read from a VictoR device, once a connection has been established through a device manager, a service object the attribute in question is associated with must first be created. Once the service object has been created, calls to read data from the connected device can be made, given the device manager containing the device has been passed to the service object on creation.

Code Block
languagec#
public async Task<byte[]> Read(Guid id)
{
    var data = await m_deviceManager.Read(m_deviceName, id);
    return data;
}

Deserialization of the data from the VictoR device is handled by the service objects, resulting in either a primitive data type or a custom made class defined in the SDK where appropriate. For example, when reading the serial number for the device through the DeviceInformationService class, the CompletableFuture returns a string.

...

Similar to reading from a VictoR device, writing to a VictoR device requires an established connection with a powered on device through a device manager and a service object that contains the data to be written. Serialization of data when writing to the VictoR device is handled by the service objects, converting the data into a byte array before being sent to the device.

Code Block
languagec#
public async Task<bool> Write(Guid id, byte[] data)
{
    return await m_deviceManager.Write(m_deviceName, vid, data);
}

NOTE: When writing to a device, please reference the supported ICD document specified above to ensure the attribute itself, as well as writing to the attribute, is supported.

...

Attributes on the VictoR device, when supported, can be subscribed to through the usage of the VictorDeviceCallback class EventHandler<VictorConnectionStateChangedEventArgs> and the service containing the data to be subscribed to.

Code Block
languagec#
public async Task<bool> SubscribeAttribute(Guid id)
{
    bool subscriptionStatus = await m_deviceManager.SubscribeAttributeChange(m_deviceName, id, AttributeChanged);

    return subscriptionStatus;
}

public void AttributeChanged(object sender, VictorAttributeChangedEventArgs args)
{
    onAttributeChanged.Invoke(args.Info.Address, args.Attribute, args.Data);
}