MAUI-gRPC

These examples consists of four projects. AntGrpcShared is a class library providing a gRPC client/server to be consumed by the other projects. AntGrpcService is a Windows service that provides services to connect to and communicate with an ANT USB stick. The MauiAntClientApp is a .NET MAUI project and gRPC client that is used to demonstrate Windows/Android applications that work with the remote server application on a local subnet. I have not tested the other platforms supported by .NET MAUI.

  About gRPC

gRPC is a Google defined protocol for remote procedure calls. It is the recommended RPC to use instead of WCF. Also, don't confuse gRPC channels with ANT radio channels. gRPC is a network protocol, ANT radio channels are a Dynastream/Garmin protocol for ANT radios.

The intent of AntGrpcService is to make the ANT USB stick accessible not only to client applications running on the local host but mobile device emulators and mobile devices connected to the network the service is running on.

The MAUI Example Projects

AntGrpcShared is the glue that facilitates communication between clients and the service. The benefit of using gRPC is that multiple clients can run simultaneously, both on the local PC and over the network.

AntGrpcShared

This class library is configured as a gRPC client/server library. The .proto files define the messages and data exchanged between client and server implementations.

AntGrpcService

The server is a Windows service that runs on the host PC that has an ANT USB stick connected to it. Three services are provided.

The DiscoveryService replies to client messages over a UDP multicast port on the local subnet. This allows clients to obtain the URI of the server and connect to the gRPC channel. Think of it as a poor man's service discovery protocol. No authentication or validation is performed.

The AntRadioService provides methods to get information about the connected ANT USB stick, initialize for continuous scan mode, and get channels to communicate with ANT devices via the AntChannelService.

The AntChannelService has a method to subscribe to a gRPC stream of ANT messages when the ANT USB stick has been initialized for continuous scan mode. The remaining gRPC/ANT radio channels are used to send extended acknowledged messages to individual ANT devices.

The service can be debugged! Make sure the service is not installed or disable it in the service controller (services.msc). Select the Debug solution configuration and launch it from Visual Studio. A console window will open. Logging output is displayed in the console window and breakpoints can be set.

MauiAntClientApp

The client app is the consumer of the client services provided by AntGrpcShared project. The server URI is first obtained via a UDP multicast message, a gRPC connection to the server is established, and then requests to initialize and communicate with the remote ANT USB stick are invoked.

AntGrpcServicePackage

The AntGrpcServicePackage installs the AntGrpcService on the local Windows PC and creates a firewall exception allowing network traffic between the service and any clients on the network. Wix is used to create the MSI file. You must install the Wix toolset to build this project.

How It Works

UDP multicast is used for service discovery and HTTP is used for communication between client and server. The multicast discovery service is invoked first to obtain the server URI, then the server gRPC channel is established to the server IP address obtained from the reply to the UDP message. I'm assuming the server and client app are on the same local subnet.

The UDP multicast endpoint is 239.55.43.6:55437. This is an arbitrary assignment in the multicast address range. You are certainly welcome to select your own address and port. The message sent/received is not relevant in this example, we're just interested in the server IP address in the received message. This is used in the next step to form the URI to connect to the gRPC server channel.

The client then connects to the gRPC service. The URI is constructed from the server IP address and the arbitrary port number created when the AntGrpcService project was created. The AntGrpcService launchSettings.json file has the port number. Two protocols are defined in launchSettings.json; I'm using the HTTP protocol and port number. Once the client is connected to the server the client initializes continuous scan mode. The client then subscribes to streaming messages from ANT channel 0 and the client app is up and running.

See Also