Jc.OpenNov release for Avalonia

Published on

So I finally found some time to finish building the Jc.OpenNov library, which allows developers to read Novo Nordisk insulin pens into their Android/iOS apps. This release marks a significant milestone in the development of the library, as it provides a robust and reliable way to access insulin pen data from within mobile applications.

The Final Hurdle

The final hurdle was getting NFC working in iOS. This required a fair amount of effort to learn a little more about NFC to understand the Apple documentation and implement the necessary code. After a few times where I was close to banging my head against the wall, I was able to successfully implement NFC support for iOS devices and update the API to make it easier to consume.

Usage

As mentioned, the usage has changed slightly since my last post on the project... I have switched it to using event handlers over the previous callback-based approach.

To start listening for NFC tags, call:

OpenNov.Current.MonitorNfc(/* you may pass in an optional stop condition */);

Likewise, to stop listening for NFC tags, call:

OpenNov.Current.StopNfc();

To know when an NFC tag is detected and data is obtained, subscribe to the event handlers:

public MainViewModel()
{
    StarNfcCommand = ReactiveCommand.Create(StartNfc);
    StopNfcCommand = ReactiveCommand.Create(StopNfc);

    Avalonia.OpenNov.Current.OnDataRead += OnDataRead;
    Avalonia.OpenNov.Current.OnTagDetected += OnTagDetected;
    Avalonia.OpenNov.Current.OnError += OnError;
}

~MainViewModel()
{
    Avalonia.OpenNov.Current.OnDataRead -= OnDataRead;
    Avalonia.OpenNov.Current.OnTagDetected -= OnTagDetected;
    Avalonia.OpenNov.Current.OnError -= OnError;
}

private void OnDataRead(object? sender, Data.PenResult e)
{
    if (e is Data.PenResult.Success success)
    {
        Serial = success.Data.Serial;
    }
}

private void OnTagDetected(object? sender, ITag? e)
{
    var bytes = e?.GetId();
    if (bytes is null)
    {
        TagId = "Tag not found";
        return;
    }

    TagId = Convert.ToHexString(bytes);
}

private void OnError(object? sender, Exception e)
{
    Error = e.Message;
}

To see the latest usage instructions, visit the GitHub repository.