Library for reading data from NFC Novo Nordisk insulin pens in .NET.
C# implementation derived from lcacheux's Kotlin nov-open-reader project - big thanks!
Jc.OpenNov is a library designed to facilitate the reading of data from NFC Novo Nordisk insulin pens from iOS and Android in .NET.
- Jc.OpenNov: Core library containing data structure and protocol implementation.
- Jc.OpenNov.Nfc.Android: Android implementation of NFC communication using Jc.OpenNov.
- Jc.OpenNov.Avalonia: Avalonia implementation of NFC communication using Jc.OpenNov.
- Jc.OpenNov.Avalonia.Android: Android implementation of NFC communication using Jc.OpenNov.Avalonia.
- Jc.OpenNov.Avalonia.iOS: iOS implementation of NFC communication using Jc.OpenNov.Avalonia.
| Android | iOS | 
|---|---|
|  | 
To use Jc.OpenNov, you need to install the NuGet package:
dotnet add package Jc.OpenNovFollowed by adding the Android/iOS Jc.OpenNov.Nfc.xxx package to your project:
dotnet add package Jc.OpenNov.Nfc.AndroidInstall the following NuGet packages to their respective projects:
dotnet add package Jc.OpenNov.Avalonia
dotnet add package Jc.OpenNov.Avalonia.AndroidAdd the following to your AndroidManifest.xml:
<uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" />In your MainActivity, add to your AppBuilder like so:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        // ...
        .UseOpenNov(this);
}Install the following NuGet packages to their respective projects:
dotnet add package Jc.OpenNov.Avalonia
dotnet add package Jc.OpenNov.Avalonia.iOSAdd the following to your Entitlements.plist:
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
    <string>TAG</string>
</array>and the following to your Info.plist:
<key>NFCReaderUsageDescription</key>
<string>Used to retrieve data from Novopens.</string>
<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
<array>
    <string>D2760000850101</string>
    <string>E103</string>
    <string>E104</string>
</array>In your AppDelegate, add to your AppBuilder like so:
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
    return base.CustomizeAppBuilder(builder)
        // ...
        .UseOpenNov();
}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;
}