-
-
Notifications
You must be signed in to change notification settings - Fork 120
IDevice
Christian Findlay edited this page Dec 30, 2018
·
6 revisions
IDevice is the common interface across platforms and connected device types that allows you to communicate with connected devices. This slim interface uses Task based Async programming pattern. This means that the user of the device should not lock up your UI. This interface can be used to achieve dependency injection. If your app or library needs to communicate with a device, you can pass a parameter in to your startup as IDevice. This means that your app is free to write and read to/from the device without having to worry about any platform specific code.
This is the interface:
public interface IDevice : IDisposable
{
/// <summary>
/// Occurs after the device has successfully connected. Note: this can be called multiple times and will occurr every time InitializeAsync is called successfull.
/// </summary>
event EventHandler Connected;
/// <summary>
/// Placeholder. This is not currently being used. Please do not rely on this at the moment.
/// </summary>
event EventHandler Disconnected;
/// <summary>
/// Checks to see if the device has been successfully connected. Note: check the implementation to see if this method is actually asking the device whether it is still connected or not
/// </summary>
Task<bool> GetIsConnectedAsync();
/// <summary>
/// Read a page of data. Warning: this is not thread safe. WriteAndReadAsync() should be preferred.
/// </summary>
Task<byte[]> ReadAsync();
/// <summary>
/// Write a page of data. Warning: this is not thread safe. WriteAndReadAsync() should be preferred.
/// </summary>
Task WriteAsync(byte[] data);
/// <summary>
/// Dispose of any existing connections and reinitialize the device.
/// </summary>
Task InitializeAsync();
/// <summary>
/// Write a page of data and then wait for the device to return a page. If the implementation derives from DeviceBase, this method is thread safe.
/// </summary>
Task<byte[]> WriteAndReadAsync(byte[] writeBuffer);
}The general pattern for usage is:
- Find your device with one of the factory classes (Example: WindowsDeviceFactoryBase)
- Call InitializeAsync()
- Exchange input and output with the device with the WriteAndReadAsync() method